Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
"Strings3",
"Syntax",
"Templates",
"Trigraph",
"TypeRanges",
"Lambdas",
"Pointers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import Statements
import Strings
import Templates
import Toolchain
import Trigraph
import TrustBoundaries
import TypeRanges
import Uninitialized
Expand Down Expand Up @@ -123,6 +124,7 @@ newtype TCPPQuery =
TStringsPackageQuery(StringsQuery q) or
TTemplatesPackageQuery(TemplatesQuery q) or
TToolchainPackageQuery(ToolchainQuery q) or
TTrigraphPackageQuery(TrigraphQuery q) or
TTrustBoundariesPackageQuery(TrustBoundariesQuery q) or
TTypeRangesPackageQuery(TypeRangesQuery q) or
TUninitializedPackageQuery(UninitializedQuery q) or
Expand Down Expand Up @@ -187,6 +189,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isStringsQueryMetadata(query, queryId, ruleId, category) or
isTemplatesQueryMetadata(query, queryId, ruleId, category) or
isToolchainQueryMetadata(query, queryId, ruleId, category) or
isTrigraphQueryMetadata(query, queryId, ruleId, category) or
isTrustBoundariesQueryMetadata(query, queryId, ruleId, category) or
isTypeRangesQueryMetadata(query, queryId, ruleId, category) or
isUninitializedQueryMetadata(query, queryId, ruleId, category) or
Expand Down
26 changes: 26 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/Trigraph.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype TrigraphQuery = TTrigraphLikeSequencesShouldNotBeUsedQuery()

predicate isTrigraphQueryMetadata(Query query, string queryId, string ruleId, string category) {
query =
// `Query` instance for the `trigraphLikeSequencesShouldNotBeUsed` query
TrigraphPackage::trigraphLikeSequencesShouldNotBeUsedQuery() and
queryId =
// `@id` for the `trigraphLikeSequencesShouldNotBeUsed` query
"cpp/misra/trigraph-like-sequences-should-not-be-used" and
ruleId = "RULE-5-0-1" and
category = "advisory"
}

module TrigraphPackage {
Query trigraphLikeSequencesShouldNotBeUsedQuery() {
//autogenerate `Query` type
result =
// `Query` type for `trigraphLikeSequencesShouldNotBeUsed` query
TQueryCPP(TTrigraphPackageQuery(TTrigraphLikeSequencesShouldNotBeUsedQuery()))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @id cpp/misra/trigraph-like-sequences-should-not-be-used
* @name RULE-5-0-1: Trigraph-like sequences should not be used
* @description Using trigraph-like sequences can lead to developer confusion.
* @kind problem
* @precision medium
* @problem.severity warning
* @tags external/misra/id/rule-5-0-1
* maintainability
* readability
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra

class CanContainSequenceType extends Locatable {
CanContainSequenceType() {
this instanceof StringLiteral
or
this instanceof Comment
or
this instanceof Macro
}

string getContent() {
result = this.(StringLiteral).getValue()
or
result = this.(Comment).getContents()
or
result = this.(Macro).getBody()
}
}

from CanContainSequenceType s, int occurrenceOffset, string substring
where
not isExcluded(s, TrigraphPackage::trigraphLikeSequencesShouldNotBeUsedQuery()) and
substring = s.getContent().regexpFind("\\?\\?[=/'()!<>-]", _, occurrenceOffset) and
//one escape character is enough to mean this isnt a trigraph-like sequence
not exists(s.(StringLiteral).getASimpleEscapeSequence(_, occurrenceOffset + 1))
select s, "Trigraph-like sequence found: '" + substring + "'."
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| test.cpp:1:17:1:21 | ??= | Trigraph-like sequence found: '??='. |
| test.cpp:2:18:2:22 | ??/ | Trigraph-like sequence found: '??/'. |
| test.cpp:3:18:3:22 | ??' | Trigraph-like sequence found: '??''. |
| test.cpp:4:18:4:22 | ??( | Trigraph-like sequence found: '??('. |
| test.cpp:5:18:5:22 | ??) | Trigraph-like sequence found: '??)'. |
| test.cpp:6:18:6:22 | ??! | Trigraph-like sequence found: '??!'. |
| test.cpp:7:18:7:22 | ??< | Trigraph-like sequence found: '??<'. |
| test.cpp:8:18:8:22 | ??> | Trigraph-like sequence found: '??>'. |
| test.cpp:9:18:9:22 | ??- | Trigraph-like sequence found: '??-'. |
| test.cpp:14:1:14:45 | // comment trigraph-like ??= // NON_COMPLIANT | Trigraph-like sequence found: '??='. |
| test.cpp:17:1:17:25 | #define TRIGRAPH_LIKE ??= | Trigraph-like sequence found: '??='. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-5-0-1/TrigraphLikeSequencesShouldNotBeUsed.ql
17 changes: 17 additions & 0 deletions cpp/misra/test/rules/RULE-5-0-1/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const char *g = "??="; // NON_COMPLIANT
const char *g1 = "??/"; // NON_COMPLIANT
const char *g2 = "??'"; // NON_COMPLIANT
const char *g3 = "??("; // NON_COMPLIANT
const char *g4 = "??)"; // NON_COMPLIANT
const char *g5 = "??!"; // NON_COMPLIANT
const char *g6 = "??<"; // NON_COMPLIANT
const char *g7 = "??>"; // NON_COMPLIANT
const char *g8 = "??-"; // NON_COMPLIANT

const char *g9 = "\?\?="; // COMPLIANT
const char *g10 = "?="; // COMPLIANT

// comment trigraph-like ??= // NON_COMPLIANT

// clang-format off
#define TRIGRAPH_LIKE ??= // NON_COMPLIANT - format off otherwise it separates the characters
29 changes: 29 additions & 0 deletions rule_packages/cpp/Trigraph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"MISRA-C++-2023": {
"RULE-5-0-1": {
"properties": {
"enforcement": "decidable",
"obligation": "advisory"
},
"queries": [
{
"description": "Using trigraph-like sequences can lead to developer confusion.",
"kind": "problem",
"name": "Trigraph-like sequences should not be used",
"precision": "medium",
"severity": "warning",
"short_name": "TrigraphLikeSequencesShouldNotBeUsed",
"tags": [
"maintainability",
"readability",
"scope/single-translation-unit"
],
"implementation_scope": {
"description": "The rule checks within string literals only for trigraph-like sequences."
}
}
],
"title": "Trigraph-like sequences should not be used"
}
}
}
Loading