Skip to content

Commit 338c99c

Browse files
committed
Reinforce A5-2-6
1 parent 1cc399b commit 338c99c

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

cpp/autosar/src/rules/A5-2-6/OperandsOfALogicalAndOrNotParenthesized.ql

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
import cpp
1818
import codingstandards.cpp.autosar
1919

20-
from BinaryLogicalOperation op, BinaryOperation binop
20+
from BinaryLogicalOperation op, Expr operand
2121
where
2222
not isExcluded(op, OrderOfEvaluationPackage::operandsOfALogicalAndOrNotParenthesizedQuery()) and
23-
op.getAnOperand() = binop and
24-
not exists(ParenthesisExpr p | p = binop.getFullyConverted()) and
25-
// Exclude binary operations expanded by a macro.
26-
not binop.isInMacroExpansion()
27-
select op, "Binary $@ operand of logical operation is not parenthesized.", binop, "operator"
23+
operand = op.getAnOperand() and
24+
/* The operand is a built-in arithmetic/logic binary operation */
25+
if operand instanceof BinaryOperation
26+
then
27+
not exists(ParenthesisExpr p | p = operand.getFullyConverted()) and
28+
// Exclude binary operations expanded by a macro.
29+
not operand.isInMacroExpansion()
30+
else
31+
/* The operand should not be a field access operation */
32+
not operand instanceof FieldAccess
33+
select op, "Binary $@ operand of logical operation is not parenthesized.", operand, "operator"

cpp/autosar/test/rules/A5-2-6/test.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
extern void f1();
1+
// TODO: remove function body
2+
extern void f1(){};
23
void f2(int p1, int p2) {
34
if (p1 > 0 && p1 < 10) { // NON_COMPLIANT
45
f1();
@@ -20,15 +21,18 @@ void f2(int p1, int p2) {
2021
int x;
2122
} sample;
2223

23-
if ((p1 > 0 ||
24-
sample.x)) { // COMPLIANT: struct member accessors (.) are excluded
24+
if ((p1 > 0) ||
25+
sample.x) { // COMPLIANT: struct member accessors (.) are excluded
2526
f1();
2627
}
2728

2829
Sample *sample_ptr = &sample;
2930

30-
if ((p1 > 0 || sample_ptr->x)) { // COMPLIANT: struct member accessors with
31+
if ((p1 > 0) || sample_ptr->x) { // COMPLIANT: struct member accessors with
3132
// dereference (->) are excluded
3233
f1();
3334
}
34-
}
35+
}
36+
37+
// TODO: remove this
38+
int main() { return 0; }

0 commit comments

Comments
 (0)