Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and 19-2-1#893
Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and 19-2-1#893MichaelRFairhurst wants to merge 14 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This pull request implements new CodeQL queries to enforce MISRA-C++23 preprocessor rules (RULE-19-0-4, RULE-19-1-1, and RULE-19-2-1), and it adds test cases to verify improper include guards and misuse of the preprocessor.
- Added test files with intentionally malformed include guards and misuse of the "defined" operator.
- Created new QL query files to detect violations of MISRA preprocessor rules.
- Updated common utilities and exclusion lists to support preprocessor-related queries.
Reviewed Changes
Copilot reviewed 34 out of 34 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cpp/misra/test/rules/RULE-19-2-1/invalid2.h | Test file with an include guard using "#ifdef" instead of "#ifndef". |
| cpp/misra/test/rules/RULE-19-2-1/invalid1.h | Test file with a spelling mismatch in the include guard macros. |
| Other files | New queries and supporting files for enforcing MISRA preprocessor rules. |
lcartey
left a comment
There was a problem hiding this comment.
Looks good - mostly suggestion on 19.2.1, where we have some prior art.
| from File included | ||
| where | ||
| not isExcluded(included, PreprocessorPackage::noValidIfdefGuardInHeaderQuery()) and | ||
| included = any(Compilation c).getAFileCompiled().getAnIncludedFile+() and |
There was a problem hiding this comment.
In other Coding Standard queries we use the HeaderFile class to identify header files, based on the extension. The advantage of this approach is that we will report contraventions in files in header libraries that are not included within the header library itself. Given that this can be an important case, and for consistency reasons, I would suggest we switch definitions.
…lement-package-preprocessor Update qtil to 0.0.2, which is newly published to support newer versions of codeql/util.
|
Note: Updated to use qtil. |
| { | ||
| "description": "Precautions shall be taken in order to prevent the contents of a header file being included more than once.", | ||
| "kind": "problem", | ||
| "name": "Precautions shall be taken in order to prevent the contents of a header file being included more", |
There was a problem hiding this comment.
The query name on line 69 appears to be truncated. It reads "Precautions shall be taken in order to prevent the contents of a header file being included more" but should likely end with "...more than once" to match the title on line 94 and the full rule description.
| #ifndef MISSPELLED | ||
| #define MISPELED | ||
|
|
||
| void invalid1_f1(); | ||
|
|
||
| #endif No newline at end of file |
There was a problem hiding this comment.
The test file invalid1.h is missing a NON_COMPLIANT annotation. The file contains an invalid include guard (MISSPELLED vs MISPELED) and is expected to produce an alert in NoValidIfdefGuardInHeader.expected line 1, but there is no NON_COMPLIANT comment in the test file to mark this case.
| #ifdef INVALID2_H | ||
| #define INVALID2_H | ||
|
|
||
| void invalid2_f1(); | ||
| // invalid: uses ifdef, not ifndef | ||
|
|
||
| #endif No newline at end of file |
There was a problem hiding this comment.
The test file invalid2.h is missing a NON_COMPLIANT annotation. The file uses ifdef instead of ifndef for the include guard and is expected to produce an alert in NoValidIfdefGuardInHeader.expected line 2, but there is no NON_COMPLIANT comment in the test file to mark this case. Note that line 5 contains only an explanatory comment, not a NON_COMPLIANT annotation.
Description
Also includes two files in
cpp/utilwhich I intend to port toqtilpackage (CondensedListandPair).Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-19-0-4,RULE-19-1-1,RULE-19-2-1Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.