Skip to content

A8-4-7: exclude user defined operators and move constructors #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 6, 2024
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
3 changes: 3 additions & 0 deletions change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`A8-4-7` - `InParametersForCheapToCopyTypesNotPassedByValue.ql`, `InParametersForNotCheapToCopyTypesNotPassedByReference.ql`:
- Fixes #397. Exclude user defined operators and move constructors.`
- Exclude parameters for instantiated templates because the declaration location of the function does not contain enough information about the type used in the instantiation to make an actionable alert.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import cpp
import codingstandards.cpp.autosar
import TriviallySmallType
import codingstandards.cpp.CommonTypes as CommonTypes
import codingstandards.cpp.Class

/*
* For the purposes of this rule, "cheap to copy" is defined as a trivially copyable type that is no
Expand All @@ -34,8 +35,10 @@ where
) and
t.isConst() and
not exists(CatchBlock cb | cb.getParameter() = v) and
not exists(CopyConstructor cc | cc.getAParameter() = v) and
not v.isFromUninstantiatedTemplate(_)
not exists(SpecialMemberFunction cc | cc.getAParameter() = v) and
not exists(Operator op | op.getAParameter() = v) and
not v.isFromUninstantiatedTemplate(_) and
not v.isFromTemplateInstantiation(_)
select v,
"Parameter " + v.getName() + " is the trivially copyable type " + t.getName() +
" but it is passed by reference instead of by value."
"Parameter '" + v.getName() + "' is the trivially copyable type '" + t.getName() +
"' but it is passed by reference instead of by value."
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ where
not v.getType() instanceof TriviallySmallType and
not v.getType().getUnderlyingType() instanceof ReferenceType and
not exists(CatchBlock cb | cb.getParameter() = v) and
not v.isFromUninstantiatedTemplate(_)
not v.isFromUninstantiatedTemplate(_) and
not v.isFromTemplateInstantiation(_)
select v,
"Parameter " + v.getName() +
" is the trivially non-copyable type $@ but it is passed by value instead of by reference.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| test.cpp:20:19:20:21 | f5a | Parameter f5a is the trivially copyable type const S1 but it is passed by reference instead of by value. |
| test.cpp:20:19:20:21 | f5a | Parameter 'f5a' is the trivially copyable type 'const S1' but it is passed by reference instead of by value. |
8 changes: 8 additions & 0 deletions cpp/autosar/test/rules/A8-4-7/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ inline S1 Value(size_t n, const char *data) {} // COMPLIANT
struct A {
int n;
A(const A &a) : n(a.n) {} // COMPLIANT user-defined copy ctor
A(const A &&other_a); // COMPLIANT user-defined move ctor
};

class C1 {};

class C2 : public C1 {
public:
C2 &operator=(const C2 &); // COMPLIANT
};