Skip to content

Commit 3de4838

Browse files
committed
Rule 13.2: Improve performance
Avoid expensive cross-products on the ConditionalExpr case, and filter by partial expressions earlier in the predicate set.
1 parent f0d8026 commit 3de4838

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

c/misra/src/rules/RULE-13-2/UnsequencedSideEffects.ql

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VariableEffectOrAccess extends Expr {
2727
pragma[noinline]
2828
predicate partOfFullExpr(VariableEffectOrAccess e, FullExpr fe) {
2929
(
30-
e.(VariableEffect).getAnAccess() = fe.getAChild+()
30+
exists(VariableEffect ve | e = ve and ve.getAnAccess() = fe.getAChild+() and not ve.isPartial())
3131
or
3232
e.(VariableAccess) = fe.getAChild+()
3333
)
@@ -154,6 +154,24 @@ int getOperandIndex(LeftRightOperation binop, Expr operand) {
154154
)
155155
}
156156

157+
predicate inConditionalThen(ConditionalExpr ce, Expr e) {
158+
e = ce.getThen()
159+
or
160+
exists(Expr parent |
161+
inConditionalThen(ce, parent) and
162+
parent.getAChild() = e
163+
)
164+
}
165+
166+
predicate inConditionalElse(ConditionalExpr ce, Expr e) {
167+
e = ce.getElse()
168+
or
169+
exists(Expr parent |
170+
inConditionalElse(ce, parent) and
171+
parent.getAChild() = e
172+
)
173+
}
174+
157175
from
158176
ConstituentExprOrdering orderingConfig, FullExpr fullExpr, VariableEffect variableEffect1,
159177
VariableAccess va1, VariableAccess va2, Locatable placeHolder, string label
@@ -219,8 +237,6 @@ where
219237
not variableEffect1.getAChild+() = va2
220238
) and
221239
// Both are evaluated
222-
not exists(ConditionalExpr ce |
223-
ce.getThen().getAChild*() = va1 and ce.getElse().getAChild*() = va2
224-
)
240+
not exists(ConditionalExpr ce | inConditionalThen(ce, va1) and inConditionalElse(ce, va2))
225241
select fullExpr, "The expression contains unsequenced $@ to $@ and $@ to $@.", variableEffect1,
226242
"side effect", va1, va1.getTarget().getName(), placeHolder, label, va2, va2.getTarget().getName()

0 commit comments

Comments
 (0)