-
Notifications
You must be signed in to change notification settings - Fork 74
Add trigraph rule RULE-5-0-1 #1022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -290,6 +290,7 @@ | |
| "Strings3", | ||
| "Syntax", | ||
| "Templates", | ||
| "Trigraph", | ||
| "TypeRanges", | ||
| "Lambdas", | ||
| "Pointers", | ||
|
|
||
| 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,24 @@ | ||||||
| /** | ||||||
| * @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 | ||||||
| * readability | ||||||
| * scope/single-translation-unit | ||||||
| * external/misra/enforcement/decidable | ||||||
| * external/misra/obligation/advisory | ||||||
| */ | ||||||
|
|
||||||
| import cpp | ||||||
| import codingstandards.cpp.misra | ||||||
|
|
||||||
| from StringLiteral s, int occurrenceOffset | ||||||
| where | ||||||
| not isExcluded(s, TrigraphPackage::trigraphLikeSequencesShouldNotBeUsedQuery()) and | ||||||
| exists(s.getValue().regexpFind("\\?\\?[=/'()!<>-]", _, occurrenceOffset)) and | ||||||
| //one escape character is enough to mean this isnt a trigraph-like sequence | ||||||
|
||||||
| //one escape character is enough to mean this isnt a trigraph-like sequence | |
| //one escape character is enough to mean this isn't a trigraph-like sequence |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case this is strangely buried
I'd recommend doing one of:
- reporting the offset in the string literal here
- and/or showing the text, e.g. "??=" in the error message
- or to be super fancy, create a class with a custom
hasLocation(...)predicate that selects the subsection of the string with the sequence
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| | test.cpp:1:17:1:21 | ??= | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:2:18:2:22 | ??/ | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:3:18:3:22 | ??' | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:4:18:4:22 | ??( | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:5:18:5:22 | ??) | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:6:18:6:22 | ??! | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:7:18:7:22 | ??< | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:8:18:8:22 | ??> | Trigraph-like sequence used in string literal. | | ||
| | test.cpp:9:18:9:22 | ??- | Trigraph-like sequence used in string literal. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-5-0-1/TrigraphLikeSequencesShouldNotBeUsed.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "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": [ | ||
| "readability", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add portability as well |
||
| "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" | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also check comments and macros?