Skip to content

Commit 5ecee7e

Browse files
committed
Fixes false positives for M0-1-3.
1 parent c5bf50b commit 5ecee7e

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Consider constexpr variables used in template instantiations as "used".

cpp/autosar/src/rules/M0-1-3/UnusedLocalVariable.ql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,37 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.deadcode.UnusedVariables
2020

21+
/** Gets the constant value of a constexpr variable. */
22+
private string getConstExprValue(Variable v) {
23+
result = v.getInitializer().getExpr().getValue() and
24+
v.isConstexpr()
25+
}
26+
27+
// This predicate is similar to getUseCount for M0-1-4 except that it also
28+
// considers static_asserts. This was created to cater for M0-1-3 specifically
29+
// and hence, doesn't attempt to reuse the M0-1-4 specific predicate
30+
// - getUseCount()
31+
int getUseCountConservatively(Variable v) {
32+
result =
33+
count(VariableAccess access | access = v.getAnAccess() and not access.isCompilerGenerated())
34+
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v) +
35+
// For constexpr variables used as template arguments, we don't see accesses (just the
36+
// appropriate literals). We therefore take a conservative approach and count the number of
37+
// template instantiations that use the given constant, and consider each one to be a use
38+
// of the variable
39+
count(ClassTemplateInstantiation cti |
40+
cti.getTemplateArgument(_).(Expr).getValue() = getConstExprValue(v)
41+
)
42+
// For static asserts too, check if there is a child which has the same value
43+
// as the constexpr variable.
44+
+ count(StaticAssert s |
45+
s.getCondition().getAChild*().getValue() = getConstExprValue(v))
46+
}
47+
2148
from PotentiallyUnusedLocalVariable v
2249
where
2350
not isExcluded(v, DeadCodePackage::unusedLocalVariableQuery()) and
2451
// Local variable is never accessed
2552
not exists(v.getAnAccess())
53+
and getUseCountConservatively(v) = 0
2654
select v, "Local variable " + v.getName() + " in " + v.getFunction().getName() + " is not used."

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,24 @@ void test_side_effect_init() {
4444
LA a; // NON_COMPLIANT - no constructor called
4545
LC c; // COMPLIANT - constructor called which is considered to potentially
4646
// have side effects
47-
}
47+
}
48+
49+
#include <cstdio>
50+
template <int t>
51+
class CharBuffer
52+
{
53+
public:
54+
int member[t];
55+
CharBuffer():member{0}{}
56+
};
57+
58+
int foo()
59+
{
60+
constexpr int line_length = 1024U;
61+
CharBuffer<line_length> buffer{};
62+
constexpr std::size_t max_stack_size_usage = 64 * 1024;
63+
static_assert(
64+
(sizeof(buffer) + sizeof(line_length)) <= max_stack_size_usage,
65+
"assert");
66+
return buffer.member[0];
67+
}

rule_packages/cpp/Templates.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,33 @@
190190
}
191191
],
192192
"title": "In a class template with a dependent base, any name that may be found in that dependent base shall be referred to using a qualified-id or this->."
193+
},
194+
"M0-1-3": {
195+
"properties": {
196+
"allocated-target": [
197+
"implementation"
198+
],
199+
"enforcement": "automated",
200+
"obligation": "required"
201+
},
202+
"queries": [
203+
{
204+
"description": "Unused variables complicate the program and can indicate a possible mistake on the part of the programmer.",
205+
"kind": "problem",
206+
"name": "A project shall not contain unused local variables",
207+
"precision": "very-high",
208+
"severity": "warning",
209+
"short_name": "UnusedLocalVariable",
210+
"tags": [
211+
"maintainability",
212+
"readability"
213+
],
214+
"implementation_scope": {
215+
"description": "In limited cases, this query can raise false-positives for variables that are defined as constexpr and used in an expression to instantiate a template."
216+
}
217+
}
218+
],
219+
"title": "A project shall not contain unused local variables."
193220
}
194221
}
195-
}
222+
}

0 commit comments

Comments
 (0)