Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-01-14-m5-3-3-exclude-binary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`M5-3-3` - `UnaryOperatorOverloaded.ql`:
- Exclude binary user defined `operator&` from this rule.
5 changes: 3 additions & 2 deletions cpp/autosar/src/rules/M5-3-3/UnaryOperatorOverloaded.ql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.Operator

from Operator o
where not isExcluded(o, OperatorsPackage::unaryOperatorOverloadedQuery()) and o.hasName("operator&")
from UnaryAddressOfOperator o
where not isExcluded(o, OperatorsPackage::unaryOperatorOverloadedQuery())
select o, "The unary & operator overloaded."
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
| test.cpp:6:5:6:13 | operator& | The unary & operator overloaded. |
| test.cpp:2:5:2:13 | operator& | The unary & operator overloaded. |
| test.cpp:8:3:8:11 | operator& | The unary & operator overloaded. |
14 changes: 8 additions & 6 deletions cpp/autosar/test/rules/M5-3-3/test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* The unary & operator shall not be overloaded */
// do not defined it at all
class A

{
A operator&(); // NON_COMPLIANT
class A {
A operator&(); // NON_COMPLIANT - unary
constexpr A operator&(const A rhs); // COMPLIANT - binary
};

class B {};

B operator&(B b); // NON_COMPLIANT - unary
constexpr B operator&(const B lhs, const B rhs); // COMPLIANT - binary
16 changes: 16 additions & 0 deletions cpp/common/src/codingstandards/cpp/Operator.qll
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ class UserOverloadedOperator extends Function {
}
}

/** A user defined operator address of operator (`&`). */
class UnaryAddressOfOperator extends Operator {
UnaryAddressOfOperator() {
hasName("operator&") and
(
// If this is a member function, it needs to have zero arguments to be the unary addressof
// operator
if this instanceof MemberFunction
then getNumberOfParameters() = 0
else
// Otherwise it needs one argument to be unary
getNumberOfParameters() = 1
)
}
}

private newtype TOperatorUse =
TBuiltinOperatorUse(Operation op) or
TOverloadedOperatorUse(FunctionCall call, Operator op) { op.getACallToThisFunction() = call }
Expand Down