@@ -22,37 +22,6 @@ import codingstandards.cpp.Call
2222import codingstandards.cpp.Loops
2323import 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) {
7241valueToUpdate ( 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 ( )
15265or
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-
18596predicate loopVariableAssignedToNonConstPointerOrReferenceType (
18697ForStmt forLoop , VariableAccess loopVariableAccessInCondition
18798) {
@@ -199,7 +110,7 @@ predicate loopVariableAssignedToNonConstPointerOrReferenceType(
199110assignmentRhs .( AddressOfExpr ) .getOperand ( ) =
200111loopVariableAccessInCondition .getTarget ( ) .getAnAccess ( )
201112or
202- /* 2.The address is taken: A loop variable access */
113+ /* 2.A reference is taken: A loop variable access */
203114assignmentRhs = loopVariableAccessInCondition .getTarget ( ) .getAnAccess ( )
204115)
205116)