Skip to content

Commit 9298ce5

Browse files
committed
Address mixup between the unary and binary & operator
1 parent 8b9f677 commit 9298ce5

File tree

5 files changed

+213
-144
lines changed

5 files changed

+213
-144
lines changed

cpp/autosar/src/rules/A4-5-1/EnumUsedInArithmeticContexts.ql

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,23 @@
1818

1919
import cpp
2020
import codingstandards.cpp.autosar
21+
import codingstandards.cpp.Operator
2122

22-
/*
23-
* Get an operand to all overloaded operator member functions, except:
24-
* operator[]
25-
* operator=
26-
* operator==
27-
* operator!=
28-
* operator&
29-
* operator<
30-
* operator<=
31-
* operator>
32-
* operator>=
33-
*/
34-
35-
Expr getAnOperandOfAllowedOverloadedOperator(FunctionCall fc) {
36-
fc.getAnArgument() = result and
37-
fc.getTarget().getName().regexpMatch("operator(?!\\[]$|=$|==$|!=$|&$|<$|<=$|>$|>=$).+")
38-
}
39-
40-
Expr getAnOperandOfAllowedOperation(Operation o) {
41-
o.getAnOperand() = result and
42-
not (
43-
o instanceof AssignExpr or
44-
o instanceof BitwiseAndExpr or
45-
o instanceof ComparisonOperation
46-
)
23+
class AllowedOperatorUse extends OperatorUse {
24+
AllowedOperatorUse() {
25+
this.getOperator() in ["[]", "=", "==", "!=", "<", "<=", ">", ">="]
26+
or
27+
this.(UnaryOperatorUse).getOperator() = "&"
28+
}
4729
}
4830

49-
from Expr e, Expr operand
31+
from OperatorUse operatorUse, Access access, Enum enum
5032
where
51-
not isExcluded(e, ExpressionsPackage::enumUsedInArithmeticContextsQuery()) and
33+
not isExcluded(access, ExpressionsPackage::enumUsedInArithmeticContextsQuery()) and
34+
operatorUse.getAnOperand() = access and
5235
(
53-
operand = getAnOperandOfAllowedOverloadedOperator(e)
54-
or
55-
operand = getAnOperandOfAllowedOperation(e)
36+
access.(EnumConstantAccess).getTarget().getDeclaringEnum() = enum or
37+
access.(VariableAccess).getType() = enum
5638
) and
57-
(
58-
operand instanceof EnumConstantAccess or
59-
operand.(VariableAccess).getType() instanceof Enum
60-
)
61-
select e, "Enum $@ is used as an operand of arithmetic operation.", operand, "expression"
39+
not operatorUse instanceof AllowedOperatorUse
40+
select access, "Enum $@ is used as an operand of arithmetic operation.", enum, enum.getName()

0 commit comments

Comments
 (0)