Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,12 @@ import codingstandards.cpp.autosar
import codingstandards.cpp.SideEffect
import codingstandards.cpp.sideeffect.DefaultEffects

/**
* an operator that does not evaluate its operand
* `decltype` also has a non evaluated operand but cannot be used in a `BinaryLogicalOperation`
*/
class UnevaluatedOperand extends Expr {
Expr operator;

UnevaluatedOperand() {
exists(SizeofExprOperator op | op.getExprOperand() = this |
not this.getUnderlyingType().(ArrayType).hasArraySize() and
operator = op
)
or
exists(NoExceptExpr e |
e.getExpr() = this and
operator = e
)
or
exists(TypeidOperator t |
t.getExpr() = this and
operator = t
)
or
exists(FunctionCall declval |
declval.getTarget().hasQualifiedName("std", "declval") and
declval.getAChild() = this and
operator = declval
)
}

Expr getOp() { result = operator }
}

from BinaryLogicalOperation op, Expr rhs
where
not isExcluded(op,
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
rhs = op.getRightOperand() and
hasSideEffect(rhs) and
not exists(UnevaluatedOperand un | un.getOp() = rhs)
not rhs.(NoExceptExpr).getExpr().isUnevaluated() and
not rhs.(SizeofExprOperator).getExprOperand().isUnevaluated()
Comment thread
knewbury01 marked this conversation as resolved.
Outdated
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| 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 |
| 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 |
| 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 |
| 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 |
12 changes: 9 additions & 3 deletions cpp/autosar/test/rules/M5-14-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ int g1 = 0;
int f4() { return g1++; }
int f5() { return 1; }

#include <typeinfo>

void f6() {
if (noexcept(f5()) &&noexcept(
f4())) { // COMPLIANT - noexcept operands not evaluated
}
if (1 && sizeof(f4())) {
} // COMPLIANT - sizeof operands not evaluated
if (1 &&noexcept(f4()) &&noexcept(f4())) {
} // COMPLIANT - noexcept operands not evaluated

if (1 || (typeid(f5()) == typeid(f4()))) {
} // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is
}
1 change: 1 addition & 0 deletions cpp/common/test/includes/standard-library/typeinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace std {
struct type_info {
const char *name() const noexcept;
std::size_t hash_code() const noexcept;
bool operator==(const type_info &rhs) const;
};
} // namespace std