First pass implementing RULE-0-2-1, unused variables.#1037
First pass implementing RULE-0-2-1, unused variables.#1037MichaelRFairhurst wants to merge 14 commits intomainfrom
Conversation
Implementation relies on shared code with autosar that was previously declared in the ql files. This behavior has been moved into UnusedObjects.qll. The previous code seemed like a very disorganized set of exceptional cases, which was going to be worse with the new code. I made a minimal attempt to better organize it by organizing it into modules containing individual filter passes. Filtering out `.getAnAccess()` was always the last step. From a performance perspective, this is a bad order. We start with all variables, and for each one we join it against some set of rare exceptional cases, like functions with assembly, that only minimally reduce the set of variables we want to query. Changed to use a better order, where we begin with the set of variables that have no accesses, which should be a very small set relatively speaking, and then perform the odds-and-ends filters on top of that set. Small performance improvements have been measured across the queries.
|
/test-performance |
|
🏁 Beep Boop! Performance testing for this PR has been initiated. Please check back later for results. Note that the query package generation step must complete before testing will start so it might be a minute. |
There was a problem hiding this comment.
Pull request overview
This PR implements RULE-0-2-1 for MISRA C++ 2023, which requires variables with limited visibility to be used at least once. The implementation reorganizes unused variable analysis into a modular three-pass structure (FirstPassUnused, SecondPassUnused, ThirdPassUnused) shared across MISRA and AUTOSAR standards. The refactoring improves performance by filtering out variables without accesses first (using .getAnAccess()) before applying more expensive filtering criteria.
Changes:
- Adds new MISRA C++ rule RULE-0-2-1 for detecting unused variables with limited visibility
- Refactors unused variable analysis in UnusedVariables.qll into three modular passes for reuse across standards
- Optimizes query performance by checking for variable accesses first before applying other filters
- Updates existing AUTOSAR M0-1-3 queries to use the new shared module structure
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| rules.csv | Updates RULE-0-2-1 entry to mark it as implemented with package DeadCode7 |
| rule_packages/cpp/DeadCode7.json | Adds metadata for new RULE-0-2-1 query |
| cpp/misra/test/rules/RULE-0-2-1/.cpp/.hpp | Comprehensive test cases covering local, global, namespace, and member variables |
| cpp/misra/test/rules/RULE-0-2-1/UnusedLimitedVisibilityVariable.qlref | Reference to the main query file |
| cpp/misra/test/rules/RULE-0-2-1/UnusedLimitedVisibilityVariable.expected | Expected test results |
| cpp/misra/src/rules/RULE-0-2-1/UnusedLimitedVisibilityVariable.ql | Main query implementation for RULE-0-2-1 |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Adds DeadCode7 package metadata support |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode7.qll | Auto-generated exclusion metadata for DeadCode7 package |
| cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll | Refactors unused variable logic into three modular passes with performance improvements |
| cpp/common/src/codingstandards/cpp/deadcode/UnusedObjects.qll | Updates to use new FirstPassUnused module |
| cpp/autosar/src/rules/M0-1-3/*.ql | Refactors AUTOSAR queries to use new ThirdPassUnused module |
| change_notes/2026-02-12-unused-variable-reorganization.md | Documents the reorganization and performance improvements |
Comments suppressed due to low confidence (1)
cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll:294
- Spelling error: 'inintialize' should be 'initialize'.
// Exclude members whose value is compile time and is potentially used to inintialize a template
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll
Outdated
Show resolved
Hide resolved
cpp/misra/test/rules/RULE-0-2-1/UnusedLimitedVisibilityVariable.expected
Outdated
Show resolved
Hide resolved
|
🏁 Beep Boop! One or things failed during performance testing. Please check the release engineering repo for details. |
mbaluda
left a comment
There was a problem hiding this comment.
Very complex query... LGTM!
I marked a few inconsistencies in the tests annotations.
cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll
Outdated
Show resolved
Hide resolved
|
|
||
| class HeaderClass { | ||
| const int m1 = 9; // NON_COMPLIANT -- not namespace scope | ||
| static const int m2 = 30234; // NON_COMPLIANT -- not namespace scope |
There was a problem hiding this comment.
| static const int m2 = 30234; // NON_COMPLIANT -- not namespace scope | |
| static const int m2 = 30234; // NON_COMPLIANT[FALSE_NEGATIVE] -- not namespace scope |
There was a problem hiding this comment.
Good catch!
Had to reread the spec here. By spec, "static variables at namespace scope" are internal linkage -- but m2 is not namespace scope. "static data members" inherit the class linkage. In this case, "HeaderClass" has external linkage since its in the global namespace. This should have been COMPLIANT, because m2 has external linkage, and the rule only applies to variables with internal linkage or no linkage.
I also added a new test case, a static data member of a class in an anonymous namespace. Namespace has internal linkage -> class has internal linkage -> static data member has internal linkage. This new static member is unused by basic rule definition, since it has internal linkage, but still fits some of the criteria of Exception 2: it is constant, and in a header file. However, exception 3 only applies to variables with namespace scope, and this static data member has class scope, so the exception doesn't apply and it is NON_COMPLIANT.
This is a lot but I think that's what my brain was going for when I added this test case originally...?
| int pad : 1; // NON_COMPLIANT - padding bytes, so not used, but is not | ||
| // unnamed | ||
| int sm2 : 6; // NON_COMPLIANT - unused |
There was a problem hiding this comment.
| int pad : 1; // NON_COMPLIANT - padding bytes, so not used, but is not | |
| // unnamed | |
| int sm2 : 6; // NON_COMPLIANT - unused | |
| int pad : 1; // NON_COMPLIANT[FALSE_NEGATIVE] - padding bytes, so not used, but is not | |
| // unnamed | |
| int sm2 : 6; // NON_COMPLIANT[FALSE_NEGATIVE] - unused |
There was a problem hiding this comment.
Oof! Another good catch!
I completely missed this, but looks like this is a "regression" (of sorts) since I used .getQualifiedName() and M0-1-3 uses getName().
Changing to getName() on UnusedMemberVariable in 0-2-1 to match fixed these FNs!
Co-authored-by: Mauro Baluda <mbaluda@github.com>
394b2ff to
a4a9186
Compare
|
Somehow my merge ment massively wrong. Reset the branch to a4a9186, known safe last state. |
Implementation relies on shared code with autosar that was previously declared in the ql files. This behavior has been moved into UnusedObjects.qll.
The previous code seemed like a very disorganized set of exceptional cases, which was going to be worse with the new code. I made a minimal attempt to better organize it by organizing it into modules containing individual filter passes.
Filtering out
.getAnAccess()was always the last step. From a performance perspective, this is a bad order. We start with all variables, and for each one we join it against some set of rare exceptional cases, like functions with assembly, that only minimally reduce the set of variables we want to query. Changed to use a better order, where we begin with the set of variables that have no accesses, which should be a very small set relatively speaking, and then perform the odds-and-ends filters on top of that set. Small performance improvements have been measured across the queries.Description
please enter the description of your change here
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-0-2-1M0-1-3RULE-2-8Release 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.