- Notifications
You must be signed in to change notification settings - Fork70
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 ourterms of service andprivacy 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?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
9830cc02095d1def9d96ba49c0d1ea173e5a31e047b47ec4539ef0035da3a00eb5453bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| //** 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() | ||
| 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" | ||
| } | ||
| 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())) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,3 +6,4 @@ license: MIT | ||
| dependencies: | ||
| codeql/common-cpp-coding-standards: '*' | ||
| codeql/cpp-all: 4.0.3 | ||
| advanced-security/qtil: 0.0.3 | ||
| 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." |
| 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() |
| 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." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * @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 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 File included | ||
| where | ||
| not isExcluded(included, PreprocessorPackage::noValidIfdefGuardInHeaderQuery()) and | ||
| included = any(Compilation c).getAFileCompiled().getAnIncludedFile+() and | ||
Contributor 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. In other Coding Standard queries we use the | ||
| not exists(CorrectIncludeGuard includeGuard | | ||
| includeGuard.getFile() = included and | ||
| // Stricter: define is before all other contents | ||
| not included | ||
| .getATopLevelDeclaration() | ||
| .getLocation() | ||
| .isBefore(includeGuard.getDefine().getLocation()) and | ||
| // Stricter: do not allow includes outside of the inclusion guard | ||
| not exists(Include include | isOutside(includeGuard, include.getLocation())) | ||
Contributor 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. My recollection is that these two conditions are already factored in within the the exists(Locationloc| loc=ifndef.getLocation() and loc.getFile()= hf and loc.getStartLine()=min(int l|includeGuardRelevantLine(hf,l))) Which should enforce that the condition appears on the first meaningful line of the file? | ||
| ) | ||
| select included, "File does not have a well formatted include guard." | ||
Contributor 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 suggest reviewing the | ||
| 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. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-0-4/UndefOfMacroNotDefinedInFile.ql |
| 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 |
| 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 | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-1-1/DefinedOperatorExpandedInIfDirective.ql |
| 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. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-1-1/InvalidTokenInDefinedOperator.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #if defined M1 // COMPLIANT | ||
| #endif | ||
| #if defined(M1) // COMPLIANT | ||
| #endif | ||
| #if defined(M1) // COMPLIANT | ||
| #endif | ||
| #if defined M1 && defined M2 // COMPLIANT | ||
| #endif | ||
| #if defined(M1) && defined(M2) // COMPLIANT | ||
| #endif | ||
| #if defined // NON-COMPLIANT | ||
| #endif | ||
| #if defined(M1) && defined // NON-COMPLIANT | ||
| #endif | ||
| #if defined && defined(M1) // NON-COMPLIANT | ||
| #endif | ||
| // Compliant, there are no keywords in the context of the preprocessor, only | ||
| // identifiers. Therefore, 'new' is a valid identifier. | ||
| #if defined new // COMPLIANT | ||
| #endif | ||
| #if defined(new) // COMPLIANT | ||
| #endif | ||
| // These cases don't compile in default tests, but may on other compilers | ||
| // #if defined 1 // NON-COMPLIANT | ||
| // #endif | ||
| // #if defined ( 1 ) // NON-COMPLIANT | ||
| // #endif | ||
| // #if defined + // NON-COMPLIANT | ||
| // #endif | ||
| // #if defined ( + ) // NON-COMPLIANT | ||
| // #endif | ||
| #define M1 defined | ||
| #define M2 1 + 2 + defined + 3 | ||
| #define M3 M2 | ||
| #define M4 1 + 2 + 3 | ||
| #define M5 M4 | ||
| #if M1 // NON-COMPLIANT | ||
| #endif | ||
| #if M2 // NON-COMPLIANT | ||
| #endif | ||
| #if M3 // NON-COMPLIANT | ||
| #endif | ||
| #if M4 // COMPLIANT | ||
| #endif | ||
| #if M5 // COMPLIANT | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| | invalid1.h:0:0:0:0 | invalid1.h | File does not have a well formatted include guard. | | ||
| | invalid2.h:0:0:0:0 | invalid2.h | File does not have a well formatted include guard. | | ||
| | invalid3.h:0:0:0:0 | invalid3.h | File does not have a well formatted include guard. | | ||
| | invalid4.h:0:0:0:0 | invalid4.h | File does not have a well formatted include guard. | | ||
| | invalid5_file2.h:0:0:0:0 | invalid5_file2.h | File does not have a well formatted include guard. | | ||
| | invalid6_b.h:0:0:0:0 | invalid6_b.h | File does not have a well formatted include guard. | | ||
| | invalid7.h:0:0:0:0 | invalid7.h | File does not have a well formatted include guard. | | ||
| | invalid8.h:0:0:0:0 | invalid8.h | File does not have a well formatted include guard. | | ||
| | invalid9.h:0:0:0:0 | invalid9.h | File does not have a well formatted include guard. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-19-2-1/NoValidIfdefGuardInHeader.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #ifndef MISSPELLED | ||
| #define MISPELED | ||
MichaelRFairhurst marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| void invalid1_f1(); | ||
| #endif | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.