From 44a57acf5666529abb4de66caae41a75a75c250a Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Thu, 18 Aug 2022 02:31:21 +0200 Subject: [PATCH 1/2] Distinguish stream operators from binary shifts --- cpp/common/src/codingstandards/cpp/Operator.qll | 4 ++-- cpp/common/test/includes/standard-library/ostream.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cpp/common/src/codingstandards/cpp/Operator.qll b/cpp/common/src/codingstandards/cpp/Operator.qll index 8e2e30df36..55db1f9da2 100644 --- a/cpp/common/src/codingstandards/cpp/Operator.qll +++ b/cpp/common/src/codingstandards/cpp/Operator.qll @@ -170,8 +170,8 @@ class UserBitwiseOperator extends Function { op in ["&", "|", "^", "~", "%", "<<", ">>"] ) and not this.isCompilerGenerated() and - not this.getType().hasName("ostream") and - not this.getType().hasName("istream") + not this.getType().(ReferenceType).getBaseType().hasName("ostream") and + not this.getType().(ReferenceType).getBaseType().hasName("istream") } } diff --git a/cpp/common/test/includes/standard-library/ostream.h b/cpp/common/test/includes/standard-library/ostream.h index 04408d6747..bde2b7a53f 100644 --- a/cpp/common/test/includes/standard-library/ostream.h +++ b/cpp/common/test/includes/standard-library/ostream.h @@ -1,6 +1,7 @@ #ifndef _GHLIBCPP_OSTREAM #define _GHLIBCPP_OSTREAM #include "string.h" +#include namespace std { template From eb5616d8534580a59f5c70884d123afb1f53bd65 Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Thu, 18 Aug 2022 02:39:25 +0200 Subject: [PATCH 2/2] Distinguish stream operators from binary shifts --- .../2022-08-17-fix-reported-fp-for-a13-2-2.md | 2 ++ ...OperatorAndBitwiseOperatorReturnAPrvalue.expected | 3 ++- cpp/autosar/test/rules/A13-2-2/test.cpp | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 change_notes/2022-08-17-fix-reported-fp-for-a13-2-2.md diff --git a/change_notes/2022-08-17-fix-reported-fp-for-a13-2-2.md b/change_notes/2022-08-17-fix-reported-fp-for-a13-2-2.md new file mode 100644 index 0000000000..aceb818bd9 --- /dev/null +++ b/change_notes/2022-08-17-fix-reported-fp-for-a13-2-2.md @@ -0,0 +1,2 @@ + - `A13-2-2` - `BinaryOperatorAndBitwiseOperatorReturnAPrvalue.ql`: + - Remove findings related to stream operators. \ No newline at end of file diff --git a/cpp/autosar/test/rules/A13-2-2/BinaryOperatorAndBitwiseOperatorReturnAPrvalue.expected b/cpp/autosar/test/rules/A13-2-2/BinaryOperatorAndBitwiseOperatorReturnAPrvalue.expected index 6db5f520a5..45d74bda6f 100644 --- a/cpp/autosar/test/rules/A13-2-2/BinaryOperatorAndBitwiseOperatorReturnAPrvalue.expected +++ b/cpp/autosar/test/rules/A13-2-2/BinaryOperatorAndBitwiseOperatorReturnAPrvalue.expected @@ -1,3 +1,4 @@ | test.cpp:16:9:16:17 | operator- | User-defined bitwise or arithmetic operator operator-(const A &, int) -> const A does not return a prvalue. | | test.cpp:20:4:20:12 | operator\| | User-defined bitwise or arithmetic operator operator\|(const A &, const A &) -> A * does not return a prvalue. | -| test.cpp:29:6:29:14 | operator+ | User-defined bitwise or arithmetic operator NS_C::operator+(const C &, const C &) -> int & does not return a prvalue. | +| test.cpp:24:9:24:18 | operator<< | User-defined bitwise or arithmetic operator operator<<(const A &, const A &) -> const A does not return a prvalue. | +| test.cpp:34:6:34:14 | operator+ | User-defined bitwise or arithmetic operator NS_C::operator+(const C &, const C &) -> int & does not return a prvalue. | diff --git a/cpp/autosar/test/rules/A13-2-2/test.cpp b/cpp/autosar/test/rules/A13-2-2/test.cpp index b2195e213c..5743804582 100644 --- a/cpp/autosar/test/rules/A13-2-2/test.cpp +++ b/cpp/autosar/test/rules/A13-2-2/test.cpp @@ -21,6 +21,11 @@ A *operator|(A const &a1, A const &a2) noexcept { // NON_COMPLIANT return new A(); } +const A operator<<(A const &, A const &) noexcept // NON_COMPLIANT +{ + return A{}; +} + class C { C &operator=(const C &rhs); }; @@ -31,3 +36,10 @@ int &operator+(const C &lhs, const C &rhs) { // NON_COMPLIANT return slocal; } } // namespace NS_C + +#include +struct Test {}; +std::ostream &operator<<(std::ostream &os, const Test &) { // COMPLIANT + os << "test"; + return os; +}