|
18 | 18 |
|
19 | 19 | import cpp |
20 | 20 | import codingstandards.cpp.autosar |
| 21 | +import codingstandards.cpp.Operator |
| 22 | +import codingstandards.cpp.Type |
21 | 23 |
|
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 | | - ) |
| 24 | +class AllowedOperatorUse extends OperatorUse { |
| 25 | + AllowedOperatorUse() { |
| 26 | + this.getOperator() in ["[]", "=", "==", "!=", "<", "<=", ">", ">="] |
| 27 | + or |
| 28 | + this.(UnaryOperatorUse).getOperator() = "&" |
| 29 | + } |
47 | 30 | } |
48 | 31 |
|
49 | | -from Expr e, Expr operand |
| 32 | +from OperatorUse operatorUse, Access access, Enum enum |
50 | 33 | where |
51 | | - not isExcluded(e, ExpressionsPackage::enumUsedInArithmeticContextsQuery()) and |
| 34 | + not isExcluded(access, ExpressionsPackage::enumUsedInArithmeticContextsQuery()) and |
| 35 | + operatorUse.getAnOperand() = access and |
52 | 36 | ( |
53 | | - operand = getAnOperandOfAllowedOverloadedOperator(e) |
54 | | - or |
55 | | - operand = getAnOperandOfAllowedOperation(e) |
| 37 | + access.(EnumConstantAccess).getTarget().getDeclaringEnum() = enum or |
| 38 | + access.(VariableAccess).getType() = enum |
56 | 39 | ) 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" |
| 40 | + not operatorUse instanceof AllowedOperatorUse and |
| 41 | + // Enums that implement the BitmaskType trait are an exception. |
| 42 | + not enum instanceof BitmaskType |
| 43 | +select access, "Enum $@ is used as an operand of arithmetic operation.", enum, enum.getName() |
0 commit comments