Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
93 changes: 4 additions & 89 deletions cpp/autosar/src/rules/A0-1-3/UnusedLocalFunction.ql
Original file line number Diff line number Diff line change
Expand Up @@ -16,95 +16,10 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.DynamicCallGraph
import codingstandards.cpp.deadcode.UnusedFunctions
import codingstandards.cpp.rules.unusedlocalfunction.UnusedLocalFunction

/**
* 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 member functions, which don't have internal linkage
not this instanceof MemberFunction and
localFunctionType = "Anonymous namespace"
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 }
module UnusedLocalFunctionConfig implements UnusedLocalFunctionConfigSig {
Query getQuery() { result = DeadCodePackage::unusedLocalFunctionQuery() }
}

from LocalFunction unusedLocalFunction, string name
where
not isExcluded(unusedLocalFunction, DeadCodePackage::unusedLocalFunctionQuery()) 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()
)
select unusedLocalFunction,
unusedLocalFunction.getLocalFunctionType() + " function " + name +
" is not statically called, or is in an unused template."
import UnusedLocalFunction<UnusedLocalFunctionConfig>
110 changes: 110 additions & 0 deletions cpp/autosar/src/rules/A0-1-3/UnusedLocalFunctionOld.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @id cpp/autosar/unused-local-function
* @name A0-1-3: Unused local function
* @description Every function defined in an anonymous namespace, or static function with internal
* linkage, or private member function shall be used.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/autosar/id/a0-1-3
* readability
Comment on lines +1 to +10
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

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

UnusedLocalFunctionOld.ql is a full query (has @id, @kind, etc.) that duplicates the existing query id cpp/autosar/unused-local-function from UnusedLocalFunction.ql. Because cpp/autosar/src/qlpack.yml doesn't exclude files, this will be included in the pack and can cause duplicate-query-id conflicts / unintended extra shipped query. Remove this file, or convert it into non-query documentation (e.g., move out of the pack, rename to .qll, or remove query metadata) if you need to keep it for reference.

Copilot uses AI. Check for mistakes.
* maintainability
* external/autosar/allocated-target/implementation
* external/autosar/enforcement/automated
* external/autosar/obligation/required
*/

import cpp
import codingstandards.cpp.autosar
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 member functions, which don't have internal linkage
not this instanceof MemberFunction and
localFunctionType = "Anonymous namespace"
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 }
}

from LocalFunction unusedLocalFunction, string name
where
not isExcluded(unusedLocalFunction, DeadCodePackage::unusedLocalFunctionQuery()) 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()
)
select unusedLocalFunction,
unusedLocalFunction.getLocalFunctionType() + " function " + name +
" is not statically called, or is in an unused template."
1 change: 0 additions & 1 deletion cpp/autosar/test/rules/A0-1-3/UnusedLocalFunction.qlref

This file was deleted.

1 change: 1 addition & 0 deletions cpp/autosar/test/rules/A0-1-3/UnusedLocalFunction.testref
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 cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode10.qll
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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Const
import Conversions
import Conversions2
import DeadCode
import DeadCode10
import DeadCode3
import DeadCode4
import Declarations
Expand Down Expand Up @@ -89,6 +90,7 @@ newtype TCPPQuery =
TConversionsPackageQuery(ConversionsQuery q) or
TConversions2PackageQuery(Conversions2Query q) or
TDeadCodePackageQuery(DeadCodeQuery q) or
TDeadCode10PackageQuery(DeadCode10Query q) or
TDeadCode3PackageQuery(DeadCode3Query q) or
TDeadCode4PackageQuery(DeadCode4Query q) or
TDeclarationsPackageQuery(DeclarationsQuery q) or
Expand Down Expand Up @@ -161,6 +163,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isConversionsQueryMetadata(query, queryId, ruleId, category) or
isConversions2QueryMetadata(query, queryId, ruleId, category) or
isDeadCodeQueryMetadata(query, queryId, ruleId, category) or
isDeadCode10QueryMetadata(query, queryId, ruleId, category) or
isDeadCode3QueryMetadata(query, queryId, ruleId, category) or
isDeadCode4QueryMetadata(query, queryId, ruleId, category) or
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
Expand Down
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
Comment on lines +48 to +50
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

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

LocalFunction() assigns localFunctionType in multiple disjuncts that can overlap (e.g., a private member function inside an anonymous namespace satisfies both the "Private member" branch and the "Anonymous namespace class member" branch). This can give a single function multiple localFunctionType values and lead to duplicate results with different messages. Make these cases mutually exclusive (for example, exclude private members from the anonymous-namespace-member branch, or restructure with an if/else-style chain) and add/adjust a unit test to guard against duplicated findings.

Suggested change
// Class members in anonymous namespaces also have internal linkage.
this instanceof AnonymousNamespaceFunction and
this instanceof MemberFunction and
// Class members in anonymous namespaces also have internal linkage.
// Exclude private members, which are already covered by the "Private member" case above.
this instanceof AnonymousNamespaceFunction and
this instanceof MemberFunction and
not this.(MemberFunction).isPrivate() and

Copilot uses AI. Check for mistakes.
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."
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
| test.cpp:60:5:60:9 | getAT | Private member function C<T>::getAT is not statically called, or is in an unused template. |
| test.cpp:81:6:81:7 | h2 | Anonymous namespace function (unnamed namespace)::h2 is not statically called, or is in an unused template. |
| test.cpp:85:6:85:7 | h3 | Anonymous namespace function (unnamed namespace)::foo::bar::h3 is not statically called, or is in an unused template. |
| test.cpp:144:8:144:8 | f | Anonymous namespace class member function (unnamed namespace)::C1::f is not statically called, or is in an unused template. |
| test.cpp:150:8:150:8 | f | Anonymous namespace class member function (unnamed namespace)::named::C2::f is not statically called, or is in an unused template. |
Loading
Loading