Skip to content

Commit 8043222

Browse files
authored
Merge branch 'main' into lcartey/a0-1-1-ignore-incomplete-or-compiler-generated-vars
2 parents 2fc8f1d + bf45f8c commit 8043222

7 files changed

+59
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* `A7-1-5` - exclude auto variables initialized with an expression of non-fundamental type. Typically this occurs when using range based for loops with arrays of non-fundamental types. For example:
2+
```
3+
void iterate(Foo values[]) {
4+
for (auto value : values) { // COMPLIANT (previously false positive)
5+
// ...
6+
}
7+
}
8+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `A15-4-4`: remove false positives reported on uninsantiated templates.

cpp/autosar/src/rules/A15-4-4/MissingNoExcept.ql

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ where
3333
// The function is defined in this database
3434
f.hasDefinition() and
3535
// This function is not an overriden call operator of a lambda expression
36-
not exists(LambdaExpression lambda | lambda.getLambdaFunction() = f)
37-
select f, "Function " + f.getName() + " could be declared noexcept(true)."
36+
not exists(LambdaExpression lambda | lambda.getLambdaFunction() = f) and
37+
// Exclude results from uinstantiated templates
38+
not f.isFromUninstantiatedTemplate(_)
39+
select f, "Function " + f.getQualifiedName() + " could be declared noexcept(true)."

cpp/autosar/src/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import cpp
2020
import codingstandards.cpp.autosar
2121

22+
class FundamentalType extends BuiltInType {
23+
FundamentalType() { not this instanceof ErroneousType and not this instanceof UnknownType }
24+
}
25+
2226
from Variable v
2327
where
2428
not isExcluded(v,
@@ -28,12 +32,14 @@ where
2832
// exclude uninstantiated templates and rely on the instantiated templates, because an uninstantiated template may not contain the information required to determine if the usage is allowed.
2933
not v.isFromUninstantiatedTemplate(_) and
3034
not (
31-
// find ones where
35+
// Initialized by function call
3236
v.getInitializer().getExpr() instanceof FunctionCall
3337
or
38+
// Initialized by lambda expression
3439
v.getInitializer().getExpr() instanceof LambdaExpression
3540
or
36-
v.getInitializer().getExpr() instanceof ClassAggregateLiteral
41+
// Initialized by non-fundamental type
42+
not v.getInitializer().getExpr().getType() instanceof FundamentalType
3743
) and
3844
// Exclude compiler generated variables
3945
not v.isCompilerGenerated()

cpp/autosar/test/rules/A15-4-4/test.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ class A {
3030
void lambda_example() noexcept {
3131
auto with_capture = [=]() {};
3232
auto empty_capture = []() {};
33+
}
34+
35+
#include <utility>
36+
template <typename TypeA, typename TypeB>
37+
void swap_wrapper(TypeA lhs,
38+
TypeB rhs) noexcept(noexcept(std::swap(*lhs, *rhs))) {
39+
std::swap(*lhs, *rhs);
40+
}
41+
42+
void test_swap_wrapper() noexcept {
43+
int a = 0;
44+
int b = 1;
45+
swap_wrapper(&a, &b);
3346
}

cpp/autosar/test/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
| 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. |
55
| 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. |
66
| 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. |
7-
| 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. |
7+
| test.cpp:111:13:111:13 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. |

cpp/autosar/test/rules/A7-1-5/test.cpp

+24-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,30 @@ void instantiate() {
106106
t381.test_381_1();
107107
t381.test_381_2();
108108
}
109-
109+
class Foo {};
110110
void test_loop() {
111-
for (const auto a : {8, 9, 10}) {
111+
for (auto a : {8, 9, 10}) { // NON_COMPLIANT - a is initialized with a
112+
// non-constant initializer
113+
a;
114+
}
115+
116+
std::vector<int> v = {1, 2, 3};
117+
for (auto &a : v) { // COMPLIANT - a is intialized with a function call
118+
a;
119+
}
120+
121+
Foo f1;
122+
Foo f2;
123+
for (auto &a : {f1, f2}) { // COMPLIANT - initialized with a non-fundamental
124+
// type
112125
a;
113126
}
114-
}
127+
}
128+
129+
template <typename T> void test_template(std::vector<T> v2) {
130+
for (auto &a : v2) { // COMPLIANT - a is intialized with a function call
131+
a;
132+
}
133+
}
134+
135+
void test_template_instantiation() { test_template<int>({1, 2, 3}); }

0 commit comments

Comments
 (0)