Skip to content

Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and 19-2-1#893

Open
MichaelRFairhurst wants to merge 14 commits intomainfrom
michaelrfairhurst/implement-package-preprocessor
Open

Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and 19-2-1#893
MichaelRFairhurst wants to merge 14 commits intomainfrom
michaelrfairhurst/implement-package-preprocessor

Conversation

@MichaelRFairhurst
Copy link
Collaborator

Description

Also includes two files in cpp/util which I intend to port to qtil package (CondensedList and Pair).

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • RULE-19-0-4, RULE-19-1-1, RULE-19-2-1
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
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.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

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

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Copilot AI review requested due to automatic review settings April 29, 2025 23:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@lcartey lcartey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MichaelRFairhurst
Copy link
Collaborator Author

Note: Updated to use qtil.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 39 out of 39 changed files in this pull request and generated 3 comments.

{
"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",
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +6
#ifndef MISSPELLED
#define MISPELED

void invalid1_f1();

#endif No newline at end of file
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +1 to +7
#ifdef INVALID2_H
#define INVALID2_H

void invalid2_f1();
// invalid: uses ifdef, not ifndef

#endif No newline at end of file
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot generated this review using guidance from repository custom instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants