|
| 1 | +/** |
| 2 | + * Provides a configurable module UnusedLocalFunction with a `problems` predicate |
| 3 | + * for the following issue: |
| 4 | + * Unused functions may indicate a coding error or require maintenance; functions that |
| 5 | + * are unused with certain visibility have no effect on the program and should be |
| 6 | + * removed. |
| 7 | + */ |
| 8 | + |
| 9 | +import cpp |
| 10 | +import codingstandards.cpp.Customizations |
| 11 | +import codingstandards.cpp.Exclusions |
| 12 | +import codingstandards.cpp.DynamicCallGraph |
| 13 | +import codingstandards.cpp.deadcode.UnusedFunctions |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks if an overloaded function of |
| 17 | + * the function passed in the arguments, is called. |
| 18 | + */ |
| 19 | +predicate overloadedFunctionIsCalled(Function unusedFunction) { |
| 20 | + exists(Function f | f = unusedFunction.getAnOverload() and f = getTarget(_)) |
| 21 | +} |
| 22 | + |
| 23 | +/** Checks if a Function's address was taken. */ |
| 24 | +predicate addressBeenTaken(Function unusedFunction) { |
| 25 | + exists(FunctionAccess fa | fa.getTarget() = unusedFunction) |
| 26 | +} |
| 27 | + |
| 28 | +/** A `Function` nested in an anonymous namespace. */ |
| 29 | +class AnonymousNamespaceFunction extends Function { |
| 30 | + AnonymousNamespaceFunction() { getNamespace().getParentNamespace*().isAnonymous() } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * A function which is "local" to a particular scope or translation unit. |
| 35 | + */ |
| 36 | +class LocalFunction extends UnusedFunctions::UsableFunction { |
| 37 | + string localFunctionType; |
| 38 | + |
| 39 | + LocalFunction() { |
| 40 | + this.(MemberFunction).isPrivate() and |
| 41 | + localFunctionType = "Private member" |
| 42 | + or |
| 43 | + // A function in an anonymous namespace (which is deduced to have internal linkage) |
| 44 | + this instanceof AnonymousNamespaceFunction and |
| 45 | + not this instanceof MemberFunction and |
| 46 | + localFunctionType = "Anonymous namespace" |
| 47 | + or |
| 48 | + // Class members in anonymous namespaces also have internal linkage. |
| 49 | + this instanceof AnonymousNamespaceFunction and |
| 50 | + this instanceof MemberFunction and |
| 51 | + localFunctionType = "Anonymous namespace class member" |
| 52 | + or |
| 53 | + // Static functions with internal linkage |
| 54 | + this.isStatic() and |
| 55 | + // Member functions never have internal linkage |
| 56 | + not this instanceof MemberFunction and |
| 57 | + // Functions in anonymous namespaces automatically have the "static" specifier added by the |
| 58 | + // extractor. We therefore excluded them from this case, and instead report them in the |
| 59 | + // anonymous namespace, as we don't know whether the "static" specifier was explicitly |
| 60 | + // provided by the user. |
| 61 | + not this instanceof AnonymousNamespaceFunction and |
| 62 | + localFunctionType = "Static" |
| 63 | + } |
| 64 | + |
| 65 | + /** Gets the type of local function. */ |
| 66 | + string getLocalFunctionType() { result = localFunctionType } |
| 67 | +} |
| 68 | + |
| 69 | +signature module UnusedLocalFunctionConfigSig { |
| 70 | + Query getQuery(); |
| 71 | +} |
| 72 | + |
| 73 | +module UnusedLocalFunction<UnusedLocalFunctionConfigSig Config> { |
| 74 | + query predicate problems(LocalFunction unusedLocalFunction, string message) { |
| 75 | + not isExcluded(unusedLocalFunction, Config::getQuery()) and |
| 76 | + // No static or dynamic call target for this function |
| 77 | + not unusedLocalFunction = getTarget(_) and |
| 78 | + // If this is a TemplateFunction or an instantiation of a template, then only report it as unused |
| 79 | + // if all other instantiations of the template are unused |
| 80 | + not exists( |
| 81 | + Function functionFromUninstantiatedTemplate, Function functionFromInstantiatedTemplate |
| 82 | + | |
| 83 | + // `unusedLocalFunction` is a template instantiation from `functionFromUninstantiatedTemplate` |
| 84 | + unusedLocalFunction.isConstructedFrom(functionFromUninstantiatedTemplate) |
| 85 | + or |
| 86 | + // `unusedLocalFunction` is from an uninstantiated template |
| 87 | + unusedLocalFunction = functionFromUninstantiatedTemplate |
| 88 | + | |
| 89 | + // There exists an instantiation which is called |
| 90 | + functionFromInstantiatedTemplate.isConstructedFrom(functionFromUninstantiatedTemplate) and |
| 91 | + functionFromInstantiatedTemplate = getTarget(_) |
| 92 | + ) and |
| 93 | + // A function is defined as "used" if any one of the following holds true: |
| 94 | + // - It's an explicitly deleted functions e.g. =delete |
| 95 | + // - It's annotated as "[[maybe_unused]]" |
| 96 | + // - It's part of an overloaded set and any one of the overloaded instance |
| 97 | + // is called. |
| 98 | + // - It's an operand of an expression in an unevaluated context. |
| 99 | + not unusedLocalFunction.isDeleted() and |
| 100 | + not unusedLocalFunction.getAnAttribute().getName() = "maybe_unused" and |
| 101 | + not overloadedFunctionIsCalled(unusedLocalFunction) and |
| 102 | + not addressBeenTaken(unusedLocalFunction) and |
| 103 | + message = |
| 104 | + unusedLocalFunction.getLocalFunctionType() + " function " + unusedLocalFunction.getName() + |
| 105 | + " is not statically called, or is in an unused template." |
| 106 | + } |
| 107 | +} |
0 commit comments