@@ -16,11 +16,31 @@ 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
22
23
Query getQuery ( ) { result instanceof DeadCodeSharedQuery }
23
24
25
+ /**
26
+ * Returns integer value of a constexpr variable
27
+ */
28
+ int getConstexprValue ( Variable v ) {
29
+ result = v .getInitializer ( ) .getExpr ( ) .getValue ( ) .toInt ( ) and v .isConstexpr ( )
30
+ }
31
+
32
+ /**
33
+ * Holds if `Variable` v is used for a local array size with value `n`
34
+ */
35
+ bindingset [ n]
36
+ predicate isUsedInLocalArraySize ( Variable v , int n ) {
37
+ // Cf. https://github.com/github/codeql-coding-standards/pull/660/files.
38
+ count ( ArrayType at , LocalVariable arrayVariable |
39
+ arrayVariable .getType ( ) .resolveTypedefs ( ) = at and
40
+ v .( PotentiallyUnusedLocalVariable ) .getFunction ( ) = arrayVariable .getFunction ( ) and
41
+ at .getArraySize ( ) = n ) > 0
42
+ }
43
+
24
44
/**
25
45
* Holds if the `Stmt` `s` is either dead or unreachable.
26
46
*/
@@ -39,6 +59,7 @@ predicate isDeadStmt(Stmt s) {
39
59
// - All the declarations are variable declarations
40
60
// - None of those variables are ever accessed in non-dead code
41
61
// - The initializers for each of the variables are pure
62
+ // - It isn't constexpr and used to declare an array size
42
63
exists ( DeclStmt ds |
43
64
ds = s and
44
65
// Use forex so that we don't flag "fake" generated `DeclStmt`s (e.g. those generated by the
@@ -50,7 +71,8 @@ predicate isDeadStmt(Stmt s) {
50
71
not exists ( VariableAccess va |
51
72
va .getTarget ( ) = v and
52
73
not isDeadOrUnreachableStmt ( va .getEnclosingStmt ( ) )
53
- )
74
+ ) and
75
+ not isUsedInLocalArraySize ( v , getConstexprValue ( v ) )
54
76
)
55
77
)
56
78
)
0 commit comments