Skip to content

Commit 033dc61

Browse files
authored
Merge pull request #545 from knewbury01/knewbury01/fix-375
M5-14-1: exclusion unevaluated contexts
2 parents d2cdde7 + 2757c3f commit 033dc61

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.SideEffect
2020
import codingstandards.cpp.sideeffect.DefaultEffects
21+
import codingstandards.cpp.Expr
2122

2223
from BinaryLogicalOperation op, Expr rhs
2324
where
2425
not isExcluded(op,
2526
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
2627
rhs = op.getRightOperand() and
27-
hasSideEffect(rhs)
28+
hasSideEffect(rhs) and
29+
not rhs instanceof UnevaluatedExprExtension
2830
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
| test.cpp:15:7:15:14 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:15:12:15:14 | ... ++ | right-hand operand |
22
| test.cpp:18:7:18:21 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:18:13:18:20 | ... == ... | right-hand operand |
33
| test.cpp:21:7:21:15 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:21:12:21:13 | call to f1 | right-hand operand |
4+
| test.cpp:40:7:40:41 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:40:26:40:26 | call to operator== | right-hand operand |

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

+16
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@ 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+
#include <typeinfo>
33+
34+
void f6() {
35+
if (1 && sizeof(f4())) {
36+
} // COMPLIANT - sizeof operands not evaluated
37+
if (1 &&noexcept(f4()) &&noexcept(f4())) {
38+
} // COMPLIANT - noexcept operands not evaluated
39+
40+
if (1 || (typeid(f5()) == typeid(f4()))) {
41+
} // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is
2642
}

cpp/common/src/codingstandards/cpp/Expr.qll

+14
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,17 @@ module MisraExpr {
189189
CValue() { isCValue(this) }
190190
}
191191
}
192+
193+
/**
194+
* an operator that does not evaluate its operand
195+
*/
196+
class UnevaluatedExprExtension extends Expr {
197+
UnevaluatedExprExtension() {
198+
this.getAChild().isUnevaluated()
199+
or
200+
exists(FunctionCall declval |
201+
declval.getTarget().hasQualifiedName("std", "declval") and
202+
declval.getAChild() = this
203+
)
204+
}
205+
}

cpp/common/test/includes/standard-library/typeinfo.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace std {
44
struct type_info {
55
const char *name() const noexcept;
66
std::size_t hash_code() const noexcept;
7+
bool operator==(const type_info &rhs) const;
78
};
89
} // namespace std

0 commit comments

Comments
 (0)