Skip to content

Commit e793f83

Browse files
committed
Add trigraph rule for RULE-5-0-1
1 parent 1de2ad0 commit e793f83

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
"Strings3",
291291
"Syntax",
292292
"Templates",
293+
"Trigraph",
293294
"TypeRanges",
294295
"Lambdas",
295296
"Pointers",

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import Statements
5959
import Strings
6060
import Templates
6161
import Toolchain
62+
import Trigraph
6263
import TrustBoundaries
6364
import TypeRanges
6465
import Uninitialized
@@ -123,6 +124,7 @@ newtype TCPPQuery =
123124
TStringsPackageQuery(StringsQuery q) or
124125
TTemplatesPackageQuery(TemplatesQuery q) or
125126
TToolchainPackageQuery(ToolchainQuery q) or
127+
TTrigraphPackageQuery(TrigraphQuery q) or
126128
TTrustBoundariesPackageQuery(TrustBoundariesQuery q) or
127129
TTypeRangesPackageQuery(TypeRangesQuery q) or
128130
TUninitializedPackageQuery(UninitializedQuery q) or
@@ -187,6 +189,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
187189
isStringsQueryMetadata(query, queryId, ruleId, category) or
188190
isTemplatesQueryMetadata(query, queryId, ruleId, category) or
189191
isToolchainQueryMetadata(query, queryId, ruleId, category) or
192+
isTrigraphQueryMetadata(query, queryId, ruleId, category) or
190193
isTrustBoundariesQueryMetadata(query, queryId, ruleId, category) or
191194
isTypeRangesQueryMetadata(query, queryId, ruleId, category) or
192195
isUninitializedQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype TrigraphQuery = TTrigraphLikeSequencesShouldNotBeUsedQuery()
7+
8+
predicate isTrigraphQueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `trigraphLikeSequencesShouldNotBeUsed` query
11+
TrigraphPackage::trigraphLikeSequencesShouldNotBeUsedQuery() and
12+
queryId =
13+
// `@id` for the `trigraphLikeSequencesShouldNotBeUsed` query
14+
"cpp/misra/trigraph-like-sequences-should-not-be-used" and
15+
ruleId = "RULE-5-0-1" and
16+
category = "advisory"
17+
}
18+
19+
module TrigraphPackage {
20+
Query trigraphLikeSequencesShouldNotBeUsedQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `trigraphLikeSequencesShouldNotBeUsed` query
24+
TQueryCPP(TTrigraphPackageQuery(TTrigraphLikeSequencesShouldNotBeUsedQuery()))
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id cpp/misra/trigraph-like-sequences-should-not-be-used
3+
* @name RULE-5-0-1: Trigraph-like sequences should not be used
4+
* @description Using trigraph-like sequences can lead to developer confusion.
5+
* @kind problem
6+
* @precision medium
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-5-0-1
9+
* readability
10+
* scope/single-translation-unit
11+
* external/misra/enforcement/decidable
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.cpp.misra
17+
18+
from StringLiteral s, int occurrenceOffset
19+
where
20+
not isExcluded(s, TrigraphPackage::trigraphLikeSequencesShouldNotBeUsedQuery()) and
21+
exists(s.getValue().regexpFind("\\?\\?[=/'()!<>-]", _, occurrenceOffset)) and
22+
//one escape character is enough to mean this isnt a trigraph-like sequence
23+
not exists(s.getASimpleEscapeSequence(_, occurrenceOffset + 1))
24+
select s, "Trigraph-like sequence used in string literal."
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| test.cpp:1:18:1:22 | ??= | Trigraph-like sequence used in string literal. |
2+
| test.cpp:2:19:2:23 | ??/ | Trigraph-like sequence used in string literal. |
3+
| test.cpp:3:19:3:23 | ??' | Trigraph-like sequence used in string literal. |
4+
| test.cpp:4:19:4:23 | ??( | Trigraph-like sequence used in string literal. |
5+
| test.cpp:5:19:5:23 | ??) | Trigraph-like sequence used in string literal. |
6+
| test.cpp:6:19:6:23 | ??! | Trigraph-like sequence used in string literal. |
7+
| test.cpp:7:19:7:23 | ??< | Trigraph-like sequence used in string literal. |
8+
| test.cpp:8:19:8:23 | ??> | Trigraph-like sequence used in string literal. |
9+
| test.cpp:9:19:9:23 | ??- | Trigraph-like sequence used in string literal. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-0-1/TrigraphLikeSequencesShouldNotBeUsed.ql
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const char * g = "??="; // NON_COMPLIANT
2+
const char * g1 = "??/"; // NON_COMPLIANT
3+
const char * g2 = "??'"; // NON_COMPLIANT
4+
const char * g3 = "??("; // NON_COMPLIANT
5+
const char * g4 = "??)"; // NON_COMPLIANT
6+
const char * g5 = "??!"; // NON_COMPLIANT
7+
const char * g6 = "??<"; // NON_COMPLIANT
8+
const char * g7 = "??>"; // NON_COMPLIANT
9+
const char * g8 = "??-"; // NON_COMPLIANT
10+
11+
const char * g9 = "\?\?="; // COMPLIANT[FALSE_POSITIVE]
12+
const char * g10 = "?="; // COMPLIANT

rule_packages/cpp/Trigraph.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-5-0-1": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "advisory"
7+
},
8+
"queries": [
9+
{
10+
"description": "Using trigraph-like sequences can lead to developer confusion.",
11+
"kind": "problem",
12+
"name": "Trigraph-like sequences should not be used",
13+
"precision": "medium",
14+
"severity": "warning",
15+
"short_name": "TrigraphLikeSequencesShouldNotBeUsed",
16+
"tags": [
17+
"readability",
18+
"scope/single-translation-unit"
19+
],
20+
"implementation_scope": {
21+
"description": "The rule checks within string literals only for trigraph-like sequences."
22+
}
23+
}
24+
],
25+
"title": "Trigraph-like sequences should not be used"
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)