Skip to content

Commit 3e98b67

Browse files
authored
Merge pull request #620 from github/lcartey/m0-1-3-comp-uninst
M0-1-3: Exclude uninstantiated variable templates
2 parents f1e1f21 + 06afa3c commit 3e98b67

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M0-1-3` - `UnusedGlobalOrNamespaceVariable.ql`
2+
- Reduces false positives by excluding compiler generated variables, and variables in uninstantiated templates.

cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
55
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
66
| test.cpp:44:6:44:6 | a | Local variable 'a' in 'test_side_effect_init' is not used. |
7+
| test.cpp:91:5:91:5 | t | Local variable 't' in 'template_function' is not used. |

cpp/autosar/test/rules/M0-1-3/test.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,24 @@ int baz() {
7878
test_constexpr_in_static_assert<int>();
7979
return 0;
8080
}
81+
82+
template <bool... Args> extern constexpr bool all_of_v = true; // COMPLIANT
83+
84+
template <bool B1, bool... Args>
85+
extern constexpr bool all_of_v<B1, Args...> =
86+
B1 &&all_of_v<Args...>; // COMPLIANT
87+
88+
void test_template_variable() { all_of_v<true, true, true>; }
89+
90+
template <typename T> void template_function() {
91+
T t; // NON_COMPLIANT - t is never used
92+
T t2; // COMPLIANT - t is used
93+
t2.test(); // Call may not be resolved in uninstantiated template
94+
}
95+
96+
class ClassT {
97+
public:
98+
void test() {}
99+
};
100+
101+
void test_template_function() { template_function<ClassT>(); }

cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll

+13-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ class PotentiallyUnusedLocalVariable extends LocalVariable {
4848
not exists(AsmStmt s | f = s.getEnclosingFunction()) and
4949
// Ignore functions with error expressions as they indicate expressions that the extractor couldn't process
5050
not any(ErrorExpr e).getEnclosingFunction() = f
51-
)
51+
) and
52+
// exclude uninstantiated template members
53+
not this.isFromUninstantiatedTemplate(_) and
54+
// Do not report compiler generated variables
55+
not this.isCompilerGenerated()
5256
}
5357
}
5458

@@ -95,7 +99,9 @@ class PotentiallyUnusedMemberVariable extends MemberVariable {
9599
// Lambda captures are not "real" member variables - it's an implementation detail that they are represented that way
96100
not this = any(LambdaCapture lc).getField() and
97101
// exclude uninstantiated template members
98-
not this.isFromUninstantiatedTemplate(_)
102+
not this.isFromUninstantiatedTemplate(_) and
103+
// Do not report compiler generated variables
104+
not this.isCompilerGenerated()
99105
}
100106
}
101107

@@ -107,7 +113,11 @@ class PotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariab
107113
// Not declared in a macro expansion
108114
not isInMacroExpansion() and
109115
// No side-effects from declaration
110-
not declarationHasSideEffects(this)
116+
not declarationHasSideEffects(this) and
117+
// exclude uninstantiated template members
118+
not this.isFromUninstantiatedTemplate(_) and
119+
// Do not report compiler generated variables
120+
not this.isCompilerGenerated()
111121
}
112122
}
113123

0 commit comments

Comments
 (0)