Skip to content

Commit ef44566

Browse files
committed
Implement query for dependence on operator precedence
1 parent 6c4a79d commit ef44566

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

cpp/autosar/src/rules/M5-0-2/InsufficientUseOfParentheses.ql

+18-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,21 @@
1717
import cpp
1818
import codingstandards.cpp.autosar
1919

20-
from Expr e
21-
where
22-
not isExcluded(e, OrderOfEvaluationPackage::insufficientUseOfParenthesesQuery())
23-
select e, "Insufficient use of parenthesis in expression."
20+
class InsufficientlyParenthesizedExpr extends Expr {
21+
InsufficientlyParenthesizedExpr() {
22+
exists(BinaryOperation root, BinaryOperation child | child = this |
23+
root.getAnOperand() = child and
24+
root.getOperator() != child.getOperator() and
25+
not any(ParenthesisExpr pe).getExpr() = child
26+
)
27+
or
28+
exists(ConditionalExpr root, BinaryOperation child | child = this |
29+
root.getAnOperand() = child and
30+
not any(ParenthesisExpr pe).getExpr() = child
31+
)
32+
}
33+
}
34+
35+
from InsufficientlyParenthesizedExpr e
36+
where not isExcluded(e, OrderOfEvaluationPackage::insufficientUseOfParenthesesQuery())
37+
select e, "Dependence on operator precedence rules."
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
No expected results have yet been specified
1+
| test.cpp:40:8:40:13 | ... * ... | Dependence on operator precedence rules. |
2+
| test.cpp:41:19:41:24 | ... * ... | Dependence on operator precedence rules. |
3+
| test.cpp:42:8:42:13 | ... * ... | Dependence on operator precedence rules. |
4+
| test.cpp:42:17:42:22 | ... * ... | Dependence on operator precedence rules. |
5+
| test.cpp:48:8:48:15 | ... == ... | Dependence on operator precedence rules. |
6+
| test.cpp:49:26:49:32 | ... - ... | Dependence on operator precedence rules. |
7+
| test.cpp:50:8:50:15 | ... == ... | Dependence on operator precedence rules. |
8+
| test.cpp:50:24:50:30 | ... - ... | Dependence on operator precedence rules. |

cpp/autosar/test/rules/M5-0-2/test.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,21 @@ void f1() {
3131
int **l7;
3232
l1 = (*l7)[l2]; // NON_COMPLIANT[FALSE_NEGATIVE]
3333
char l8 = (char)(l1 + 1); // NON_COMPLIANT[FALSE_NEGATIVE]
34+
}
35+
36+
void test_insufficient_parentheses() {
37+
int l1, l2, l3;
38+
39+
l1 = (2 * l2) + (3 * l3); // COMPLIANT
40+
l1 = 2 * l2 + (3 * l3); // NON_COMPLIANT
41+
l1 = (2 * l2) + 3 * l3; // NON_COMPLIANT
42+
l1 = 2 * l2 + 3 * l3; // NON_COMPLIANT
43+
l1 = (2 * l2) + l3 + 1; // COMPLIANT
44+
l1 = (l2 + 1) - (l2 + l3); // COMPLIANT
45+
l1 = l2 + l3 + 1; // COMPLIANT
46+
47+
l1 = (l2 == l3) ? l2 : (l2 - l3); // COMPLIANT
48+
l1 = l2 == l3 ? l2 : (l2 - l3); // NON_COMPLIANT
49+
l1 = (l2 == l3) ? l2 : l2 - l3; // NON_COMPLIANT
50+
l1 = l2 == l3 ? l2 : l2 - l3; // NON_COMPLIANT
3451
}

0 commit comments

Comments
 (0)