Skip to content

Commit 55b8476

Browse files
committed
Split cases 5-1 and 5-2
1 parent 18daff7 commit 55b8476

File tree

1 file changed

+66
-34
lines changed

1 file changed

+66
-34
lines changed

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

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ private newtype TAlertType =
198198
loopCounterType.getSize() < loopBoundType.getSize()
199199
)
200200
} or
201-
/*
202-
* 5-1. The loop bound is a non-const expression, or a variable that is mutated in the for loop.
203-
*/
204-
205-
TLoopBoundIsNonConstExprOrMutatedVariableAccess(ForStmt forLoop, Expr loopBound, Expr mutatingExpr) {
201+
/* 5-1-1. The loop bound is a variable that is mutated in the for loop. */
202+
TLoopBoundIsMutatedVariableAccess(ForStmt forLoop, Expr loopBound, Expr mutatingExpr) {
206203
loopBound = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
207204
(
208205
/* The mutating expression may be in the loop body. */
@@ -211,20 +208,22 @@ private newtype TAlertType =
211208
/* The mutating expression may be in the loop updating expression. */
212209
mutatingExpr = forLoop.getUpdate().getAChild*()
213210
) and
214-
/* 5-1-1. The loop bound is a variable that is mutated in the for loop. */
211+
variableModifiedInExpression(mutatingExpr, loopBound.(VariableAccess).getTarget().getAnAccess())
212+
} or
213+
/* 5-1-2. The loop bound is not a variable access nor a constant expression. */
214+
TLoopBoundIsNonConstExpr(ForStmt forLoop, Expr loopBound, Expr mutatingExpr) {
215+
loopBound = forLoop.getCondition().(LegacyForLoopCondition).getLoopBound() and
215216
(
216-
variableModifiedInExpression(mutatingExpr,
217-
loopBound.(VariableAccess).getTarget().getAnAccess())
217+
/* The mutating expression may be in the loop body. */
218+
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
218219
or
219-
/* 5-1-2. The loop bound is not a variable access nor a constant expression. */
220-
not loopBound instanceof VariableAccess and not loopBound.isConstant()
221-
)
220+
/* The mutating expression may be in the loop updating expression. */
221+
mutatingExpr = forLoop.getUpdate().getAChild*()
222+
) and
223+
(not loopBound instanceof VariableAccess and not loopBound.isConstant())
222224
} or
223-
/*
224-
* 5-2. The loop step is a non-const expression, or are variable that is mutated in the for loop.
225-
*/
226-
227-
TLoopStepIsNonConstExprOrMutatedVariableAccess(ForStmt forLoop, Expr loopStep, Expr mutatingExpr) {
225+
/* 5-2-1. The loop step is a variable that is mutated in the for loop. */
226+
TLoopStepIsMutatedVariableAccess(ForStmt forLoop, Expr loopStep, Expr mutatingExpr) {
228227
loopStep = getLoopStepOfForStmt(forLoop) and
229228
(
230229
/* The mutating expression may be in the loop body. */
@@ -233,13 +232,19 @@ private newtype TAlertType =
233232
/* The mutating expression may be in the loop updating expression. */
234233
mutatingExpr = forLoop.getUpdate().getAChild*()
235234
) and
235+
variableModifiedInExpression(mutatingExpr, loopStep.(VariableAccess).getTarget().getAnAccess())
236+
} or
237+
/* 5-2-2. The loop step is not a variable access nor a constant expression. */
238+
TLoopStepIsNonConstExpr(ForStmt forLoop, Expr loopStep, Expr mutatingExpr) {
239+
loopStep = getLoopStepOfForStmt(forLoop) and
236240
(
237-
/* 5-2-2. The loop step is a variable that is mutated in the for loop. */
238-
variableModifiedInExpression(mutatingExpr, loopStep.(VariableAccess).getTarget().getAnAccess())
241+
/* The mutating expression may be in the loop body. */
242+
mutatingExpr = forLoop.getStmt().getChildStmt().getAChild*()
239243
or
240-
/* 5-2-2. The loop step is not a variable access nor a constant expression. */
241-
not loopStep instanceof VariableAccess and not loopStep.isConstant()
242-
)
244+
/* The mutating expression may be in the loop updating expression. */
245+
mutatingExpr = forLoop.getUpdate().getAChild*()
246+
) and
247+
(not loopStep instanceof VariableAccess and not loopStep.isConstant())
243248
} or
244249
/*
245250
* 6-1. The loop counter is taken as a mutable reference or its address to a mutable pointer.
@@ -292,8 +297,10 @@ class AlertType extends TAlertType {
292297
this = TNoRelationalOperatorInLoopCondition(result, _) or
293298
this = TLoopCounterMutatedInLoopBody(result, _) or
294299
this = TLoopCounterSmallerThanLoopBound(result, _) or
295-
this = TLoopBoundIsNonConstExprOrMutatedVariableAccess(result, _, _) or
296-
this = TLoopStepIsNonConstExprOrMutatedVariableAccess(result, _, _) or
300+
this = TLoopBoundIsMutatedVariableAccess(result, _, _) or
301+
this = TLoopStepIsNonConstExpr(result, _, _) or
302+
this = TLoopBoundIsMutatedVariableAccess(result, _, _) or
303+
this = TLoopStepIsNonConstExpr(result, _, _) or
297304
this = TLoopCounterIsTakenNonConstAddress(result, _) or
298305
this = TLoopBoundIsTakenNonConstAddress(result, _) or
299306
this = TLoopStepIsTakenNonConstAddress(result, _)
@@ -314,9 +321,13 @@ class AlertType extends TAlertType {
314321
result = forLoopCondition.getLoopCounter()
315322
)
316323
or
317-
this = TLoopBoundIsNonConstExprOrMutatedVariableAccess(_, result, _)
324+
this = TLoopBoundIsNonConstExpr(_, result, _)
325+
or
326+
this = TLoopBoundIsMutatedVariableAccess(_, result, _)
318327
or
319-
this = TLoopStepIsNonConstExprOrMutatedVariableAccess(_, result, _)
328+
this = TLoopStepIsNonConstExpr(_, result, _)
329+
or
330+
this = TLoopStepIsMutatedVariableAccess(_, result, _)
320331
or
321332
this = TLoopCounterIsTakenNonConstAddress(_, result)
322333
or
@@ -341,10 +352,16 @@ class AlertType extends TAlertType {
341352
this = TLoopCounterSmallerThanLoopBound(_, _) and
342353
result = "counter variable"
343354
or
344-
this = TLoopBoundIsNonConstExprOrMutatedVariableAccess(_, _, _) and
355+
this = TLoopBoundIsMutatedVariableAccess(_, _, _) and
356+
result = "loop bound"
357+
or
358+
this = TLoopBoundIsNonConstExpr(_, _, _) and
345359
result = "loop bound"
346360
or
347-
this = TLoopStepIsNonConstExprOrMutatedVariableAccess(_, _, _) and
361+
this = TLoopStepIsMutatedVariableAccess(_, _, _) and
362+
result = "loop step"
363+
or
364+
this = TLoopStepIsNonConstExpr(_, _, _) and
348365
result = "loop step"
349366
or
350367
this = TLoopCounterIsTakenNonConstAddress(_, _) and
@@ -374,10 +391,16 @@ class AlertType extends TAlertType {
374391
this = TLoopCounterSmallerThanLoopBound(_, _) and
375392
result = "The $@ has a smaller type than that of the $@."
376393
or
377-
this = TLoopBoundIsNonConstExprOrMutatedVariableAccess(_, _, _) and
394+
this = TLoopBoundIsNonConstExpr(_, _, _) and
395+
result = "The $@ is a non-const expression, or a variable that is $@ in the loop."
396+
or
397+
this = TLoopBoundIsMutatedVariableAccess(_, _, _) and
378398
result = "The $@ is a non-const expression, or a variable that is $@ in the loop."
379399
or
380-
this = TLoopStepIsNonConstExprOrMutatedVariableAccess(_, _, _) and
400+
this = TLoopStepIsNonConstExpr(_, _, _) and
401+
result = "The $@ is a non-const expression, or a variable that is $@ in the loop."
402+
or
403+
this = TLoopStepIsMutatedVariableAccess(_, _, _) and
381404
result = "The $@ is a non-const expression, or a variable that is $@ in the loop."
382405
or
383406
this = TLoopCounterIsTakenNonConstAddress(_, _) and
@@ -402,9 +425,13 @@ class AlertType extends TAlertType {
402425
result = forLoopCondition.getLoopBound()
403426
)
404427
or
405-
this = TLoopBoundIsNonConstExprOrMutatedVariableAccess(_, _, result)
428+
this = TLoopBoundIsNonConstExpr(_, _, result)
429+
or
430+
this = TLoopBoundIsMutatedVariableAccess(_, _, result)
406431
or
407-
this = TLoopStepIsNonConstExprOrMutatedVariableAccess(_, _, result)
432+
this = TLoopStepIsNonConstExpr(_, _, result)
433+
or
434+
this = TLoopStepIsMutatedVariableAccess(_, _, result)
408435
or
409436
this = TLoopCounterIsTakenNonConstAddress(_, result) // Throwaway
410437
or
@@ -426,10 +453,16 @@ class AlertType extends TAlertType {
426453
this = TLoopCounterSmallerThanLoopBound(_, _) and
427454
result = "loop bound"
428455
or
429-
this = TLoopBoundIsNonConstExprOrMutatedVariableAccess(_, _, _) and
456+
this = TLoopBoundIsNonConstExpr(_, _, _) and
430457
result = "mutated"
431458
or
432-
this = TLoopStepIsNonConstExprOrMutatedVariableAccess(_, _, _) and
459+
this = TLoopBoundIsMutatedVariableAccess(_, _, _) and
460+
result = "mutated"
461+
or
462+
this = TLoopStepIsNonConstExpr(_, _, _) and
463+
result = "mutated"
464+
or
465+
this = TLoopStepIsMutatedVariableAccess(_, _, _) and
433466
result = "mutated"
434467
or
435468
this = TLoopCounterIsTakenNonConstAddress(_, _) and
@@ -449,4 +482,3 @@ from AlertType alert
449482
where not isExcluded(alert.asElement(), StatementsPackage::legacyForStatementsShouldBeSimpleQuery())
450483
select alert, alert.getMessage(), alert.getLinkTarget1(), alert.getLinkText1(),
451484
alert.getLinkTarget2(), alert.getLinkText2()
452-

0 commit comments

Comments
 (0)