From f99713a693b8f9342105184eb377d13667e12026 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Thu, 25 Jan 2024 01:34:17 -0500 Subject: [PATCH 1/5] A8-4-7: exclude user defined operators and move constructors --- change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md | 2 ++ .../InParametersForCheapToCopyTypesNotPassedByValue.ql | 2 ++ cpp/autosar/test/rules/A8-4-7/test.cpp | 8 ++++++++ 3 files changed, 12 insertions(+) create mode 100644 change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md diff --git a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md new file mode 100644 index 0000000000..6400cfb6fc --- /dev/null +++ b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md @@ -0,0 +1,2 @@ +`A8-4-7`: ` cpp/autosar/in-parameters-for-cheap-to-copy-types-not-passed-by-value` + - Fixes #397. Exclude user defined operators and move constructors. \ No newline at end of file diff --git a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql index e188241672..b5a964e5c6 100644 --- a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql +++ b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql @@ -35,6 +35,8 @@ where t.isConst() and not exists(CatchBlock cb | cb.getParameter() = v) and not exists(CopyConstructor cc | cc.getAParameter() = v) and + not exists(MoveConstructor mc | mc.getAParameter() = v) and + not exists(Operator op | op.getAParameter() = v) and not v.isFromUninstantiatedTemplate(_) select v, "Parameter " + v.getName() + " is the trivially copyable type " + t.getName() + diff --git a/cpp/autosar/test/rules/A8-4-7/test.cpp b/cpp/autosar/test/rules/A8-4-7/test.cpp index 70829ef907..80cd3d48e5 100644 --- a/cpp/autosar/test/rules/A8-4-7/test.cpp +++ b/cpp/autosar/test/rules/A8-4-7/test.cpp @@ -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 +}; \ No newline at end of file From 34ae3bd920bfe26b17e2534a79831663670c4ab5 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Thu, 25 Jan 2024 11:27:46 -0500 Subject: [PATCH 2/5] A8-4-7: exclude user defined operators and special functions --- .../A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql index b5a964e5c6..58309da4cf 100644 --- a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql +++ b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql @@ -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 @@ -34,8 +35,7 @@ where ) and t.isConst() and not exists(CatchBlock cb | cb.getParameter() = v) and - not exists(CopyConstructor cc | cc.getAParameter() = v) and - not exists(MoveConstructor mc | mc.getAParameter() = v) and + not exists(SpecialMemberFunction cc | cc.getAParameter() = v) and not exists(Operator op | op.getAParameter() = v) and not v.isFromUninstantiatedTemplate(_) select v, From cdde1f340e067b7b3618b267c0a1095ae4a8fcc2 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 5 Feb 2024 09:08:03 -0500 Subject: [PATCH 3/5] A8-4-7 address review comments --- change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md | 4 ++-- .../A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md index 6400cfb6fc..614a196494 100644 --- a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md +++ b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md @@ -1,2 +1,2 @@ -`A8-4-7`: ` cpp/autosar/in-parameters-for-cheap-to-copy-types-not-passed-by-value` - - Fixes #397. Exclude user defined operators and move constructors. \ No newline at end of file +`A8-4-7`: `InParametersForCheapToCopyTypesNotPassedByValue.ql` + - Fixes #397. Exclude user defined operators and move constructors.` \ No newline at end of file diff --git a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql index 58309da4cf..2e4ff2addc 100644 --- a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql +++ b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql @@ -39,5 +39,5 @@ where not exists(Operator op | op.getAParameter() = v) and not v.isFromUninstantiatedTemplate(_) 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." From 898c7f1f581480fec198dd2f092f09b7821cfe90 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 5 Feb 2024 18:38:28 -0500 Subject: [PATCH 4/5] A8-4-7: exclusion for templates --- change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md | 5 +++-- .../InParametersForCheapToCopyTypesNotPassedByValue.ql | 3 ++- ...InParametersForNotCheapToCopyTypesNotPassedByReference.ql | 3 ++- .../InParametersForCheapToCopyTypesNotPassedByValue.expected | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md index 614a196494..367471f245 100644 --- a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md +++ b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md @@ -1,2 +1,3 @@ -`A8-4-7`: `InParametersForCheapToCopyTypesNotPassedByValue.ql` - - Fixes #397. Exclude user defined operators and move constructors.` \ No newline at end of file +`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. \ No newline at end of file diff --git a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql index 2e4ff2addc..78e9db28a4 100644 --- a/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql +++ b/cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql @@ -37,7 +37,8 @@ where not exists(CatchBlock cb | cb.getParameter() = v) and not exists(SpecialMemberFunction cc | cc.getAParameter() = v) and not exists(Operator op | op.getAParameter() = v) and - not v.isFromUninstantiatedTemplate(_) + 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." diff --git a/cpp/autosar/src/rules/A8-4-7/InParametersForNotCheapToCopyTypesNotPassedByReference.ql b/cpp/autosar/src/rules/A8-4-7/InParametersForNotCheapToCopyTypesNotPassedByReference.ql index f6d481a54a..b96b9347d3 100644 --- a/cpp/autosar/src/rules/A8-4-7/InParametersForNotCheapToCopyTypesNotPassedByReference.ql +++ b/cpp/autosar/src/rules/A8-4-7/InParametersForNotCheapToCopyTypesNotPassedByReference.ql @@ -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.", diff --git a/cpp/autosar/test/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.expected b/cpp/autosar/test/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.expected index c89e65db90..bc8a9d5f5b 100644 --- a/cpp/autosar/test/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.expected +++ b/cpp/autosar/test/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.expected @@ -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. | From da4599193ed6857a60ee0005545a5ff8fe6f20b0 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 5 Feb 2024 20:24:49 -0500 Subject: [PATCH 5/5] Update change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md Co-authored-by: Remco Vermeulen --- change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md index 367471f245..34c4343d1b 100644 --- a/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md +++ b/change_notes/2024-01-25-fix-reported-fp-for-a8-4-7.md @@ -1,3 +1,3 @@ -`A8-4-7`: `InParametersForCheapToCopyTypesNotPassedByValue.ql`, `InParametersForNotCheapToCopyTypesNotPassedByReference.ql` +`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. \ No newline at end of file