Skip to content

Commit 15801d9

Browse files
committed
Refactor to share a predicate between M0-1-3 and M0-1-9 reducing duplication.
1 parent c765f93 commit 15801d9

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

cpp/autosar/src/rules/M0-1-3/UnusedLocalVariable.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff 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

5450
from PotentiallyUnusedLocalVariable v

cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

cpp/common/src/codingstandards/cpp/rules/deadcode/DeadCode.qll

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,6 @@ abstract class DeadCodeSharedQuery extends Query { }
2222

2323
Query 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
)

0 commit comments

Comments
 (0)