Skip to content

Commit dfe986f

Browse files
Merge branch 'main' into jeongsoolee09/MISRA-C++-Memory2-Memory3
2 parents 017f019 + 4f23719 commit dfe986f

File tree

8 files changed

+131
-0
lines changed

8 files changed

+131
-0
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
"Strings3",
293293
"Syntax",
294294
"Templates",
295+
"Trigraph",
295296
"TypeRanges",
296297
"Lambdas",
297298
"Pointers",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import Statements
6262
import Strings
6363
import Templates
6464
import Toolchain
65+
import Trigraph
6566
import TrustBoundaries
6667
import TypeRanges
6768
import Uninitialized
@@ -129,6 +130,7 @@ newtype TCPPQuery =
129130
TStringsPackageQuery(StringsQuery q) or
130131
TTemplatesPackageQuery(TemplatesQuery q) or
131132
TToolchainPackageQuery(ToolchainQuery q) or
133+
TTrigraphPackageQuery(TrigraphQuery q) or
132134
TTrustBoundariesPackageQuery(TrustBoundariesQuery q) or
133135
TTypeRangesPackageQuery(TypeRangesQuery q) or
134136
TUninitializedPackageQuery(UninitializedQuery q) or
@@ -196,6 +198,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
196198
isStringsQueryMetadata(query, queryId, ruleId, category) or
197199
isTemplatesQueryMetadata(query, queryId, ruleId, category) or
198200
isToolchainQueryMetadata(query, queryId, ruleId, category) or
201+
isTrigraphQueryMetadata(query, queryId, ruleId, category) or
199202
isTrustBoundariesQueryMetadata(query, queryId, ruleId, category) or
200203
isTypeRangesQueryMetadata(query, queryId, ruleId, category) or
201204
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
* maintainability
10+
* readability
11+
* scope/single-translation-unit
12+
* external/misra/enforcement/decidable
13+
* external/misra/obligation/advisory
14+
*/
15+
16+
import cpp
17+
import codingstandards.cpp.misra
18+
19+
class CanContainSequenceType extends Locatable {
20+
CanContainSequenceType() {
21+
this instanceof StringLiteral
22+
or
23+
this instanceof Comment
24+
or
25+
this instanceof Macro
26+
}
27+
28+
string getContent() {
29+
result = this.(StringLiteral).getValue()
30+
or
31+
result = this.(Comment).getContents()
32+
or
33+
result = this.(Macro).getBody()
34+
}
35+
}
36+
37+
from CanContainSequenceType s, int occurrenceOffset, string substring
38+
where
39+
not isExcluded(s, TrigraphPackage::trigraphLikeSequencesShouldNotBeUsedQuery()) and
40+
substring = s.getContent().regexpFind("\\?\\?[=/'()!<>-]", _, occurrenceOffset) and
41+
//one escape character is enough to mean this isnt a trigraph-like sequence
42+
not exists(s.(StringLiteral).getASimpleEscapeSequence(_, occurrenceOffset + 1))
43+
select s, "Trigraph-like sequence found: '" + substring + "'."
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| test.cpp:1:17:1:21 | ??= | Trigraph-like sequence found: '??='. |
2+
| test.cpp:2:18:2:22 | ??/ | Trigraph-like sequence found: '??/'. |
3+
| test.cpp:3:18:3:22 | ??' | Trigraph-like sequence found: '??''. |
4+
| test.cpp:4:18:4:22 | ??( | Trigraph-like sequence found: '??('. |
5+
| test.cpp:5:18:5:22 | ??) | Trigraph-like sequence found: '??)'. |
6+
| test.cpp:6:18:6:22 | ??! | Trigraph-like sequence found: '??!'. |
7+
| test.cpp:7:18:7:22 | ??< | Trigraph-like sequence found: '??<'. |
8+
| test.cpp:8:18:8:22 | ??> | Trigraph-like sequence found: '??>'. |
9+
| test.cpp:9:18:9:22 | ??- | Trigraph-like sequence found: '??-'. |
10+
| test.cpp:14:1:14:45 | // comment trigraph-like ??= // NON_COMPLIANT | Trigraph-like sequence found: '??='. |
11+
| test.cpp:17:1:17:25 | #define TRIGRAPH_LIKE ??= | Trigraph-like sequence found: '??='. |
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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
12+
const char *g10 = "?="; // COMPLIANT
13+
14+
// comment trigraph-like ??= // NON_COMPLIANT
15+
16+
// clang-format off
17+
#define TRIGRAPH_LIKE ??= // NON_COMPLIANT - format off otherwise it separates the characters

rule_packages/cpp/Trigraph.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
"maintainability",
18+
"readability",
19+
"scope/single-translation-unit"
20+
],
21+
"implementation_scope": {
22+
"description": "The rule checks within string literals only for trigraph-like sequences."
23+
}
24+
}
25+
],
26+
"title": "Trigraph-like sequences should not be used"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)