-
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 3 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,6 @@ | ||
| - `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. |
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,21 @@ | ||
| 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() { | ||
| this = | ||
| max(Declaration decl, Location l | | ||
| decl = cls.getADeclaration() and | ||
| l = decl.getLocation() | ||
| | | ||
| decl order by l.getEndLine(), l.getEndColumn() | ||
| ) | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
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,124 @@ | ||
| /** | ||
| * 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 | ||
|
|
||
| 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 } | ||
|
|
||
| Declaration getNextSiblingDeclaration() { | ||
| exists(LastClassDeclaration last | | ||
| last.getDeclaringType() = this and | ||
| 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() > last.getLocation().getEndLine() | ||
| | | ||
| decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| Declaration getNextOrphanedDeclaration() { | ||
| exists(LastClassDeclaration last | | ||
| last.getDeclaringType() = this and | ||
| result = | ||
| min(OrphanedDeclaration decl, Location locLast, Location locDecl | | ||
| orphanHasFile(decl, this.getFile()) and | ||
| locDecl = decl.getLocation() and | ||
| locLast = last.getLocation() and | ||
| locLast.getStartLine() < locDecl.getEndLine() | ||
| | | ||
| decl order by locDecl.getStartLine(), locDecl.getStartColumn() | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| Declaration getFirstNonClassDeclaration() { | ||
| exists(LastClassDeclaration last | | ||
| last.getDeclaringType() = this and | ||
| result = | ||
| min(Declaration decl | | ||
| decl = getNextSiblingDeclaration() or decl = getNextOrphanedDeclaration() | ||
| | | ||
| decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| pragma[nomagic] | ||
| private predicate orphanHasFile(OrphanedDeclaration orphan, FileCandidate file) { | ||
| orphan.getFile() = file | ||
| } | ||
|
|
||
| private class OrphanedDeclaration extends Declaration { | ||
| OrphanedDeclaration() { | ||
| not exists(getEnclosingElement()) and | ||
| not this instanceof HiddenFriendCandidate and | ||
| getFile() instanceof FileCandidate and | ||
| not isFromTemplateInstantiation(_) | ||
| } | ||
| } | ||
|
|
||
| pragma[nomagic] | ||
| private predicate classCandidateHasFile(ClassCandidate c, FileCandidate f) { c.getFile() = f } | ||
|
|
||
| pragma[nomagic] | ||
| private predicate hiddenFriendCandidateHasFile(HiddenFriendCandidate h, FileCandidate f) { | ||
| h.getFile() = f | ||
| } | ||
|
|
||
| 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
22 changes: 22 additions & 0 deletions
22
cpp/misra/src/rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.ql
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,22 @@ | ||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.types.Uses | ||
|
|
||
| predicate hasLimitedVisibility(UserType type) { | ||
| type.isLocal() or | ||
| type.getNamespace().isAnonymous() | ||
| } | ||
|
|
||
| predicate hasMaybeUnusedAttribute(UserType type) { | ||
| exists(Attribute a | a = type.getAnAttribute() and a.getName() = "maybe_unused") | ||
| } | ||
|
|
||
| from UserType type | ||
| where | ||
| not isExcluded(type, DeadCode9Package::unusedTypeWithLimitedVisibilityQuery()) and | ||
| hasLimitedVisibility(type) and | ||
| not exists(getATypeUse(type)) and | ||
| not hasMaybeUnusedAttribute(type) and | ||
| not type instanceof TypeTemplateParameter and | ||
| not type instanceof ClassTemplateSpecialization | ||
| select type, "Type '" + type.getName() + "' with limited visibility is not used." |
11 changes: 11 additions & 0 deletions
11
cpp/misra/test/rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.expected
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,11 @@ | ||
| | test.cpp:5:9:5:10 | T1 | Type 'T1' with limited visibility is not used. | | ||
| | test.cpp:22:8:22:9 | A1 | Type 'A1' with limited visibility is not used. | | ||
| | test.cpp:26:8:26:9 | A2 | Type 'A2' with limited visibility is not used. | | ||
| | test.cpp:62:28:62:29 | C1<<unnamed>> | Type 'C1<<unnamed>>' with limited visibility is not used. | | ||
| | test.cpp:79:14:79:14 | (unnamed class/struct/union) | Type '(unnamed class/struct/union)' with limited visibility is not used. | | ||
| | test.cpp:105:12:105:13 | E2 | Type 'E2' with limited visibility is not used. | | ||
| | test.cpp:115:9:115:10 | T2 | Type 'T2' with limited visibility is not used. | | ||
| | test.cpp:126:10:126:11 | S2 | Type 'S2' with limited visibility is not used. | | ||
| | test.cpp:133:8:133:9 | A3 | Type 'A3' with limited visibility is not used. | | ||
| | test.cpp:145:8:145:9 | A6 | Type 'A6' with limited visibility is not used. | | ||
| | test.cpp:152:29:152:42 | AliasTemplate1 | Type 'AliasTemplate1' with limited visibility is not used. | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.qlref
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 @@ | ||
| rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.ql |
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.