Skip to content

Commit 1daf1d5

Browse files
committed
M5-14-1: use lib implementation, improve testcase
1 parent 38b29ca commit 1daf1d5

File tree

4 files changed

+13
-37
lines changed

4 files changed

+13
-37
lines changed

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,12 @@ 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-
29-
UnevaluatedOperand() {
30-
exists(SizeofExprOperator op | op.getExprOperand() = this |
31-
not this.getUnderlyingType().(ArrayType).hasArraySize() and
32-
operator = op
33-
)
34-
or
35-
exists(NoExceptExpr e |
36-
e.getExpr() = this and
37-
operator = e
38-
)
39-
or
40-
exists(TypeidOperator t |
41-
t.getExpr() = this and
42-
operator = t
43-
)
44-
or
45-
exists(FunctionCall declval |
46-
declval.getTarget().hasQualifiedName("std", "declval") and
47-
declval.getAChild() = this and
48-
operator = declval
49-
)
50-
}
51-
52-
Expr getOp() { result = operator }
53-
}
54-
5522
from BinaryLogicalOperation op, Expr rhs
5623
where
5724
not isExcluded(op,
5825
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
5926
rhs = op.getRightOperand() and
6027
hasSideEffect(rhs) and
61-
not exists(UnevaluatedOperand un | un.getOp() = rhs)
28+
not rhs.(NoExceptExpr).getExpr().isUnevaluated() and
29+
not rhs.(SizeofExprOperator).getExprOperand().isUnevaluated()
6230
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ int g1 = 0;
2929
int f4() { return g1++; }
3030
int f5() { return 1; }
3131

32+
#include <typeinfo>
33+
3234
void f6() {
33-
if (noexcept(f5()) &&noexcept(
34-
f4())) { // COMPLIANT - noexcept operands not evaluated
35-
}
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
3642
}

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

Lines changed: 1 addition & 0 deletions
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)