|
18 | 18 |
|
19 | 19 | import cpp
|
20 | 20 | import codingstandards.cpp.autosar
|
| 21 | +import codingstandards.cpp.Operator |
21 | 22 |
|
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 | + } |
47 | 29 | }
|
48 | 30 |
|
49 |
| -from Expr e, Expr operand |
| 31 | +from OperatorUse operatorUse, Access access, Enum enum |
50 | 32 | where
|
51 |
| - not isExcluded(e, ExpressionsPackage::enumUsedInArithmeticContextsQuery()) and |
| 33 | + not isExcluded(access, ExpressionsPackage::enumUsedInArithmeticContextsQuery()) and |
| 34 | + operatorUse.getAnOperand() = access and |
52 | 35 | (
|
53 |
| - operand = getAnOperandOfAllowedOverloadedOperator(e) |
54 |
| - or |
55 |
| - operand = getAnOperandOfAllowedOperation(e) |
| 36 | + access.(EnumConstantAccess).getTarget().getDeclaringEnum() = enum or |
| 37 | + access.(VariableAccess).getType() = enum |
56 | 38 | ) 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