Skip to content

Initial implementation of rule 4-1-1, must be standard C++17#1106

Open
MichaelRFairhurst wants to merge 1 commit intomainfrom
michaelrfairhust/toolchain2-be-iso-cpp17-compliant
Open

Initial implementation of rule 4-1-1, must be standard C++17#1106
MichaelRFairhurst wants to merge 1 commit intomainfrom
michaelrfairhust/toolchain2-be-iso-cpp17-compliant

Conversation

@MichaelRFairhurst
Copy link
Copy Markdown
Collaborator

Description

please enter the description of your change here

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-4-1-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 10, 2026 06:35
Copy link
Copy Markdown
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

Adds the initial CodeQL implementation for MISRA C++:2023 RULE-4-1-1 (“must be standard C++17”) by modeling several common compiler-language extensions and wiring the rule into the toolchain package and test suite.

Changes:

  • Introduces a new rule query (CompilerLanguageExtensionsUsed) and a shared library model of C++ compiler extensions.
  • Adds a dedicated unit test directory with a .qlref and .expected results for RULE-4-1-1.
  • Wires the rule into Toolchain2 packaging/metadata and updates standard-library test stubs needed by the test.
Show a summary per file
File Description
rule_packages/cpp/Toolchain2.json Adds MISRA-C++-2023 RULE-4-1-1 metadata and query descriptor in Toolchain2 package.
cpp/misra/src/rules/RULE-4-1-1/CompilerLanguageExtensionsUsed.ql New rule query selecting detected compiler extensions and reporting messages.
cpp/common/src/codingstandards/cpp/Extensions.qll Adds the core modeling of C++ compiler extensions and associated alert messages.
cpp/misra/test/rules/RULE-4-1-1/test.cpp New test cases covering attributes, builtins, statement expressions, pragmas, etc.
cpp/misra/test/rules/RULE-4-1-1/CompilerLanguageExtensionsUsed.qlref Test reference to the production query.
cpp/misra/test/rules/RULE-4-1-1/CompilerLanguageExtensionsUsed.expected Expected results for the test query execution.
cpp/common/test/includes/standard-library/type_traits.h Extends the type-traits stub to support test compilation.
cpp/common/src/codingstandards/cpp/exclusions/cpp/Toolchain2.qll Autogenerated Toolchain2 query metadata wrapper for exclusions.
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Registers Toolchain2 metadata with the exclusions framework.

Copilot's findings

Comments suppressed due to low confidence (2)

cpp/common/src/codingstandards/cpp/Extensions.qll:126

  • Some message strings exceed the repo's 100-character line limit for CodeQL source/test files (for example, this line). Please wrap these strings across multiple lines to keep each source line ≤ 100 characters.
  override string getMessage() {
    result =
      "Extended floating-point types are a compiler extension and are not portable to other compilers."
  }

cpp/common/src/codingstandards/cpp/Extensions.qll:203

  • CPPPreprocessorDirectiveExtension is a new public class (not marked private) but it does not have a QLDoc comment. Add QLDoc (or mark it private / document it as INTERNAL) to satisfy the project's documentation requirements for public declarations.
class CPPPreprocessorDirectiveExtension extends CPPCompilerExtension, PreprocessorDirective {
  string kind;

  CPPPreprocessorDirectiveExtension() {
    this instanceof PreprocessorPragma and kind = "#pragma " + getHead()
    or
  • Files reviewed: 9/9 changed files
  • Comments generated: 8

Comment on lines +23 to +25
from CPPCompilerExtension e
where not isExcluded(e, Toolchain2Package::compilerLanguageExtensionsUsedQuery())
select MacroUnwrapper<CPPCompilerExtension>::unwrapElement(e), e.getMessage()
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The query is currently reporting a result whose primary location is inside the test standard-library stub (includes/standard-library/stdarg.h), which leads to machine-specific absolute file:///... paths and makes the test not correspond to a NON_COMPLIANT marker in test.cpp. Consider adjusting the reporting element (or adding a filter) so results are reported in user/test source files and not in includes/standard-library stubs (for example, mirror the standard-library exclusion pattern used elsewhere in cpp/common/src/codingstandards/cpp/Macro.qll).

Suggested change
from CPPCompilerExtension e
where not isExcluded(e, Toolchain2Package::compilerLanguageExtensionsUsedQuery())
select MacroUnwrapper<CPPCompilerExtension>::unwrapElement(e), e.getMessage()
from CPPCompilerExtension e, Element report
where
not isExcluded(e, Toolchain2Package::compilerLanguageExtensionsUsedQuery()) and
(
report = MacroUnwrapper<CPPCompilerExtension>::unwrapElement(e) and
not isExcluded(report, Toolchain2Package::compilerLanguageExtensionsUsedQuery())
or
report = e and
isExcluded(
MacroUnwrapper<CPPCompilerExtension>::unwrapElement(e),
Toolchain2Package::compilerLanguageExtensionsUsedQuery()
)
)
select report, e.getMessage()

Copilot uses AI. Check for mistakes.
#ifdef __cplusplus // COMPLIANT
#endif

#pragma once
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

#pragma once produces a result in the .expected file, but this line in test.cpp is not marked // NON_COMPLIANT. Update the test annotation (or adjust the expected results/query) so every expected alert corresponds to a NON_COMPLIANT case in the test source.

Suggested change
#pragma once
#pragma once // NON_COMPLIANT

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,25 @@
| file:///Users/michaelrfairhurst/projects/codeql-coding-standards/cpp/common/test/includes/standard-library/stdarg.h:5:1:5:47 | #define va_start(v,l) __builtin_va_start(v, l) | Use of built-in operation '__builtin_va_start' is a compiler extension. |
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

This expected result refers to a machine-local absolute file:///... path under cpp/common/test/includes/standard-library/.... Expected files should not contain developer-specific absolute paths, and results should ideally be reported against the test source (test.cpp) rather than the standard-library stubs; update the query/reporting element so this row becomes stable (or remove it if the rule should not flag standard va_start).

Suggested change
| file:///Users/michaelrfairhurst/projects/codeql-coding-standards/cpp/common/test/includes/standard-library/stdarg.h:5:1:5:47 | #define va_start(v,l) __builtin_va_start(v, l) | Use of built-in operation '__builtin_va_start' is a compiler extension. |

Copilot uses AI. Check for mistakes.
#pragma GCC diagnostic push // NON_COMPLIANT
#warning "This is a warning" // NON_COMPLIANT
// clang-format off
# warning "preceeding spaces is common" // NON_COMPLIANT
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

Typo in the warning string: "preceeding" should be "preceding".

Suggested change
# warning "preceeding spaces is common" // NON_COMPLIANT
# warning "preceding spaces is common" // NON_COMPLIANT

Copilot uses AI. Check for mistakes.
Comment on lines +93 to +96
override string getMessage() {
result =
"Ternaries with omitted middle operands are a compiler extension and are not portable to other compilers."
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

Some message strings exceed the repo's 100-character line limit for CodeQL source/test files (for example, this line). Please wrap these strings across multiple lines to keep each source line ≤ 100 characters.

This issue also appears on line 123 of the same file.

Copilot uses AI. Check for mistakes.
Comment on lines +34 to +45
class CppNamespacedStdAttributeExtension extends CPPCompilerExtension, StdAttribute {
CppNamespacedStdAttributeExtension() { exists(this.getNamespace()) and not getNamespace() = "" }

override string getMessage() {
result =
"Use of attribute '" + getName() + "' in namespace '" + getNamespace() +
"' is a compiler extension and is not portable to other compilers."
}
}

class CppUnrecognizedAttributeExtension extends CPPCompilerExtension, StdAttribute {
CppUnrecognizedAttributeExtension() {
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

CppNamespacedStdAttributeExtension / CppUnrecognizedAttributeExtension use a different Cpp... prefix than the rest of the newly introduced CPP... extension classes in this module. Align the naming (for example, consistently use CPP...) so the API is easier to discover and search.

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +50
class CppUnrecognizedAttributeExtension extends CPPCompilerExtension, StdAttribute {
CppUnrecognizedAttributeExtension() {
not this instanceof CppNamespacedStdAttributeExtension and
not getName() in [
"maybe_unused", "nodiscard", "noreturn", "deprecated", "carries_dependency", "fallthrough"
]
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

These newly introduced classes are public (not marked private) but are missing QLDoc comments. The project guidelines require public declarations to be documented; add /** ... */ docs (or mark them private if they are intended for internal use only).

This issue also appears on line 198 of the same file.

Copilot uses AI. Check for mistakes.
Comment on lines +70 to +77
template <typename T> bool is_abstract_v = is_abstract<T>::value;

template <typename T, typename U> struct is_same {
const static bool value = false;
constexpr operator bool() { return value; }
};

template <typename T, typename U> bool is_same_v = is_same<T, U>::value;
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The new _v variable templates are defined as non-constexpr non-inline variables, but this same header uses inline constexpr for other _v templates (for example is_literal_type_v). Update is_abstract_v/is_same_v to follow the same pattern to avoid ODR/linkage issues and keep the stub consistent.

Suggested change
template <typename T> bool is_abstract_v = is_abstract<T>::value;
template <typename T, typename U> struct is_same {
const static bool value = false;
constexpr operator bool() { return value; }
};
template <typename T, typename U> bool is_same_v = is_same<T, U>::value;
template <typename T>
inline constexpr bool is_abstract_v = is_abstract<T>::value;
template <typename T, typename U> struct is_same {
const static bool value = false;
constexpr operator bool() { return value; }
};
template <typename T, typename U>
inline constexpr bool is_same_v = is_same<T, U>::value;

Copilot uses AI. Check for mistakes.
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.

2 participants