-
Notifications
You must be signed in to change notification settings - Fork 76
Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and 19-2-1 #893
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
Merged
mbaluda
merged 17 commits into
main
from
michaelrfairhurst/implement-package-preprocessor
Feb 26, 2026
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9830cc0
Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and …
MichaelRFairhurst 2095d1d
Fix Preprocessor.json, for defined in if directive
MichaelRFairhurst ef9d96b
Fix package errors
MichaelRFairhurst a49c0d1
s/maintanability/maintainability
MichaelRFairhurst ea173e5
Regenerate query metadata
MichaelRFairhurst a31e047
Format additional files
MichaelRFairhurst b47ec45
Merge branch 'main' into michaelrfairhurst/implement-package-preproce…
lcartey 39ef003
Update to use features from codeql-qtil
MichaelRFairhurst 5da3a00
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst eb5453b
Update to 0.0.3 which has fixes for codeql/util breaking change.
MichaelRFairhurst 5d22374
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 3d578f0
Address feedback
MichaelRFairhurst a83e513
Merge branch 'main' into michaelrfairhurst/implement-package-preproce…
MichaelRFairhurst c808c96
Merge branch 'main' into michaelrfairhurst/implement-package-preproce…
MichaelRFairhurst 98e2c71
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst c0b2aa3
Update cpp/misra/src/rules/RULE-19-2-1/NoValidIfdefGuardInHeader.ql
mbaluda 660830b
Update rule_packages/cpp/Preprocessor.json
mbaluda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
cpp/common/src/codingstandards/cpp/exclusions/cpp/Preprocessor.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| //** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
| import cpp | ||
| import RuleMetadata | ||
| import codingstandards.cpp.exclusions.RuleMetadata | ||
|
|
||
| newtype PreprocessorQuery = | ||
| TUndefOfMacroNotDefinedInFileQuery() or | ||
| TInvalidTokenInDefinedOperatorQuery() or | ||
| TDefinedOperatorExpandedInIfDirectiveQuery() or | ||
| TNoValidIfdefGuardInHeaderQuery() or | ||
| TIncludeOutsideGuardQuery() | ||
|
|
||
| predicate isPreprocessorQueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `undefOfMacroNotDefinedInFile` query | ||
| PreprocessorPackage::undefOfMacroNotDefinedInFileQuery() and | ||
| queryId = | ||
| // `@id` for the `undefOfMacroNotDefinedInFile` query | ||
| "cpp/misra/undef-of-macro-not-defined-in-file" and | ||
| ruleId = "RULE-19-0-4" and | ||
| category = "advisory" | ||
| or | ||
| query = | ||
| // `Query` instance for the `invalidTokenInDefinedOperator` query | ||
| PreprocessorPackage::invalidTokenInDefinedOperatorQuery() and | ||
| queryId = | ||
| // `@id` for the `invalidTokenInDefinedOperator` query | ||
| "cpp/misra/invalid-token-in-defined-operator" and | ||
| ruleId = "RULE-19-1-1" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `definedOperatorExpandedInIfDirective` query | ||
| PreprocessorPackage::definedOperatorExpandedInIfDirectiveQuery() and | ||
| queryId = | ||
| // `@id` for the `definedOperatorExpandedInIfDirective` query | ||
| "cpp/misra/defined-operator-expanded-in-if-directive" and | ||
| ruleId = "RULE-19-1-1" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `noValidIfdefGuardInHeader` query | ||
| PreprocessorPackage::noValidIfdefGuardInHeaderQuery() and | ||
| queryId = | ||
| // `@id` for the `noValidIfdefGuardInHeader` query | ||
| "cpp/misra/no-valid-ifdef-guard-in-header" and | ||
| ruleId = "RULE-19-2-1" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `includeOutsideGuard` query | ||
| PreprocessorPackage::includeOutsideGuardQuery() and | ||
| queryId = | ||
| // `@id` for the `includeOutsideGuard` query | ||
| "cpp/misra/include-outside-guard" and | ||
| ruleId = "RULE-19-2-1" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module PreprocessorPackage { | ||
| Query undefOfMacroNotDefinedInFileQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `undefOfMacroNotDefinedInFile` query | ||
| TQueryCPP(TPreprocessorPackageQuery(TUndefOfMacroNotDefinedInFileQuery())) | ||
| } | ||
|
|
||
| Query invalidTokenInDefinedOperatorQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `invalidTokenInDefinedOperator` query | ||
| TQueryCPP(TPreprocessorPackageQuery(TInvalidTokenInDefinedOperatorQuery())) | ||
| } | ||
|
|
||
| Query definedOperatorExpandedInIfDirectiveQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `definedOperatorExpandedInIfDirective` query | ||
| TQueryCPP(TPreprocessorPackageQuery(TDefinedOperatorExpandedInIfDirectiveQuery())) | ||
| } | ||
|
|
||
| Query noValidIfdefGuardInHeaderQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `noValidIfdefGuardInHeader` query | ||
| TQueryCPP(TPreprocessorPackageQuery(TNoValidIfdefGuardInHeaderQuery())) | ||
| } | ||
|
|
||
| Query includeOutsideGuardQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `includeOutsideGuard` query | ||
| TQueryCPP(TPreprocessorPackageQuery(TIncludeOutsideGuardQuery())) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cpp/misra/src/rules/RULE-19-0-4/UndefOfMacroNotDefinedInFile.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * @id cpp/misra/undef-of-macro-not-defined-in-file | ||
| * @name RULE-19-0-4: #undef should only be used for macros defined previously in the same file | ||
| * @description Using #undef to undefine a macro that is not defined in the same file can lead to | ||
| * confusion. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity warning | ||
| * @tags external/misra/id/rule-19-0-4 | ||
| * scope/single-translation-unit | ||
| * readability | ||
| * maintainability | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import qtil.Qtil | ||
|
|
||
| class DefOrUndef extends PreprocessorDirective { | ||
| DefOrUndef() { this instanceof PreprocessorUndef or this instanceof Macro } | ||
|
|
||
| string getName() { | ||
| result = this.(PreprocessorUndef).getName() or | ||
| result = this.(Macro).getName() | ||
| } | ||
| } | ||
|
|
||
| predicate relevantNameAndFile(string name, File file) { | ||
| exists(DefOrUndef m | | ||
| m.getName() = name and | ||
| m.getFile() = file | ||
| ) | ||
| } | ||
|
|
||
| class StringFilePair = Qtil::Pair<string, File, relevantNameAndFile/2>::Pair; | ||
|
|
||
| /** | ||
| * Defs and undefs ordered by location, grouped by name and file. | ||
| */ | ||
| class OrderedDefOrUndef extends Qtil::Ordered<DefOrUndef>::GroupBy<StringFilePair>::Type { | ||
| override int getOrder() { result = getLocation().getStartLine() } | ||
|
|
||
| override StringFilePair getGroup() { | ||
| result.getFirst() = getName() and result.getSecond() = getFile() | ||
| } | ||
| } | ||
|
|
||
| from OrderedDefOrUndef defOrUndef | ||
| where | ||
| not isExcluded(defOrUndef, PreprocessorPackage::undefOfMacroNotDefinedInFileQuery()) and | ||
| // There exists an #undef for a given name and file | ||
| defOrUndef instanceof PreprocessorUndef and | ||
| // A previous def or undef of this name must exist in this file, and it must be a #define | ||
| not defOrUndef.getPrevious() instanceof Macro | ||
| select defOrUndef, "Undef of name '" + defOrUndef.getName() + "' not defined in the same file." |
27 changes: 27 additions & 0 deletions
27
cpp/misra/src/rules/RULE-19-1-1/DefinedOperatorExpandedInIfDirective.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * @id cpp/misra/defined-operator-expanded-in-if-directive | ||
| * @name RULE-19-1-1: The defined preprocessor operator shall be used appropriately | ||
| * @description Macro expansions that produce the token 'defined' inside of an if directive result | ||
| * in undefined behavior. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-19-1-1 | ||
| * scope/single-translation-unit | ||
| * correctness | ||
| * maintainability | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
||
| from PreprocessorIf ifDirective, MacroInvocation mi | ||
| where | ||
| not isExcluded(ifDirective, PreprocessorPackage::definedOperatorExpandedInIfDirectiveQuery()) and | ||
| ifDirective.getLocation().subsumes(mi.getLocation()) and | ||
| mi.getMacro().getBody().regexpMatch(".*defined.*") | ||
| select ifDirective, | ||
| "If directive contains macro expansion including the token 'defined' from macro $@, which results in undefined behavior.", | ||
| mi.getMacro(), mi.getMacroName() |
42 changes: 42 additions & 0 deletions
42
cpp/misra/src/rules/RULE-19-1-1/InvalidTokenInDefinedOperator.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * @id cpp/misra/invalid-token-in-defined-operator | ||
| * @name RULE-19-1-1: The defined preprocessor operator shall be used appropriately | ||
| * @description Using the defined operator without an immediately following optionally parenthesized | ||
| * identifier results in undefined behavior. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-19-1-1 | ||
| * scope/single-translation-unit | ||
| * correctness | ||
| * maintainability | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
||
| string idRegex() { result = "[a-zA-Z_]([a-zA-Z_0-9]*)" } | ||
|
|
||
| bindingset[body] | ||
| predicate hasInvalidDefinedOperator(string body) { | ||
| body.regexpMatch(".*\\bdefined" + | ||
| // Contains text "defined" at a word break | ||
| // Negative zero width lookahead: | ||
| "(?!(" + | ||
| // (group) optional whitespace followed by a valid identifier | ||
| "(\\s*" + idRegex() + ")" + | ||
| // or | ||
| "|" + | ||
| // (group) optional whitespace followed by parenthesis and valid identifier | ||
| "(\\s*\\(\\s*" + idRegex() + "\\s*\\))" + | ||
| // End negative zero width lookahead, match remaining text | ||
| ")).*") | ||
| } | ||
|
|
||
| from PreprocessorIf ifDirective | ||
| where | ||
| not isExcluded(ifDirective, PreprocessorPackage::invalidTokenInDefinedOperatorQuery()) and | ||
| hasInvalidDefinedOperator(ifDirective.getHead()) | ||
| select ifDirective, "Invalid use of defined operator in if directive." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * @id cpp/misra/include-outside-guard | ||
| * @name RULE-19-2-1: Comments are the only content permitted outside of the scope of an include guard in a header file | ||
| * @description Include directives shall be within the scope of an include guard. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-19-2-1 | ||
| * scope/single-translation-unit | ||
| * maintainability | ||
| * correctness | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import semmle.code.cpp.headers.MultipleInclusion | ||
|
|
||
| predicate isOutside(CorrectIncludeGuard includeGuard, Location location) { | ||
| location.getFile() = includeGuard.getFile() and | ||
| ( | ||
| location.isBefore(includeGuard.getIfndef().getLocation()) | ||
| or | ||
| includeGuard.getEndif().getLocation().isBefore(location) | ||
| ) | ||
| } | ||
|
|
||
| from Include include, CorrectIncludeGuard includeGuard, HeaderFile header | ||
| where | ||
| not isExcluded(include, PreprocessorPackage::includeOutsideGuardQuery()) and | ||
| includeGuard.getFile() = header and | ||
| header = include.getFile() and | ||
| isOutside(includeGuard, include.getLocation()) | ||
| select include, "Include is outside of its header file's $@.", includeGuard.getIfndef(), | ||
| "include guard" |
23 changes: 23 additions & 0 deletions
23
cpp/misra/src/rules/RULE-19-2-1/NoValidIfdefGuardInHeader.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * @id cpp/misra/no-valid-ifdef-guard-in-header | ||
| * @name RULE-19-2-1: Precautions shall be taken in order to prevent the contents of a header file being included more | ||
| * @description Precautions shall be taken in order to prevent the contents of a header file being | ||
| * included more than once. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-19-2-1 | ||
| * scope/single-translation-unit | ||
| * maintainability | ||
| * correctness | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.includeguardsnotused.IncludeGuardsNotUsed | ||
|
|
||
| class NoValidIfdefGuardInHeaderQuery extends IncludeGuardsNotUsedSharedQuery { | ||
| NoValidIfdefGuardInHeaderQuery() { this = PreprocessorPackage::noValidIfdefGuardInHeaderQuery() } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
cpp/misra/test/rules/RULE-19-0-4/UndefOfMacroNotDefinedInFile.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | test.cpp:3:1:3:9 | #undef M1 | Undef of name 'M1' not defined in the same file. | | ||
| | test.cpp:6:1:6:9 | #undef M1 | Undef of name 'M1' not defined in the same file. | | ||
| | test.cpp:7:1:7:9 | #undef M2 | Undef of name 'M2' not defined in the same file. | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-19-0-4/UndefOfMacroNotDefinedInFile.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-0-4/UndefOfMacroNotDefinedInFile.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #define M1 | ||
| #undef M1 // COMPLIANT | ||
| #undef M1 // NON-COMPLIANT | ||
| #define M1 | ||
| #undef M1 // COMPLIANT | ||
| #undef M1 // NON-COMPLIANT | ||
| #undef M2 // NON-COMPLIANT |
3 changes: 3 additions & 0 deletions
3
cpp/misra/test/rules/RULE-19-1-1/DefinedOperatorExpandedInIfDirective.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | test.cpp:39:1:39:6 | #if M1 | If directive contains macro expansion including the token 'defined' from macro $@, which results in undefined behavior. | test.cpp:34:1:34:18 | #define M1 defined | M1 | | ||
| | test.cpp:41:1:41:6 | #if M2 | If directive contains macro expansion including the token 'defined' from macro $@, which results in undefined behavior. | test.cpp:35:1:35:30 | #define M2 1 + 2 + defined + 3 | M2 | | ||
| | test.cpp:43:1:43:6 | #if M3 | If directive contains macro expansion including the token 'defined' from macro $@, which results in undefined behavior. | test.cpp:35:1:35:30 | #define M2 1 + 2 + defined + 3 | M2 | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-19-1-1/DefinedOperatorExpandedInIfDirective.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-1-1/DefinedOperatorExpandedInIfDirective.ql |
3 changes: 3 additions & 0 deletions
3
cpp/misra/test/rules/RULE-19-1-1/InvalidTokenInDefinedOperator.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | test.cpp:11:1:11:11 | #if defined | Invalid use of defined operator in if directive. | | ||
| | test.cpp:13:1:13:26 | #if defined(M1) && defined | Invalid use of defined operator in if directive. | | ||
| | test.cpp:15:1:15:26 | #if defined && defined(M1) | Invalid use of defined operator in if directive. | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-19-1-1/InvalidTokenInDefinedOperator.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-1-1/InvalidTokenInDefinedOperator.ql |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.