Skip to content

Commit 6c7c258

Browse files
authored
Merge pull request #488 from github/lcartey/m5-3-3
M5-3-3: Excude binary operators.
2 parents e204fc4 + 230aa6e commit 6c7c258

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`M5-3-3` - `UnaryOperatorOverloaded.ql`:
2+
- Exclude binary user defined `operator&` from this rule.

cpp/autosar/src/rules/M5-3-3/UnaryOperatorOverloaded.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
import cpp
1515
import codingstandards.cpp.autosar
16+
import codingstandards.cpp.Operator
1617

17-
from Operator o
18-
where not isExcluded(o, OperatorsPackage::unaryOperatorOverloadedQuery()) and o.hasName("operator&")
18+
from UnaryAddressOfOperator o
19+
where not isExcluded(o, OperatorsPackage::unaryOperatorOverloadedQuery())
1920
select o, "The unary & operator overloaded."
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
| test.cpp:6:5:6:13 | operator& | The unary & operator overloaded. |
1+
| test.cpp:2:5:2:13 | operator& | The unary & operator overloaded. |
2+
| test.cpp:8:3:8:11 | operator& | The unary & operator overloaded. |
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/* The unary & operator shall not be overloaded */
2-
// do not defined it at all
3-
class A
4-
5-
{
6-
A operator&(); // NON_COMPLIANT
1+
class A {
2+
A operator&(); // NON_COMPLIANT - unary
3+
constexpr A operator&(const A rhs); // COMPLIANT - binary
74
};
5+
6+
class B {};
7+
8+
B operator&(B b); // NON_COMPLIANT - unary
9+
constexpr B operator&(const B lhs, const B rhs); // COMPLIANT - binary

cpp/common/src/codingstandards/cpp/Operator.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,22 @@ class UserOverloadedOperator extends Function {
265265
}
266266
}
267267

268+
/** A user defined operator address of operator (`&`). */
269+
class UnaryAddressOfOperator extends Operator {
270+
UnaryAddressOfOperator() {
271+
hasName("operator&") and
272+
(
273+
// If this is a member function, it needs to have zero arguments to be the unary addressof
274+
// operator
275+
if this instanceof MemberFunction
276+
then getNumberOfParameters() = 0
277+
else
278+
// Otherwise it needs one argument to be unary
279+
getNumberOfParameters() = 1
280+
)
281+
}
282+
}
283+
268284
private newtype TOperatorUse =
269285
TBuiltinOperatorUse(Operation op) or
270286
TOverloadedOperatorUse(FunctionCall call, Operator op) { op.getACallToThisFunction() = call }

0 commit comments

Comments
 (0)