-
Notifications
You must be signed in to change notification settings - Fork 76
Implement Rule 0.2.4, unused functions with limited visibility. #1044
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
Open
MichaelRFairhurst
wants to merge
6
commits into
main
Choose a base branch
from
michaelrfairhurst/implement-deadcode-10-rule-0-2-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1417b0
Implement Rule 0.2.4, unused functions with limited visibility.
MichaelRFairhurst b8d858a
Remove qlref from 10-1-3 now that its shared.
MichaelRFairhurst 5b83957
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 0dfd029
Fix RuleMetadata order (alphabetical, not numeric sort)
MichaelRFairhurst ad4da3b
Delete accidentally committed duplicate file
MichaelRFairhurst b6ea3a9
Merge branch 'main' into michaelrfairhurst/implement-deadcode-10-rule…
MichaelRFairhurst 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
2 changes: 2 additions & 0 deletions
2
change_notes/2026-02-18-unused-member-functions-with-internal-linkage.md
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,2 @@ | ||
| - `A0-1-3` - `UnusedLocalFunction.ql`: | ||
| - Query now reports unused public members of classes in anonymous namespaces, which have internal linkage. |
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 was deleted.
Oops, something went wrong.
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 @@ | ||
| cpp/common/test/rules/unusedlocalfunction/UnusedLocalFunction.ql |
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode10.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 DeadCode10Query = TUnusedLimitedVisibilityFunctionQuery() | ||
|
|
||
| predicate isDeadCode10QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `unusedLimitedVisibilityFunction` query | ||
| DeadCode10Package::unusedLimitedVisibilityFunctionQuery() and | ||
| queryId = | ||
| // `@id` for the `unusedLimitedVisibilityFunction` query | ||
| "cpp/misra/unused-limited-visibility-function" and | ||
| ruleId = "RULE-0-2-4" and | ||
| category = "advisory" | ||
| } | ||
|
|
||
| module DeadCode10Package { | ||
| Query unusedLimitedVisibilityFunctionQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `unusedLimitedVisibilityFunction` query | ||
| TQueryCPP(TDeadCode10PackageQuery(TUnusedLimitedVisibilityFunctionQuery())) | ||
| } | ||
| } |
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
115 changes: 115 additions & 0 deletions
115
cpp/common/src/codingstandards/cpp/rules/unusedlocalfunction/UnusedLocalFunction.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,115 @@ | ||
| /** | ||
| * Provides a configurable module UnusedLocalFunction with a `problems` predicate | ||
| * for the following issue: | ||
| * Unused functions may indicate a coding error or require maintenance; functions that | ||
| * are unused with certain visibility have no effect on the program and should be | ||
| * removed. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.DynamicCallGraph | ||
| import codingstandards.cpp.deadcode.UnusedFunctions | ||
|
|
||
| /** | ||
| * Checks if an overloaded function of | ||
| * the function passed in the arguments, is called. | ||
| */ | ||
| predicate overloadedFunctionIsCalled(Function unusedFunction) { | ||
| exists(Function f | f = unusedFunction.getAnOverload() and f = getTarget(_)) | ||
| } | ||
|
|
||
| /** Checks if a Function's address was taken. */ | ||
| predicate addressBeenTaken(Function unusedFunction) { | ||
| exists(FunctionAccess fa | fa.getTarget() = unusedFunction) | ||
| } | ||
|
|
||
| /** A `Function` nested in an anonymous namespace. */ | ||
| class AnonymousNamespaceFunction extends Function { | ||
| AnonymousNamespaceFunction() { getNamespace().getParentNamespace*().isAnonymous() } | ||
| } | ||
|
|
||
| /** | ||
| * A function which is "local" to a particular scope or translation unit. | ||
| */ | ||
| class LocalFunction extends UnusedFunctions::UsableFunction { | ||
| string localFunctionType; | ||
|
|
||
| LocalFunction() { | ||
| this.(MemberFunction).isPrivate() and | ||
| localFunctionType = "Private member" | ||
| or | ||
| // A function in an anonymous namespace (which is deduced to have internal linkage) | ||
| this instanceof AnonymousNamespaceFunction and | ||
| not this instanceof MemberFunction and | ||
| localFunctionType = "Anonymous namespace" | ||
| or | ||
| // Class members in anonymous namespaces also have internal linkage. | ||
| this instanceof AnonymousNamespaceFunction and | ||
| this instanceof MemberFunction and | ||
| localFunctionType = "Anonymous namespace class member" | ||
| or | ||
| // Static functions with internal linkage | ||
| this.isStatic() and | ||
| // Member functions never have internal linkage | ||
| not this instanceof MemberFunction and | ||
| // Functions in anonymous namespaces automatically have the "static" specifier added by the | ||
| // extractor. We therefore excluded them from this case, and instead report them in the | ||
| // anonymous namespace, as we don't know whether the "static" specifier was explicitly | ||
| // provided by the user. | ||
| not this instanceof AnonymousNamespaceFunction and | ||
| localFunctionType = "Static" | ||
| } | ||
|
|
||
| /** Gets the type of local function. */ | ||
| string getLocalFunctionType() { result = localFunctionType } | ||
| } | ||
|
|
||
| signature module UnusedLocalFunctionConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| module UnusedLocalFunction<UnusedLocalFunctionConfigSig Config> { | ||
| query predicate problems(LocalFunction unusedLocalFunction, string message) { | ||
| exists(string name | | ||
| not isExcluded(unusedLocalFunction, Config::getQuery()) and | ||
| // No static or dynamic call target for this function | ||
| not unusedLocalFunction = getTarget(_) and | ||
| // If this is a TemplateFunction or an instantiation of a template, then only report it as unused | ||
| // if all other instantiations of the template are unused | ||
| not exists( | ||
| Function functionFromUninstantiatedTemplate, Function functionFromInstantiatedTemplate | ||
| | | ||
| // `unusedLocalFunction` is a template instantiation from `functionFromUninstantiatedTemplate` | ||
| unusedLocalFunction.isConstructedFrom(functionFromUninstantiatedTemplate) | ||
| or | ||
| // `unusedLocalFunction` is from an uninstantiated template | ||
| unusedLocalFunction = functionFromUninstantiatedTemplate | ||
| | | ||
| // There exists an instantiation which is called | ||
| functionFromInstantiatedTemplate.isConstructedFrom(functionFromUninstantiatedTemplate) and | ||
| functionFromInstantiatedTemplate = getTarget(_) | ||
| ) and | ||
| // A function is defined as "used" if any one of the following holds true: | ||
| // - It's an explicitly deleted functions e.g. =delete | ||
| // - It's annotated as "[[maybe_unused]]" | ||
| // - It's part of an overloaded set and any one of the overloaded instance | ||
| // is called. | ||
| // - It's an operand of an expression in an unevaluated context. | ||
| not unusedLocalFunction.isDeleted() and | ||
| not unusedLocalFunction.getAnAttribute().getName() = "maybe_unused" and | ||
| not overloadedFunctionIsCalled(unusedLocalFunction) and | ||
| not addressBeenTaken(unusedLocalFunction) and | ||
| // Get a printable name | ||
| ( | ||
| if exists(unusedLocalFunction.getQualifiedName()) | ||
| then name = unusedLocalFunction.getQualifiedName() | ||
| else name = unusedLocalFunction.getName() | ||
| ) and | ||
| message = | ||
| unusedLocalFunction.getLocalFunctionType() + " function " + name + | ||
| " is not statically called, or is in an unused template." | ||
| ) | ||
| } | ||
| } | ||
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
8 changes: 8 additions & 0 deletions
8
cpp/common/test/rules/unusedlocalfunction/UnusedLocalFunction.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,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.unusedlocalfunction.UnusedLocalFunction | ||
|
|
||
| module TestFileConfig implements UnusedLocalFunctionConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import UnusedLocalFunction<TestFileConfig> |
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
26 changes: 26 additions & 0 deletions
26
cpp/misra/src/rules/RULE-0-2-4/UnusedLimitedVisibilityFunction.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,26 @@ | ||
| /** | ||
| * @id cpp/misra/unused-limited-visibility-function | ||
| * @name RULE-0-2-4: Functions with limited visibility should be used at least once | ||
| * @description Unused functions may indicate a coding error or require maintenance; functions that | ||
| * are unused with certain visibility have no effect on the program and should be | ||
| * removed. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-0-2-4 | ||
| * scope/system | ||
| * maintainability | ||
| * correctness | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.unusedlocalfunction.UnusedLocalFunction | ||
|
|
||
| module UnusedLimitedVisibilityFunctionConfig implements UnusedLocalFunctionConfigSig { | ||
| Query getQuery() { result = DeadCode10Package::unusedLimitedVisibilityFunctionQuery() } | ||
| } | ||
|
|
||
| import UnusedLocalFunction<UnusedLimitedVisibilityFunctionConfig> |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-0-2-4/UnusedLimitedVisibilityFunction.testref
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 @@ | ||
| cpp/common/test/rules/unusedlocalfunction/UnusedLocalFunction.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
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,30 @@ | ||
| { | ||
| "MISRA-C++-2023": { | ||
| "RULE-0-2-4": { | ||
| "properties": { | ||
| "enforcement": "decidable", | ||
| "obligation": "advisory" | ||
| }, | ||
| "queries": [ | ||
| { | ||
| "description": "Unused functions may indicate a coding error or require maintenance; functions that are unused with certain visibility have no effect on the program and should be removed.", | ||
| "kind": "problem", | ||
| "name": "Functions with limited visibility should be used at least once", | ||
| "precision": "very-high", | ||
| "severity": "error", | ||
| "short_name": "UnusedLimitedVisibilityFunction", | ||
| "shared_implementation_short_name": "UnusedLocalFunction", | ||
| "tags": [ | ||
| "scope/system", | ||
| "maintainability", | ||
| "correctness" | ||
| ], | ||
| "implementation_scope": { | ||
| "description": "Use of any overload of a function in an overload set constitute a use of all members of the set. An overload set is a set of functions with the same name that differ in the number, type and/or qualifiers of their parameters, and, for the purpose of this query, are limited to functions which are declared in the same scope (namespace or class). Functions defined in anonymous (unnamed) namespaces and global namespaces are therefore not currently considered to be part of the same overload set." | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| ], | ||
| "title": "Functions with limited visibility should be used at least once" | ||
| } | ||
| } | ||
| } | ||
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.
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.