File tree Expand file tree Collapse file tree 3 files changed +18
-25
lines changed
common/src/codingstandards/cpp Expand file tree Collapse file tree 3 files changed +18
-25
lines changed Original file line number Diff line number Diff line change @@ -44,11 +44,7 @@ int getUseCountConservatively(Variable v) {
4444 count ( StaticAssert s | s .getCondition ( ) .getAChild * ( ) .getValue ( ) = getConstExprValue ( v ) ) +
4545 // In case an array type uses a constant in the same scope as the constexpr variable,
4646 // 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- )
47+ countUsesInLocalArraySize ( v )
5248}
5349
5450from PotentiallyUnusedLocalVariable v
Original file line number Diff line number Diff line change @@ -150,3 +150,19 @@ predicate maybeACompileTimeTemplateArgument(Variable v) {
150150 )
151151 )
152152}
153+
154+ /** Gets the constant value of a constexpr/const variable. */
155+ private 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 = count ( ArrayType at , LocalVariable arrayVariable |
165+ arrayVariable .getType ( ) .resolveTypedefs ( ) = at and
166+ v .( PotentiallyUnusedLocalVariable ) .getFunction ( ) = arrayVariable .getFunction ( ) and
167+ at .getArraySize ( ) .toString ( ) = getConstExprValue ( v ) )
168+ }
Original file line number Diff line number Diff line change @@ -22,25 +22,6 @@ abstract class DeadCodeSharedQuery extends Query { }
2222
2323Query getQuery ( ) { result instanceof DeadCodeSharedQuery }
2424
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-
4425/**
4526 * Holds if the `Stmt` `s` is either dead or unreachable.
4627 */
@@ -72,7 +53,7 @@ predicate isDeadStmt(Stmt s) {
7253 va .getTarget ( ) = v and
7354 not isDeadOrUnreachableStmt ( va .getEnclosingStmt ( ) )
7455 ) and
75- not isUsedInLocalArraySize ( v , getConstexprValue ( v ) )
56+ not ( countUsesInLocalArraySize ( v ) > 0 )
7657 )
7758 )
7859 )
You can’t perform that action at this time.
0 commit comments