Skip to content

Commit 01216fa

Browse files
committed
Add more candidate exprs and remove duplicate reportings on compound mutating exprs
1 parent 25e29e4 commit 01216fa

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

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

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,57 @@ private newtype TAlertType =
194194
)
195195
} or
196196
/* 5-1-1. The loop bound is a variable that is mutated in the for loop. */
197-
TLoopBoundIsMutatedVariableAccess(ForStmt forLoop, Expr loopBound, Expr mutatingExpr) {
198-
loopBound = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
199-
(
200-
/* The mutating expression may be in the loop body. */
201-
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
202-
or
203-
/* The mutating expression may be in the loop updating expression. */
204-
mutatingExpr = forLoop.getUpdate().getAChild*()
205-
) and
206-
variableModifiedInExpression(mutatingExpr, loopBound.(VariableAccess).getTarget().getAnAccess())
197+
TLoopBoundIsMutatedVariableAccess(
198+
ForStmt forLoop, VariableAccess variableAccess, VariableAccess mutatedVariableAccess
199+
) {
200+
exists(Expr loopBoundExpr, Expr mutatingExpr |
201+
loopBoundExpr = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
202+
(
203+
/* 1. The mutating expression may be in the loop body. */
204+
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
205+
or
206+
/* 2. The mutating expression may be in the loop updating expression. */
207+
mutatingExpr = forLoop.getUpdate().getAChild*()
208+
or
209+
/* 3. The mutating expression may be in the loop condition */
210+
mutatingExpr = forLoop.getCondition().getAChild*()
211+
or
212+
/* 4. The mutating expression may be in the loop initializer */
213+
mutatingExpr = forLoop.getInitialization().getAChild*()
214+
) and
215+
variableAccess = loopBoundExpr.getAChild*() and
216+
mutatedVariableAccess = variableAccess.getTarget().getAnAccess() and
217+
variableModifiedInExpression(mutatingExpr, mutatedVariableAccess)
218+
)
207219
} or
208220
/* 5-1-2. The loop bound is not a variable access nor a constant expression. */
209221
TLoopBoundIsNonConstExpr(ForStmt forLoop, Expr loopBound) {
210222
loopBound = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
211223
(not loopBound instanceof VariableAccess and not loopBound.isConstant())
212224
} or
213225
/* 5-2-1. The loop step is a variable that is mutated in the for loop. */
214-
TLoopStepIsMutatedVariableAccess(ForStmt forLoop, Expr loopStep, Expr mutatingExpr) {
215-
loopStep = getLoopStepOfForStmt(forLoop) and
216-
(
217-
/* The mutating expression may be in the loop body. */
218-
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
219-
or
220-
/* The mutating expression may be in the loop updating expression. */
221-
mutatingExpr = forLoop.getUpdate().getAChild*()
222-
) and
223-
variableModifiedInExpression(mutatingExpr, loopStep.(VariableAccess).getTarget().getAnAccess())
226+
TLoopStepIsMutatedVariableAccess(
227+
ForStmt forLoop, VariableAccess variableAccess, VariableAccess mutatedVariableAccess
228+
) {
229+
exists(Expr loopStepExpr, Expr mutatingExpr |
230+
loopStepExpr = getLoopStepOfForStmt(forLoop) and
231+
(
232+
/* 1. The mutating expression may be in the loop body. */
233+
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
234+
or
235+
/* 2. The mutating expression may be in the loop updating expression. */
236+
mutatingExpr = forLoop.getUpdate().getAChild*()
237+
or
238+
/* 3. The mutating expression may be in the loop condition */
239+
mutatingExpr = forLoop.getCondition().getAChild*()
240+
or
241+
/* 4. The mutating expression may be in the loop initializer */
242+
mutatingExpr = forLoop.getInitialization().getAChild*()
243+
) and
244+
variableAccess = loopStepExpr.getAChild*() and
245+
mutatedVariableAccess = variableAccess.getTarget().getAnAccess() and
246+
variableModifiedInExpression(mutatingExpr, mutatedVariableAccess)
247+
)
224248
} or
225249
/* 5-2-2. The loop step is not a variable access nor a constant expression. */
226250
TLoopStepIsNonConstExpr(ForStmt forLoop, Expr loopStep) {
@@ -244,13 +268,15 @@ private newtype TAlertType =
244268
* 6-2. The loop bound is taken as a mutable reference or its address to a mutable pointer.
245269
*/
246270

247-
TLoopBoundIsTakenNonConstAddress(ForStmt forLoop, Expr loopVariableAccessInCondition) {
248-
loopVariableAccessInCondition = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
249-
(
250-
loopVariableAssignedToNonConstPointerOrReferenceType(forLoop, loopVariableAccessInCondition)
251-
or
252-
loopVariablePassedAsArgumentToNonConstReferenceParameter(forLoop,
253-
loopVariableAccessInCondition)
271+
TLoopBoundIsTakenNonConstAddress(ForStmt forLoop, Expr loopBoundExpr) {
272+
loopBoundExpr = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
273+
exists(VariableAccess variableAccess |
274+
variableAccess = loopBoundExpr.getAChild*() and
275+
(
276+
loopVariableAssignedToNonConstPointerOrReferenceType(forLoop, variableAccess)
277+
or
278+
loopVariablePassedAsArgumentToNonConstReferenceParameter(forLoop, variableAccess)
279+
)
254280
)
255281
} or
256282
/*

0 commit comments

Comments
 (0)