-
Notifications
You must be signed in to change notification settings - Fork 77
Michaelrfairhurst/package deadcode 9 rule 0-2-3 #1043
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mbaluda
merged 12 commits into
main
from
michaelrfairhurst/package-deadcode-9-rule-0-2-3
Feb 26, 2026
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0080da8
Implement 0-2-3, unused type with limited visibility.
MichaelRFairhurst 7d2cd6f
Add rules.csv changes
MichaelRFairhurst 5b33319
Add package json
MichaelRFairhurst f2b1a9a
Fix ql query metadata, cpp format
MichaelRFairhurst 4e4c11d
Merge branch 'main' into michaelrfairhurst/package-deadcode-9-rule-0-2-3
mbaluda 5d8e899
Merge branch 'main' into michaelrfairhurst/package-deadcode-9-rule-0-2-3
mbaluda ad71fd3
Exclude templates, improve performance
MichaelRFairhurst 78b8af0
Reformat test.cpp for ci/cd version clang format
MichaelRFairhurst e7f6e7c
Commit .expected file changes
MichaelRFairhurst 275a96e
Copilot feedback
MichaelRFairhurst b566048
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/pac…
MichaelRFairhurst f66d898
Merge branch 'main' into michaelrfairhurst/package-deadcode-9-rule-0-2-3
mbaluda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| - `RULE-2-3`, `A0-1-6` - `UnusedTypeDeclarations.ql`: | ||
| - Type usage analysis has been improved to find more possible type usages, including: | ||
| - Previous behavior considered anonymous types in variable declarations to be considered used by the variable definition itself. This has been improved to require that a field of the anonymous type is accessed for the type to be considered used. | ||
| - Usages of a template type inside a specialization of that template are no longer considered usages of the template type. | ||
| - Hidden friend declarations are no longer considered usages of the class they are declaring friendship for. | ||
| - Improved exclusions generally, for cases such as nested types and functions within functions. These previously were a source of incorrectly identified type uses. | ||
| - Additional case added to detect `template <Enum = Enum::Value>` as a usage of `Enum`, without an explicit `tpl<Enum::Value>` usage. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import cpp | ||
|
|
||
| /** | ||
| * The last declaration in a class by location order. | ||
| * | ||
| * This may fail to capture certain cases such as hidden friends (see HiddenFriend.qll). | ||
| */ | ||
| class LastClassDeclaration extends Declaration { | ||
| Class cls; | ||
|
|
||
| pragma[nomagic] | ||
| LastClassDeclaration() { | ||
| cls.getADeclaration() = this and | ||
| getLocation().getEndLine() = getLastLineOfClassDeclaration(cls) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the line number of the last line of the declaration of `cls`. | ||
| * | ||
| * This is often more performant to use than `LastClassDeclaration.getLocation().getEndLine()`. | ||
| */ | ||
| int getLastLineOfClassDeclaration(Class cls) { | ||
| result = | ||
| max(int endLine | | ||
| endLine = pragma[only_bind_out](cls).getADeclaration().getLocation().getEndLine() | ||
| ) | ||
| } |
165 changes: 165 additions & 0 deletions
165
cpp/common/src/codingstandards/cpp/ast/HiddenFriend.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /** | ||
| * The C++ extractor does not support hidden friends, which are friend functions defined within a | ||
| * class, rather than declared: | ||
| * | ||
| * ```cpp | ||
| * struct A { | ||
| * friend void hidden_friend(A) {} // Definition: this is a hidden friend | ||
| * friend void not_hidden_friend(A); // Declaration: this is not a hidden friend | ||
| * }; | ||
| * ``` | ||
| * | ||
| * In the database, a `FriendDecl` is not created for the hidden friend. The hidden friend function | ||
| * is created as a `TopLevel` function with no enclosing element. However, we can identify it as a | ||
| * hidden friend by its location. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.ast.Class | ||
|
|
||
| /** | ||
| * A class that, by our best logic, appears to possibly be a hidden friend. | ||
| * | ||
| * Hidden friends are not directly represented in the database. Instances of this class have been | ||
| * found to have a location "within" the "body" of a class, and to satisfy other criteria that | ||
| * indicates it may be a hidden friend. | ||
| */ | ||
| class PossibleHiddenFriend extends HiddenFriendCandidate { | ||
| ClassCandidate cls; | ||
|
|
||
| PossibleHiddenFriend() { hidesFriend(cls, this) } | ||
|
|
||
| Class getFriendClass() { result = cls } | ||
| } | ||
|
|
||
| /** | ||
| * Begin by limiting the number of candidate functions to consider. | ||
| * | ||
| * Only inline top level functions can be hidden friends. | ||
| */ | ||
| private class HiddenFriendCandidate extends TopLevelFunction { | ||
| HiddenFriendCandidate() { this.isInline() } | ||
| } | ||
|
|
||
| /** | ||
| * Only consider files which contain hidden friend candidates. | ||
| */ | ||
| private class FileCandidate extends File { | ||
| FileCandidate() { exists(HiddenFriendCandidate c | c.getFile() = this) } | ||
| } | ||
|
|
||
| /** | ||
| * Only consider classes in candidate files, that include hidden friend candidates. | ||
| */ | ||
| private class ClassCandidate extends Class { | ||
| ClassCandidate() { getFile() instanceof FileCandidate } | ||
|
|
||
| /** | ||
| * Find the next declaration after this class that shares an enclosing element. | ||
| * | ||
| * This may be the next declaration after this class, or `getNextOrphanedDeclaration` may find the | ||
| * true next declaration after this class. These are split for performance reasons. | ||
| */ | ||
| Declaration getNextSiblingDeclaration() { | ||
| result = | ||
| min(Declaration decl | | ||
| decl.getEnclosingElement() = this.getEnclosingElement() and | ||
| pragma[only_bind_out](decl.getFile()) = pragma[only_bind_out](this.getFile()) and | ||
| decl.getLocation().getStartLine() > getLastLineOfClassDeclaration(this) | ||
| | | ||
| decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Get the next declaration after this class that does not have an enclosing element. | ||
| * | ||
| * This may be the next declaration after this class, or `getNextSiblingDeclaration` may find the | ||
| * true next declaration after this class. These are split for performance reasons. | ||
| * | ||
| * Note that `OrphanedDeclaration` excludes hidden friend candidates, so this will find the next | ||
| * orphan that is definitely not a hidden friend. | ||
| */ | ||
| Declaration getNextOrphanedDeclaration() { | ||
| result = | ||
| min(OrphanedDeclaration decl, int startLine, int startColumn | // Location locDecl | // Location locLast, Location locDecl | | ||
| orphanHasLocation(decl, this.getFile(), startLine, startColumn) and | ||
| startLine > getLastLineOfClassDeclaration(this) | ||
| | | ||
| decl order by startLine, startColumn | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Get the first declaration definitely after this class, and not a hidden friend declaration, to | ||
| * determine the "end" location of this class. | ||
| */ | ||
| Declaration getFirstNonClassDeclaration() { | ||
| result = | ||
| min(Declaration decl | | ||
| decl = getNextSiblingDeclaration() or decl = getNextOrphanedDeclaration() | ||
| | | ||
| decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper predicate to improve join performance. | ||
| */ | ||
| pragma[nomagic] | ||
| private predicate orphanHasLocation( | ||
| OrphanedDeclaration orphan, FileCandidate file, int endLine, int endColumn | ||
| ) { | ||
| orphan.getFile() = file and | ||
| orphan.getLocation().getEndLine() = endLine and | ||
| orphan.getLocation().getEndColumn() = endColumn | ||
| } | ||
|
|
||
| /** | ||
| * Orphaned declarations to be found by `getNextOrphanedDeclaration`. | ||
| * | ||
| * These are declarations with no enclosing element. Note that we exclude hidden friend candidates, | ||
| * as this class is used to find the declarations that are definitely not part of some class. This | ||
| * is done so we can detect if hidden friends may be within that class definition. Therefore we must | ||
| * exclude hidden friend candidates, even though those are also orphaned. | ||
| */ | ||
| private class OrphanedDeclaration extends Declaration { | ||
| OrphanedDeclaration() { | ||
| not exists(getEnclosingElement()) and | ||
| not this instanceof HiddenFriendCandidate and | ||
| getFile() instanceof FileCandidate and | ||
| not isFromTemplateInstantiation(_) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper predicate to improve join performance. | ||
| */ | ||
| pragma[nomagic] | ||
| private predicate classCandidateHasFile(ClassCandidate c, FileCandidate f) { c.getFile() = f } | ||
|
|
||
| /** | ||
| * Helper predicate to improve join performance. | ||
| */ | ||
| pragma[nomagic] | ||
| private predicate hiddenFriendCandidateHasFile(HiddenFriendCandidate h, FileCandidate f) { | ||
| h.getFile() = f | ||
| } | ||
|
|
||
| /** | ||
| * Find the class locations that have declarations that could be hidden friend declarations, by | ||
| * comparing the locations of the candidate hidden friend functions to the location of the first | ||
| * declaration that clearly is outside that class. | ||
| */ | ||
| pragma[nomagic] | ||
| private predicate hidesFriend(ClassCandidate c, HiddenFriendCandidate f) { | ||
| exists(FileCandidate file, Location cloc, Location floc | | ||
| classCandidateHasFile(c, file) and | ||
| hiddenFriendCandidateHasFile(f, file) and | ||
| cloc = c.getLocation() and | ||
| floc = f.getLocation() and | ||
| cloc.getEndLine() < floc.getStartLine() and | ||
| floc.getEndLine() < c.getFirstNonClassDeclaration().getLocation().getStartLine() | ||
| ) | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode9.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
| import cpp | ||
| import RuleMetadata | ||
| import codingstandards.cpp.exclusions.RuleMetadata | ||
|
|
||
| newtype DeadCode9Query = TUnusedTypeWithLimitedVisibilityQuery() | ||
|
|
||
| predicate isDeadCode9QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `unusedTypeWithLimitedVisibility` query | ||
| DeadCode9Package::unusedTypeWithLimitedVisibilityQuery() and | ||
| queryId = | ||
| // `@id` for the `unusedTypeWithLimitedVisibility` query | ||
| "cpp/misra/unused-type-with-limited-visibility" and | ||
| ruleId = "RULE-0-2-3" and | ||
| category = "advisory" | ||
| } | ||
|
|
||
| module DeadCode9Package { | ||
| Query unusedTypeWithLimitedVisibilityQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `unusedTypeWithLimitedVisibility` query | ||
| TQueryCPP(TDeadCode9PackageQuery(TUnusedTypeWithLimitedVisibilityQuery())) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.