From 5c4e72d9baa6211f628db3b8edf4235d93ef4fd7 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Fri, 19 May 2023 11:36:50 +0100 Subject: [PATCH 1/3] M8-0-1: Exclude compiler generated DeclStmts These can occur, for example, in range-based for loops. --- cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql | 4 +++- cpp/autosar/test/rules/M8-0-1/test.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql b/cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql index 3b2051c1b5..7545315b7e 100644 --- a/cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql +++ b/cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql @@ -20,5 +20,7 @@ import codingstandards.cpp.autosar from DeclStmt ds where not isExcluded(ds, InitializationPackage::multipleLocalDeclaratorsQuery()) and - count(ds.getADeclaration()) > 1 + count(Declaration d | d = ds.getADeclaration()) > 1 and + // Not a compiler generated `DeclStmt`, such as in the range-based for loop + not ds.isCompilerGenerated() select ds, "Declaration list contains more than one declaration." diff --git a/cpp/autosar/test/rules/M8-0-1/test.cpp b/cpp/autosar/test/rules/M8-0-1/test.cpp index 12db420603..cf664e4b34 100644 --- a/cpp/autosar/test/rules/M8-0-1/test.cpp +++ b/cpp/autosar/test/rules/M8-0-1/test.cpp @@ -15,3 +15,10 @@ class ClassA { int m1, m2; // NON_COMPLIANT int m3; // COMPLIANT }; + +#include +void test_loop(std::vector v) { + for (const auto b : v) { // COMPLIANT - DeclStmt is compiler generated + b; + } +} \ No newline at end of file From 6c0d54c85bb8a7821464eaa34624713e70b3b674 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Tue, 4 Jul 2023 17:57:29 +0100 Subject: [PATCH 2/3] A7-1-5: Exclude compiler generated variables A7-1-5 should only apply to non-compiler generated variables. This commit adds a new test case which shows an example which, on some platforms, will cause the compiler to generate a __range variable that is flagged by the query before this change. Note: I have been unable to replicate the problem in a unit test. --- ...AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql | 4 +++- ...ecifierNotUsedAppropriatelyInVariableDefinition.expected | 1 + cpp/autosar/test/rules/A7-1-5/test.cpp | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cpp/autosar/src/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql b/cpp/autosar/src/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql index b3da12685c..a233693d21 100644 --- a/cpp/autosar/src/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql +++ b/cpp/autosar/src/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql @@ -39,6 +39,8 @@ where v.getInitializer().getExpr() instanceof LambdaExpression or v.getInitializer().getExpr() instanceof ClassAggregateLiteral - ) + ) and + // Exclude compiler generated variables + not v.isCompilerGenerated() select v, "Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer." diff --git a/cpp/autosar/test/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.expected b/cpp/autosar/test/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.expected index df7b4aafcd..03e53068f4 100644 --- a/cpp/autosar/test/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.expected +++ b/cpp/autosar/test/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.expected @@ -4,3 +4,4 @@ | test.cpp:27:8:27:8 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. | | test.cpp:28:8:28:8 | b | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. | | test.cpp:81:10:81:10 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. | +| test.cpp:111:19:111:19 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. | diff --git a/cpp/autosar/test/rules/A7-1-5/test.cpp b/cpp/autosar/test/rules/A7-1-5/test.cpp index 4f85b3eb4a..34be754905 100644 --- a/cpp/autosar/test/rules/A7-1-5/test.cpp +++ b/cpp/autosar/test/rules/A7-1-5/test.cpp @@ -105,4 +105,10 @@ void instantiate() { Test_381 t381; t381.test_381_1(); t381.test_381_2(); +} + +void test_loop() { + for (const auto a : {8, 9, 10}) { + a; + } } \ No newline at end of file From 38a5ea6ef8e1f22abe830ef4ead18643770df3b3 Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Tue, 4 Jul 2023 18:01:47 +0100 Subject: [PATCH 3/3] Add change note --- change_notes/2023-07-04-remove-compiler-generated-vars.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 change_notes/2023-07-04-remove-compiler-generated-vars.md diff --git a/change_notes/2023-07-04-remove-compiler-generated-vars.md b/change_notes/2023-07-04-remove-compiler-generated-vars.md new file mode 100644 index 0000000000..b81909405b --- /dev/null +++ b/change_notes/2023-07-04-remove-compiler-generated-vars.md @@ -0,0 +1,2 @@ + * `A7-1-5` - exclude compiler generated variables, such as those generated by for loops. + * `M8-0-1` - exclude compiler generated variables, such as those generated by for loops. \ No newline at end of file