Skip to content

Commit 361d3d1

Browse files
committed
M5-14-1: exclusion unevaluated contexts
1 parent d087031 commit 361d3d1

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`:
2+
- Fix FP reported in #375. Addresses incorrect detection of side effects in unevaluated contexts.

cpp/autosar/src/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,39 @@ import codingstandards.cpp.autosar
1919
import codingstandards.cpp.SideEffect
2020
import codingstandards.cpp.sideeffect.DefaultEffects
2121

22+
/**
23+
* an operator that does not evaluate its operand
24+
* `decltype` also has a non evaluated operand but cannot be used in a `BinaryLogicalOperation`
25+
*/
26+
class UnevaluatedOperand extends Expr {
27+
Expr operator;
28+
UnevaluatedOperand() {
29+
exists(SizeofExprOperator op | op.getExprOperand() = this |
30+
not this.getUnderlyingType().(ArrayType).hasArraySize()
31+
and operator = op
32+
)
33+
or
34+
exists(NoExceptExpr e | e.getExpr() = this
35+
and operator = e)
36+
or
37+
exists(TypeidOperator t | t.getExpr() = this
38+
and operator = t)
39+
or
40+
exists(FunctionCall declval | declval.getTarget().hasQualifiedName("std", "declval")
41+
and declval.getAChild() = this
42+
and operator = declval)
43+
}
44+
45+
Expr getOp(){
46+
result = operator
47+
}
48+
}
49+
2250
from BinaryLogicalOperation op, Expr rhs
2351
where
2452
not isExcluded(op,
2553
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
2654
rhs = op.getRightOperand() and
2755
hasSideEffect(rhs)
56+
and not exists(UnevaluatedOperand un | un.getOp() = rhs)
2857
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"

cpp/autosar/test/rules/M5-14-1/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ void f3(bool b) {
2323

2424
if (b || f2()) { // COMPLIANT, f2 has local side-effects
2525
}
26+
}
27+
28+
int g1 = 0;
29+
int f4() { return g1++; }
30+
int f5() { return 1; }
31+
32+
void f6() {
33+
if (noexcept(f5()) &&noexcept(
34+
f4())) { // COMPLIANT - noexcept operands not evaluated
35+
}
2636
}

0 commit comments

Comments
 (0)