From e90cd3fef0dea68cfc13a70bc0c7a3523b59ca62 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 16 Jul 2024 19:07:51 -0400 Subject: [PATCH 1/3] A2-7-3: fix #606, omit fp in friend function declarations in template classes --- change_notes/2024-07-16-fix-fp-606-A2-7-3.md | 2 ++ .../A2-7-3/UndocumentedUserDefinedType.ql | 8 +++-- .../UndocumentedUserDefinedType.expected | 1 + cpp/autosar/test/rules/A2-7-3/test.cpp | 32 ++++++++++++++++++- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 change_notes/2024-07-16-fix-fp-606-A2-7-3.md diff --git a/change_notes/2024-07-16-fix-fp-606-A2-7-3.md b/change_notes/2024-07-16-fix-fp-606-A2-7-3.md new file mode 100644 index 0000000000..a4fc343b76 --- /dev/null +++ b/change_notes/2024-07-16-fix-fp-606-A2-7-3.md @@ -0,0 +1,2 @@ +- `A2-7-3` - `UndocumentedUserDefinedType.ql`: + - Fixes #606. Fix false positive relating to friend functions in template classes. \ No newline at end of file diff --git a/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql b/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql index a8bfe3b361..54bf53bb84 100644 --- a/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql +++ b/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql @@ -3,7 +3,7 @@ * @name A2-7-3: Declarations of 'user-defined' types, member variables and functions should be documented * @description All declarations of 'user-defined' types, static and non-static data members, * functions and methods shall be preceded by documentation. - * @kind problem + * @ kind problem * @precision very-high * @problem.severity recommendation * @tags external/autosar/id/a2-7-3 @@ -44,7 +44,11 @@ class DocumentableDeclaration extends Declaration { // Exclude instantiated template functions, which cannot reasonably be documented. not this.(Function).isFromTemplateInstantiation(_) and // Exclude anonymous lambda functions. - not exists(LambdaExpression lc | lc.getLambdaFunction() = this) + not exists(LambdaExpression lc | lc.getLambdaFunction() = this) and + //Exclude friend functions (because they have 2 entries in the database), and only one shows documented truly + not exists(FriendDecl d | + d.getFriend().(Function).getDefinition() = this.getADeclarationEntry() + ) or this instanceof MemberVariable and declarationType = "member variable" and diff --git a/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected b/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected index 0ae42152f7..90935f9396 100644 --- a/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected +++ b/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected @@ -7,3 +7,4 @@ | test.cpp:81:6:81:6 | definition of e | Declaration entry for function e is missing documentation. | | test.cpp:88:1:88:30 | definition of message_to_string_undocumented | Declaration entry for function message_to_string_undocumented is missing documentation. | | test.cpp:160:21:160:24 | definition of kBar | Declaration entry for member variable kBar is missing documentation. | +| test.cpp:207:14:207:17 | definition of foo3 | Declaration entry for function foo3 is missing documentation. | diff --git a/cpp/autosar/test/rules/A2-7-3/test.cpp b/cpp/autosar/test/rules/A2-7-3/test.cpp index 8e9e180458..77e27c49d4 100644 --- a/cpp/autosar/test/rules/A2-7-3/test.cpp +++ b/cpp/autosar/test/rules/A2-7-3/test.cpp @@ -175,4 +175,34 @@ void testFunctionScope() { void fNestedTest(); // COMPLIANT - in function scope }; }; -} \ No newline at end of file +} + +/// Test documentation +template class ClassG { // COMPLIANT +private: + /// Test documentation + int x; // COMPLIANT + +public: + /// Test documentation + friend int foo(ClassG g) { return g.x; } // COMPLIANT +}; + +/// Test documentation +void test() { // COMPLIANT + ClassG g; + foo(g); +} + +/// Test documentation +class ClassG2 { // COMPLIANT +public: + /// Test documentation + friend int foo2() { return 1; } // COMPLIANT +}; + +/// Test documentation +class ClassG3 { // COMPLIANT +public: + friend int foo3() { return 1; } // NON_COMPLIANT +}; \ No newline at end of file From 6655624206bcfeed619d80850db8457522da10ed Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 16 Jul 2024 19:13:04 -0400 Subject: [PATCH 2/3] A2-7-3: fix query metadata --- cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql b/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql index 54bf53bb84..769c4163ad 100644 --- a/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql +++ b/cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql @@ -3,7 +3,7 @@ * @name A2-7-3: Declarations of 'user-defined' types, member variables and functions should be documented * @description All declarations of 'user-defined' types, static and non-static data members, * functions and methods shall be preceded by documentation. - * @ kind problem + * @kind problem * @precision very-high * @problem.severity recommendation * @tags external/autosar/id/a2-7-3 From 3187f2c769fcc9bc35a53d712f13d4a857388e41 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Thu, 12 Sep 2024 16:57:52 -0400 Subject: [PATCH 3/3] A2-7-3: update test expected file --- .../UndocumentedUserDefinedType.expected | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected b/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected index 90935f9396..d14f6e21f7 100644 --- a/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected +++ b/cpp/autosar/test/rules/A2-7-3/UndocumentedUserDefinedType.expected @@ -1,10 +1,12 @@ -| test.cpp:70:7:70:12 | definition of ClassD | Declaration entry for user-defined type ClassD is missing documentation. | -| test.cpp:72:7:72:7 | definition of a | Declaration entry for member variable a is missing documentation. | -| test.cpp:73:14:73:14 | declaration of b | Declaration entry for member variable b is missing documentation. | -| test.cpp:74:8:74:8 | declaration of f | Declaration entry for function f is missing documentation. | -| test.cpp:76:7:76:7 | definition of c | Declaration entry for member variable c is missing documentation. | -| test.cpp:78:6:78:6 | declaration of d | Declaration entry for function d is missing documentation. | -| test.cpp:81:6:81:6 | definition of e | Declaration entry for function e is missing documentation. | -| test.cpp:88:1:88:30 | definition of message_to_string_undocumented | Declaration entry for function message_to_string_undocumented is missing documentation. | -| test.cpp:160:21:160:24 | definition of kBar | Declaration entry for member variable kBar is missing documentation. | -| test.cpp:207:14:207:17 | definition of foo3 | Declaration entry for function foo3 is missing documentation. | +| test.cpp:74:8:74:8 | declaration of j | Declaration entry for function j is missing documentation. | +| test.cpp:75:8:75:8 | declaration of k | Declaration entry for function k is missing documentation. | +| test.cpp:90:7:90:12 | definition of ClassD | Declaration entry for user-defined type ClassD is missing documentation. | +| test.cpp:92:7:92:7 | definition of a | Declaration entry for member variable a is missing documentation. | +| test.cpp:93:14:93:14 | declaration of b | Declaration entry for member variable b is missing documentation. | +| test.cpp:94:8:94:8 | declaration of f | Declaration entry for function f is missing documentation. | +| test.cpp:96:7:96:7 | definition of c | Declaration entry for member variable c is missing documentation. | +| test.cpp:98:6:98:6 | declaration of d | Declaration entry for function d is missing documentation. | +| test.cpp:101:6:101:6 | definition of e | Declaration entry for function e is missing documentation. | +| test.cpp:108:1:108:30 | definition of message_to_string_undocumented | Declaration entry for function message_to_string_undocumented is missing documentation. | +| test.cpp:180:21:180:24 | definition of kBar | Declaration entry for member variable kBar is missing documentation. | +| test.cpp:227:14:227:17 | definition of foo3 | Declaration entry for function foo3 is missing documentation. |