From a9e6e8391cd8bed9ce2841a7beb33e50fd4e92bb Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Fri, 12 Apr 2024 16:54:40 -0400 Subject: [PATCH 1/3] M9-3-3: address fp issue 381 --- change_notes/2024-04-12-fix-fp-m9-3-3.md | 2 ++ .../M9-3-3/MemberFunctionConstIfPossible.ql | 11 +++++++++- cpp/autosar/test/rules/M9-3-3/test.cpp | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 change_notes/2024-04-12-fix-fp-m9-3-3.md diff --git a/change_notes/2024-04-12-fix-fp-m9-3-3.md b/change_notes/2024-04-12-fix-fp-m9-3-3.md new file mode 100644 index 0000000000..bbc9fb9ab0 --- /dev/null +++ b/change_notes/2024-04-12-fix-fp-m9-3-3.md @@ -0,0 +1,2 @@ +`M9-3-3`: `MemberFunctionConstIfPossible.ql`: + - Fix FP reported in 381. Omit member functions that return nonconst reference types. \ No newline at end of file diff --git a/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql b/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql index 66a3affa24..596879d5fd 100644 --- a/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql +++ b/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql @@ -37,6 +37,13 @@ class NonConstMemberFunction extends MemberFunction { NonConstMemberFunction() { not this.hasSpecifier("const") } } +/** + * References that are not const + */ +class NonConstReferenceType extends ReferenceType { + NonConstReferenceType() { not this.isConst() } +} + /** * `MemberFunction`s that are not const * and not `Constructor`s ect as const constructors are @@ -57,7 +64,9 @@ class ConstMemberFunctionCandidate extends NonConstMemberFunction { this.hasDefinition() and // For uninstantiated templates we have only partial information that prevents us from determining // if the candidate calls non-const functions. Therefore we exclude these. - not this.isFromUninstantiatedTemplate(_) + not this.isFromUninstantiatedTemplate(_) and + //cannot recommend const if it returns a nonconst reference + not this.getType() instanceof NonConstReferenceType } /** diff --git a/cpp/autosar/test/rules/M9-3-3/test.cpp b/cpp/autosar/test/rules/M9-3-3/test.cpp index 704a4ae5fd..f37fd3ac66 100644 --- a/cpp/autosar/test/rules/M9-3-3/test.cpp +++ b/cpp/autosar/test/rules/M9-3-3/test.cpp @@ -193,3 +193,24 @@ void test_template() { class Z3 { void f(int) = delete; // COMPLIANT }; + +class Z4 { +public: + int values[128]; + template + void fill(const T &val) { // COMPLIANT[FALSE_NEGATIVE|TRUE_NEGATIVE] - + // exception not specified in the + // standard, we opt to not raise an issue because the template can be both + // compliant and non-compliant depending on instantiations. + for (auto &elem : values) { + elem = val; + } + } + constexpr int &front() noexcept { return values[0]; } // COMPLIANT +}; + +void fp_reported_in_381() { + Z4 z; + int i = z.front(); + z.fill(i); +} \ No newline at end of file From ab892cae8f5703f9cf4c7d2a59f4552bd5619a88 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Mon, 6 May 2024 17:37:32 -0400 Subject: [PATCH 2/3] Update cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql Co-authored-by: Remco Vermeulen --- cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql b/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql index 596879d5fd..3c8bab4d1f 100644 --- a/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql +++ b/cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql @@ -65,7 +65,7 @@ class ConstMemberFunctionCandidate extends NonConstMemberFunction { // For uninstantiated templates we have only partial information that prevents us from determining // if the candidate calls non-const functions. Therefore we exclude these. not this.isFromUninstantiatedTemplate(_) and - //cannot recommend const if it returns a nonconst reference + // Cannot recommend const if it returns a non-const reference. not this.getType() instanceof NonConstReferenceType } From 7f2099ff156503753e542886b4ddf0c97e67e7aa Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 7 May 2024 13:24:22 -0400 Subject: [PATCH 3/3] M9-3-3: add testcase clarification note --- cpp/autosar/test/rules/M9-3-3/test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/autosar/test/rules/M9-3-3/test.cpp b/cpp/autosar/test/rules/M9-3-3/test.cpp index f37fd3ac66..5469b41d5c 100644 --- a/cpp/autosar/test/rules/M9-3-3/test.cpp +++ b/cpp/autosar/test/rules/M9-3-3/test.cpp @@ -210,6 +210,7 @@ class Z4 { }; void fp_reported_in_381() { + // added to test template initialization effects/lack thereof Z4 z; int i = z.front(); z.fill(i);