Skip to content

M8-0-1/A7-1-5: Exclude compiler generated DeclStmts #322

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2023-07-04-remove-compiler-generated-vars.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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."
4 changes: 3 additions & 1 deletion cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
6 changes: 6 additions & 0 deletions cpp/autosar/test/rules/A7-1-5/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ void instantiate() {
Test_381<int> t381;
t381.test_381_1();
t381.test_381_2();
}

void test_loop() {
for (const auto a : {8, 9, 10}) {
a;
}
}
7 changes: 7 additions & 0 deletions cpp/autosar/test/rules/M8-0-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ class ClassA {
int m1, m2; // NON_COMPLIANT
int m3; // COMPLIANT
};

#include <vector>
void test_loop(std::vector<ClassA> v) {
for (const auto b : v) { // COMPLIANT - DeclStmt is compiler generated
b;
}
}