Skip to content

Commit 5e24d1b

Browse files
committed
Introduce from variables and fix logical operator association
1 parent 6855e6a commit 5e24d1b

File tree

1 file changed

+104
-87
lines changed

1 file changed

+104
-87
lines changed

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

Lines changed: 104 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -164,98 +164,115 @@ predicate loopVariablePassedAsArgumentToNonConstReferenceParameter(
164164
)
165165
}
166166

167-
from ForStmt forLoop
167+
from ForStmt forLoop, Locatable forLoopExpr, string message
168168
where
169169
not isExcluded(forLoop, StatementsPackage::legacyForStatementsShouldBeSimpleQuery()) and
170-
/* 1. There is a counter variable that is not of an integer type. */
171-
exists(Type type | type = forLoop.getAnIterationVariable().getType() |
172-
not (
173-
type instanceof IntegralType or
174-
type instanceof FixedWidthIntegralType
175-
)
176-
)
177-
or
178-
/*
179-
* 2. The loop condition checks termination without comparing the counter variable and the
180-
* loop bound using a relational operator.
181-
*/
170+
(
171+
/* 1. There is a counter variable that is not of an integer type. */
172+
exists(Type type | type = forLoop.getAnIterationVariable().getType() |
173+
not (
174+
type instanceof IntegralType or
175+
type instanceof FixedWidthIntegralType
176+
)
177+
) and
178+
forLoopExpr = forLoop.getAnIterationVariable() and
179+
message = "The counter variable is not of an integer type."
180+
or
181+
/*
182+
* 2. The loop condition checks termination without comparing the counter variable to the
183+
* loop bound using a relational operator.
184+
*/
182185

183-
not forLoop.getCondition() instanceof LegacyForLoopCondition
184-
or
185-
/* 3. The loop counter is mutated somewhere other than its update expression. */
186-
exists(Variable loopCounter |
187-
isIrregularLoopCounterModification(forLoop, loopCounter, loopCounter.getAnAccess())
188-
)
189-
or
190-
/* 4. The type size of the loop counter is not greater or equal to that of the loop counter. */
191-
exists(LegacyForLoopCondition forLoopCondition | forLoopCondition = forLoop.getCondition() |
192-
exists(Type loopCounterType, Type loopBoundType |
193-
loopCounterType = forLoopCondition.getLoopCounter().getType() and
194-
loopBoundType = forLoopCondition.getLoopBound().getType()
195-
|
196-
loopCounterType.getSize() < loopBoundType.getSize()
197-
)
198-
)
199-
or
200-
/*
201-
* 5. The loop bound and the loop step are non-const expressions, or are variables that are
202-
* mutated in the for loop.
203-
*/
186+
not forLoop.getCondition() instanceof LegacyForLoopCondition and
187+
forLoopExpr = forLoop.getCondition() and
188+
message = "TODO"
189+
or
190+
/* 3. The loop counter is mutated somewhere other than its update expression. */
191+
exists(Variable loopCounter |
192+
isIrregularLoopCounterModification(forLoop, loopCounter, loopCounter.getAnAccess())
193+
) and
194+
forLoopExpr = forLoop.getCondition().(LegacyForLoopCondition).getLoopCounter() and
195+
message = "TODO"
196+
or
197+
/* 4. The type size of the loop counter is not greater or equal to that of the loop counter. */
198+
exists(LegacyForLoopCondition forLoopCondition | forLoopCondition = forLoop.getCondition() |
199+
exists(Type loopCounterType, Type loopBoundType |
200+
loopCounterType = forLoopCondition.getLoopCounter().getType() and
201+
loopBoundType = forLoopCondition.getLoopBound().getType()
202+
|
203+
loopCounterType.getSize() < loopBoundType.getSize()
204+
)
205+
) and
206+
forLoopExpr = forLoop.getCondition() and
207+
message = "TODO"
208+
or
209+
/*
210+
* 5. The loop bound and the loop step are non-const expressions, or are variables that are
211+
* mutated in the for loop.
212+
*/
204213

205-
/* 5-1. The mutating expression mutates the loop bound. */
206-
exists(Expr loopBound |
207-
loopBound = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound()
208-
|
209-
exists(Expr mutatingExpr |
210-
/* The mutating expression may be in the loop body. */
211-
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
212-
or
213-
/* The mutating expression may be in the loop updating expression. */
214-
mutatingExpr = forLoop.getUpdate().getAChild*()
214+
/* 5-1. The mutating expression mutates the loop bound. */
215+
exists(Expr loopBound |
216+
loopBound = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound()
215217
|
216-
/* 5-1-1. The loop bound is a variable that is mutated in the for loop. */
217-
variableModifiedInExpression(mutatingExpr,
218-
loopBound.(VariableAccess).getTarget().getAnAccess())
219-
or
220-
/* 5-1-2. The loop bound is not a variable access and is not a constant expression. */
221-
not loopBound instanceof VariableAccess and not loopBound.isConstant()
222-
)
223-
)
224-
or
225-
/* 5-2. The mutating expression mutates the loop step. */
226-
exists(Expr loopStep | loopStep = getLoopStepOfForStmt(forLoop) |
227-
exists(Expr mutatingExpr |
228-
/* The mutating expression may be in the loop body. */
229-
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
230-
or
231-
/* The mutating expression may be in the loop updating expression. */
232-
mutatingExpr = forLoop.getUpdate().getAChild*()
233-
|
234-
/* 5-1-2. The loop step is a variable that is mutated in the for loop. */
235-
variableModifiedInExpression(mutatingExpr, loopStep.(VariableAccess).getTarget().getAnAccess())
236-
or
237-
/* 5-1-2. The loop bound is not a variable access and is not a constant expression. */
238-
not loopStep instanceof VariableAccess and not loopStep.isConstant()
239-
)
240-
)
241-
or
242-
/*
243-
* 6. Any of the loop counter, loop bound, or a loop step is taken as a mutable reference
244-
* or its address to a mutable pointer.
245-
*/
218+
exists(Expr mutatingExpr |
219+
/* The mutating expression may be in the loop body. */
220+
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
221+
or
222+
/* The mutating expression may be in the loop updating expression. */
223+
mutatingExpr = forLoop.getUpdate().getAChild*()
224+
|
225+
/* 5-1-1. The loop bound is a variable that is mutated in the for loop. */
226+
variableModifiedInExpression(mutatingExpr,
227+
loopBound.(VariableAccess).getTarget().getAnAccess())
228+
or
229+
/* 5-1-2. The loop bound is not a variable access and is not a constant expression. */
230+
not loopBound instanceof VariableAccess and not loopBound.isConstant()
231+
)
232+
) and
233+
forLoopExpr = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
234+
message = "TODO"
235+
or
236+
/* 5-2. The mutating expression mutates the loop step. */
237+
exists(Expr loopStep | loopStep = getLoopStepOfForStmt(forLoop) |
238+
exists(Expr mutatingExpr |
239+
/* The mutating expression may be in the loop body. */
240+
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
241+
or
242+
/* The mutating expression may be in the loop updating expression. */
243+
mutatingExpr = forLoop.getUpdate().getAChild*()
244+
|
245+
/* 5-1-2. The loop step is a variable that is mutated in the for loop. */
246+
variableModifiedInExpression(mutatingExpr,
247+
loopStep.(VariableAccess).getTarget().getAnAccess())
248+
or
249+
/* 5-1-2. The loop bound is not a variable access and is not a constant expression. */
250+
not loopStep instanceof VariableAccess and not loopStep.isConstant()
251+
)
252+
) and
253+
forLoopExpr = getLoopStepOfForStmt(forLoop) and
254+
message = "TODO"
255+
or
256+
/*
257+
* 6. Any of the loop counter, loop bound, or a loop step is taken as a mutable reference
258+
* or its address to a mutable pointer.
259+
*/
246260

247-
exists(VariableAccess loopVariableAccessInCondition |
248-
(
249-
loopVariableAccessInCondition =
250-
forLoop.getCondition().(LegacyForLoopCondition).getLoopCounter() or
251-
loopVariableAccessInCondition = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() or
252-
loopVariableAccessInCondition = getLoopStepOfForStmt(forLoop)
261+
exists(VariableAccess loopVariableAccessInCondition |
262+
(
263+
loopVariableAccessInCondition =
264+
forLoop.getCondition().(LegacyForLoopCondition).getLoopCounter() or
265+
loopVariableAccessInCondition =
266+
forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() or
267+
loopVariableAccessInCondition = getLoopStepOfForStmt(forLoop)
268+
) and
269+
(
270+
loopVariableAssignedToNonConstPointerOrReferenceType(forLoop, loopVariableAccessInCondition)
271+
or
272+
loopVariablePassedAsArgumentToNonConstReferenceParameter(forLoop,
273+
loopVariableAccessInCondition)
274+
)
253275
) and
254-
(
255-
loopVariableAssignedToNonConstPointerOrReferenceType(forLoop, loopVariableAccessInCondition)
256-
or
257-
loopVariablePassedAsArgumentToNonConstReferenceParameter(forLoop,
258-
loopVariableAccessInCondition)
259-
)
276+
message = "TODO"
260277
)
261-
select forLoop, "TODO"
278+
select forLoop, message, forLoopExpr, "???"

0 commit comments

Comments
 (0)