Skip to content

Commit 74a5d7a

Browse files
committed
Loops: Handle user crement/assignment
1 parent 585ebbb commit 74a5d7a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

cpp/common/src/codingstandards/cpp/Loops.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ private predicate inForUpdate(Expr forUpdate, Expr child) {
4949
exists(Expr mid | inForUpdate(forUpdate, mid) and child.getParent() = mid)
5050
}
5151

52+
class MemberCrementOperation extends FunctionCall {
53+
MemberCrementOperation() { this.getTarget() instanceof UserCrementOperator }
54+
55+
Expr getOperand() { result = this.getQualifier() }
56+
}
57+
58+
class MemberAssignmentOperation extends FunctionCall {
59+
MemberAssignmentOperation() { this.getTarget() instanceof AssignmentOperator }
60+
61+
Expr getLValue() { result = this.getQualifier() }
62+
63+
string getOperator() { result = this.getTarget().getName().regexpCapture("operator(.+)", 1) }
64+
}
65+
5266
/**
5367
* Gets a LoopCounter for the given `ForStmt`.
5468
*
@@ -68,6 +82,21 @@ Variable getALoopCounter(ForStmt fs) {
6882
va = result.getAnAccess()
6983
)
7084
or
85+
exists(MemberCrementOperation op, VariableAccess va |
86+
op = updateOp and
87+
op instanceof MemberCrementOperation and
88+
op.getOperand() = va and
89+
va = result.getAnAccess()
90+
)
91+
or
92+
exists(MemberAssignmentOperation op, VariableAccess va |
93+
op = updateOp and
94+
op instanceof MemberAssignmentOperation and
95+
op.getOperator() = ["+=", "-="] and
96+
op.getLValue() = va and
97+
va = result.getAnAccess()
98+
)
99+
or
71100
exists(AssignArithmeticOperation op, VariableAccess va |
72101
op = updateOp and
73102
op instanceof AssignArithmeticOperation and

0 commit comments

Comments
 (0)