File tree 6 files changed +38
-14
lines changed
6 files changed +38
-14
lines changed Original file line number Diff line number Diff line change
1
+ - ` M0-1-9 ` - ` DeadCode.qll ` :
2
+ - Fixes #678 . Remove dead code false positive when integer constant expression is used to define the size of an array.
Original file line number Diff line number Diff line change @@ -18,12 +18,6 @@ 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/const variable. */
22
- private string getConstExprValue ( Variable v ) {
23
- result = v .getInitializer ( ) .getExpr ( ) .getValue ( ) and
24
- ( v .isConst ( ) or v .isConstexpr ( ) )
25
- }
26
-
27
21
// This predicate is similar to getUseCount for M0-1-4 except that it also
28
22
// considers static_asserts. This was created to cater for M0-1-3 specifically
29
23
// and hence, doesn't attempt to reuse the M0-1-4 specific predicate
@@ -44,11 +38,7 @@ int getUseCountConservatively(Variable v) {
44
38
count ( StaticAssert s | s .getCondition ( ) .getAChild * ( ) .getValue ( ) = getConstExprValue ( v ) ) +
45
39
// In case an array type uses a constant in the same scope as the constexpr variable,
46
40
// 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
- )
41
+ countUsesInLocalArraySize ( v )
52
42
}
53
43
54
44
from PotentiallyUnusedLocalVariable v
Original file line number Diff line number Diff line change @@ -150,3 +150,21 @@ predicate maybeACompileTimeTemplateArgument(Variable v) {
150
150
)
151
151
)
152
152
}
153
+
154
+ /** Gets the constant value of a constexpr/const variable. */
155
+ string getConstExprValue ( Variable v ) {
156
+ result = v .getInitializer ( ) .getExpr ( ) .getValue ( ) and
157
+ ( v .isConst ( ) or v .isConstexpr ( ) )
158
+ }
159
+
160
+ /**
161
+ * Counts uses of `Variable` v in a local array of size `n`
162
+ */
163
+ int countUsesInLocalArraySize ( Variable v ) {
164
+ result =
165
+ count ( ArrayType at , LocalVariable arrayVariable |
166
+ arrayVariable .getType ( ) .resolveTypedefs ( ) = at and
167
+ v .( PotentiallyUnusedLocalVariable ) .getFunction ( ) = arrayVariable .getFunction ( ) and
168
+ at .getArraySize ( ) .toString ( ) = getConstExprValue ( v )
169
+ )
170
+ }
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import codingstandards.cpp.Customizations
16
16
import codingstandards.cpp.Exclusions
17
17
import codingstandards.cpp.deadcode.UselessAssignments
18
18
import codingstandards.cpp.deadcode.UnreachableCode
19
+ import codingstandards.cpp.deadcode.UnusedVariables
19
20
20
21
abstract class DeadCodeSharedQuery extends Query { }
21
22
@@ -39,6 +40,7 @@ predicate isDeadStmt(Stmt s) {
39
40
// - All the declarations are variable declarations
40
41
// - None of those variables are ever accessed in non-dead code
41
42
// - The initializers for each of the variables are pure
43
+ // - It isn't constexpr and used to declare an array size
42
44
exists ( DeclStmt ds |
43
45
ds = s and
44
46
// Use forex so that we don't flag "fake" generated `DeclStmt`s (e.g. those generated by the
@@ -50,7 +52,8 @@ predicate isDeadStmt(Stmt s) {
50
52
not exists ( VariableAccess va |
51
53
va .getTarget ( ) = v and
52
54
not isDeadOrUnreachableStmt ( va .getEnclosingStmt ( ) )
53
- )
55
+ ) and
56
+ not countUsesInLocalArraySize ( v ) > 0
54
57
)
55
58
)
56
59
)
Original file line number Diff line number Diff line change 12
12
| test.cpp:72:3:73:3 | try { ... } | This statement is dead code. |
13
13
| test.cpp:73:17:74:3 | { ... } | This statement is dead code. |
14
14
| test.cpp:79:17:80:3 | { ... } | This statement is dead code. |
15
+ | test.cpp:85:3:85:43 | declaration | This statement is dead code. |
16
+ | test.cpp:87:3:87:30 | declaration | This statement is dead code. |
17
+ | test.cpp:90:3:90:50 | declaration | This statement is dead code. |
Original file line number Diff line number Diff line change @@ -81,5 +81,13 @@ int test_dead_code(int x) {
81
81
82
82
static_assert (1 ); // COMPLIANT
83
83
84
- return live5 + live6; // COMPLIANT
85
- }
84
+ constexpr int constexpr_array_size{6 }; // COMPLIANT
85
+ int unused_array[constexpr_array_size]{}; // NON_COMPLIANT
86
+
87
+ constexpr int unused_int{2 }; // NON_COMPLIANT
88
+
89
+ constexpr int constexpr_used_array[]{3 , 4 , 5 }; // COMPLIANT
90
+ constexpr int constexpr_unused_array[]{0 , 1 , 2 }; // NON_COMPLIANT
91
+
92
+ return live5 + live6 + constexpr_used_array[1 ]; // COMPLIANT
93
+ }
You can’t perform that action at this time.
0 commit comments