Skip to content

Commit c8c0770

Browse files
committed
Separate out helper classes into libraries
1 parent a653b66 commit c8c0770

File tree

3 files changed

+91
-92
lines changed

3 files changed

+91
-92
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,34 @@ predicate isInvalidLoop(ForStmt forLoop, string reason, Locatable reasonLocation
374374
reason = "its $@ is not a boolean" and
375375
reasonLabel = "loop control variable"
376376
}
377+
378+
/**
379+
* A comparison expression that has the minimum qualification as being a valid termination
380+
* condition of a legacy for-loop. It is characterized by a value read from a variable being
381+
* compared to a value, which is supposed to be the loop bound.
382+
*/
383+
class LegacyForLoopCondition extends RelationalOperation {
384+
/**
385+
* The legacy for-loop this relational operation is a condition of.
386+
*/
387+
ForStmt forLoop;
388+
VariableAccess loopCounter;
389+
Expr loopBound;
390+
391+
LegacyForLoopCondition() {
392+
loopCounter = this.getAnOperand() and
393+
loopBound = this.getAnOperand() and
394+
loopCounter.getTarget() = getAnIterationVariable(forLoop) and
395+
loopBound != loopCounter
396+
}
397+
398+
/**
399+
* Gets the variable access to the loop counter variable, appearing in this loop condition.
400+
*/
401+
VariableAccess getLoopCounter() { result = loopCounter }
402+
403+
/**
404+
* Gets the variable access to the loop bound variable, appearing in this loop condition.
405+
*/
406+
Expr getLoopBound() { result = loopBound }
407+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import cpp
2+
3+
abstract class LegacyForLoopUpdateExpression extends Expr {
4+
ForStmt forLoop;
5+
6+
LegacyForLoopUpdateExpression() { this = forLoop.getUpdate().getAChild*() }
7+
8+
abstract Expr getLoopStep();
9+
/* TODO: Complete below and use it for 3-2 */
10+
// abstract VariableAccess getUpdatedVariable();
11+
}
12+
13+
class CrementLegacyForLoopUpdateExpression extends LegacyForLoopUpdateExpression {
14+
CrementLegacyForLoopUpdateExpression() { this instanceof CrementOperation }
15+
16+
override Expr getLoopStep() { none() }
17+
}
18+
19+
class AssignAddOrSubExpr extends LegacyForLoopUpdateExpression {
20+
AssignAddOrSubExpr() {
21+
this instanceof AssignAddExpr or
22+
this instanceof AssignSubExpr
23+
}
24+
25+
override Expr getLoopStep() {
26+
result = this.(AssignAddExpr).getRValue() or
27+
result = this.(AssignSubExpr).getRValue()
28+
}
29+
}
30+
31+
class AddOrSubThenAssignExpr extends LegacyForLoopUpdateExpression {
32+
Expr assignRhs;
33+
34+
AddOrSubThenAssignExpr() {
35+
this.(AssignExpr).getRValue() = assignRhs and
36+
(
37+
assignRhs instanceof AddExpr or
38+
assignRhs instanceof SubExpr
39+
)
40+
}
41+
42+
override Expr getLoopStep() {
43+
(
44+
result = assignRhs.(AddExpr).getAnOperand() or
45+
result = assignRhs.(SubExpr).getAnOperand()
46+
) and
47+
exists(VariableAccess iterationVariableAccess |
48+
(
49+
iterationVariableAccess = assignRhs.(AddExpr).getAnOperand()
50+
or
51+
iterationVariableAccess = assignRhs.(SubExpr).getAnOperand()
52+
) and
53+
iterationVariableAccess.getTarget() = forLoop.getAnIterationVariable() and
54+
result != iterationVariableAccess
55+
)
56+
}
57+
}

cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,6 @@ import codingstandards.cpp.Call
2222
import codingstandards.cpp.Loops
2323
import codingstandards.cpp.misra.BuiltInTypeRules::MisraCpp23BuiltInTypes
2424

25-
/**
26-
* A comparison expression that has the minimum qualification as being a valid termination
27-
* condition of a legacy for-loop. It is characterized by a value read from a variable being
28-
* compared to a value, which is supposed to be the loop bound.
29-
*/
30-
class LegacyForLoopCondition extends RelationalOperation {
31-
/**
32-
* The legacy for-loop this relational operation is a condition of.
33-
*/
34-
ForStmt forLoop;
35-
VariableAccess loopCounter;
36-
Expr loopBound;
37-
38-
LegacyForLoopCondition() {
39-
loopCounter = this.getAnOperand() and
40-
loopBound = this.getAnOperand() and
41-
loopCounter.getTarget() = getAnIterationVariable(forLoop) and
42-
loopBound != loopCounter
43-
}
44-
45-
/**
46-
* Gets the variable access to the loop counter variable, embedded in this loop condition.
47-
*/
48-
VariableAccess getLoopCounter() { result = loopCounter }
49-
50-
/**
51-
* Gets the variable access to the loop bound variable, embedded in this loop condition.
52-
*/
53-
Expr getLoopBound() { result = loopBound }
54-
}
55-
5625
/**
5726
* Holds if the given expression may mutate the variable.
5827
*/
@@ -72,60 +41,6 @@ predicate variableModifiedInExpression(Expr expr, VariableAccess va) {
7241
valueToUpdate(va, _, expr)
7342
}
7443

75-
abstract class LegacyForLoopUpdateExpression extends Expr {
76-
ForStmt forLoop;
77-
78-
LegacyForLoopUpdateExpression() { this = forLoop.getUpdate().getAChild*() }
79-
80-
abstract Expr getLoopStep();
81-
}
82-
83-
class CrementLegacyForLoopUpdateExpression extends LegacyForLoopUpdateExpression {
84-
CrementLegacyForLoopUpdateExpression() { this instanceof CrementOperation }
85-
86-
override Expr getLoopStep() { none() }
87-
}
88-
89-
class AssignAddOrSubExpr extends LegacyForLoopUpdateExpression {
90-
AssignAddOrSubExpr() {
91-
this instanceof AssignAddExpr or
92-
this instanceof AssignSubExpr
93-
}
94-
95-
override Expr getLoopStep() {
96-
result = this.(AssignAddExpr).getRValue() or
97-
result = this.(AssignSubExpr).getRValue()
98-
}
99-
}
100-
101-
class AddOrSubThenAssignExpr extends LegacyForLoopUpdateExpression {
102-
Expr assignRhs;
103-
104-
AddOrSubThenAssignExpr() {
105-
this.(AssignExpr).getRValue() = assignRhs and
106-
(
107-
assignRhs instanceof AddExpr or
108-
assignRhs instanceof SubExpr
109-
)
110-
}
111-
112-
override Expr getLoopStep() {
113-
(
114-
result = assignRhs.(AddExpr).getAnOperand() or
115-
result = assignRhs.(SubExpr).getAnOperand()
116-
) and
117-
exists(VariableAccess iterationVariableAccess |
118-
(
119-
iterationVariableAccess = assignRhs.(AddExpr).getAnOperand()
120-
or
121-
iterationVariableAccess = assignRhs.(SubExpr).getAnOperand()
122-
) and
123-
iterationVariableAccess.getTarget() = forLoop.getAnIterationVariable() and
124-
result != iterationVariableAccess
125-
)
126-
}
127-
}
128-
12944
/**
13045
* Gets the loop step of a legacy for loop.
13146
*
@@ -146,9 +61,7 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
14661
*/
14762

14863
/* 1. Get the expression `E` when the update expression is `i += E` or `i -= E`. */
149-
result = forLoop.getUpdate().getAChild*().(AssignAddExpr).getRValue()
150-
or
151-
result = forLoop.getUpdate().getAChild*().(AssignSubExpr).getRValue()
64+
result = forLoop.getUpdate().getAChild*().(AssignAddOrSubExpr).getLoopStep()
15265
or
15366
/* 2. Get the expression `E` when the update expression is `i = i + E` or `i = i - E`. */
15467
(
@@ -175,13 +88,11 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
17588
* 2. Another variable access of the same variable as the given variable access is assigned
17689
* to a non-const reference variable (thus constituting a `T` -> `&T` conversion.), i.e.
17790
* initialization and assignment.
178-
*/
179-
/*
91+
*
18092
* Note that pass-by-reference is dealt with in a different predicate named
18193
* `loopVariablePassedAsArgumentToNonConstReferenceParameter`, due to implementation
18294
* limitations.
18395
*/
184-
18596
predicate loopVariableAssignedToNonConstPointerOrReferenceType(
18697
ForStmt forLoop, VariableAccess loopVariableAccessInCondition
18798
) {
@@ -199,7 +110,7 @@ predicate loopVariableAssignedToNonConstPointerOrReferenceType(
199110
assignmentRhs.(AddressOfExpr).getOperand() =
200111
loopVariableAccessInCondition.getTarget().getAnAccess()
201112
or
202-
/* 2. The address is taken: A loop variable access */
113+
/* 2. A reference is taken: A loop variable access */
203114
assignmentRhs = loopVariableAccessInCondition.getTarget().getAnAccess()
204115
)
205116
)

0 commit comments

Comments
 (0)