@@ -16,11 +16,31 @@ import codingstandards.cpp.Customizations
1616import codingstandards.cpp.Exclusions
1717import codingstandards.cpp.deadcode.UselessAssignments
1818import codingstandards.cpp.deadcode.UnreachableCode
19+ import codingstandards.cpp.deadcode.UnusedVariables
1920
2021abstract class DeadCodeSharedQuery extends Query { }
2122
2223Query getQuery ( ) { result instanceof DeadCodeSharedQuery }
2324
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+
2444/**
2545 * Holds if the `Stmt` `s` is either dead or unreachable.
2646 */
@@ -39,6 +59,7 @@ predicate isDeadStmt(Stmt s) {
3959 // - All the declarations are variable declarations
4060 // - None of those variables are ever accessed in non-dead code
4161 // - The initializers for each of the variables are pure
62+ // - It isn't constexpr and used to declare an array size
4263 exists ( DeclStmt ds |
4364 ds = s and
4465 // 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) {
5071 not exists ( VariableAccess va |
5172 va .getTarget ( ) = v and
5273 not isDeadOrUnreachableStmt ( va .getEnclosingStmt ( ) )
53- )
74+ ) and
75+ not isUsedInLocalArraySize ( v , getConstexprValue ( v ) )
5476 )
5577 )
5678 )
0 commit comments