@@ -22,37 +22,6 @@ import codingstandards.cpp.Call
22
22
import codingstandards.cpp.Loops
23
23
import codingstandards.cpp.misra.BuiltInTypeRules:: MisraCpp23BuiltInTypes
24
24
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
-
56
25
/**
57
26
* Holds if the given expression may mutate the variable.
58
27
*/
@@ -72,60 +41,6 @@ predicate variableModifiedInExpression(Expr expr, VariableAccess va) {
72
41
valueToUpdate ( va , _, expr )
73
42
}
74
43
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
-
129
44
/**
130
45
* Gets the loop step of a legacy for loop.
131
46
*
@@ -146,9 +61,7 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
146
61
*/
147
62
148
63
/* 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 ( )
152
65
or
153
66
/* 2. Get the expression `E` when the update expression is `i = i + E` or `i = i - E`. */
154
67
(
@@ -175,13 +88,11 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
175
88
* 2. Another variable access of the same variable as the given variable access is assigned
176
89
* to a non-const reference variable (thus constituting a `T` -> `&T` conversion.), i.e.
177
90
* initialization and assignment.
178
- */
179
- /*
91
+ *
180
92
* Note that pass-by-reference is dealt with in a different predicate named
181
93
* `loopVariablePassedAsArgumentToNonConstReferenceParameter`, due to implementation
182
94
* limitations.
183
95
*/
184
-
185
96
predicate loopVariableAssignedToNonConstPointerOrReferenceType (
186
97
ForStmt forLoop , VariableAccess loopVariableAccessInCondition
187
98
) {
@@ -199,7 +110,7 @@ predicate loopVariableAssignedToNonConstPointerOrReferenceType(
199
110
assignmentRhs .( AddressOfExpr ) .getOperand ( ) =
200
111
loopVariableAccessInCondition .getTarget ( ) .getAnAccess ( )
201
112
or
202
- /* 2. The address is taken: A loop variable access */
113
+ /* 2. A reference is taken: A loop variable access */
203
114
assignmentRhs = loopVariableAccessInCondition .getTarget ( ) .getAnAccess ( )
204
115
)
205
116
)
0 commit comments