@@ -18,10 +18,10 @@ import cpp
18
18
import codingstandards.cpp.autosar
19
19
import codingstandards.cpp.deadcode.UnusedVariables
20
20
21
- /** Gets the constant value of a constexpr variable. */
21
+ /** Gets the constant value of a constexpr/const variable. */
22
22
private string getConstExprValue ( Variable v ) {
23
23
result = v .getInitializer ( ) .getExpr ( ) .getValue ( ) and
24
- v . isConstexpr ( )
24
+ ( v . isConst ( ) or v . isConstexpr ( ) )
25
25
}
26
26
27
27
// This predicate is similar to getUseCount for M0-1-4 except that it also
@@ -41,13 +41,28 @@ int getUseCountConservatively(Variable v) {
41
41
) +
42
42
// For static asserts too, check if there is a child which has the same value
43
43
// as the constexpr variable.
44
- count ( StaticAssert s | s .getCondition ( ) .getAChild * ( ) .getValue ( ) = getConstExprValue ( v ) )
44
+ count ( StaticAssert s | s .getCondition ( ) .getAChild * ( ) .getValue ( ) = getConstExprValue ( v ) ) +
45
+ // In case an array type uses a constant in the same scope as the constexpr variable,
46
+ // consider it as used.
47
+ count ( ArrayType at , LocalVariable arrayVariable |
48
+ arrayVariable .getType ( ) .resolveTypedefs ( ) = at and
49
+ v .( PotentiallyUnusedLocalVariable ) .getFunction ( ) = arrayVariable .getFunction ( ) and
50
+ at .getArraySize ( ) .toString ( ) = getConstExprValue ( v )
51
+ )
45
52
}
46
53
47
54
from PotentiallyUnusedLocalVariable v
48
55
where
49
56
not isExcluded ( v , DeadCodePackage:: unusedLocalVariableQuery ( ) ) and
50
57
// Local variable is never accessed
51
58
not exists ( v .getAnAccess ( ) ) and
59
+ // Sometimes multiple objects representing the same entities are created in
60
+ // the AST. Check if those are not accessed as well. Refer issue #658
61
+ not exists ( LocalScopeVariable another |
62
+ another .getDefinitionLocation ( ) = v .getDefinitionLocation ( ) and
63
+ another .hasName ( v .getName ( ) ) and
64
+ exists ( another .getAnAccess ( ) ) and
65
+ another != v
66
+ ) and
52
67
getUseCountConservatively ( v ) = 0
53
68
select v , "Local variable '" + v .getName ( ) + "' in '" + v .getFunction ( ) .getName ( ) + "' is not used."
0 commit comments