Skip to content

Commit ede9e1b

Browse files
committed
Consider possible use as template argument
1 parent a3e3f9d commit ede9e1b

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ from PotentiallyUnusedGlobalOrNamespaceVariable v
2222
where
2323
not isExcluded(v, DeadCodePackage::unusedGlobalOrNamespaceVariableQuery()) and
2424
// No variable access
25-
not exists(v.getAnAccess())
25+
not exists(v.getAnAccess()) and
26+
// Exclude members whose value is compile time and is potentially used to inintialize a template
27+
not maybeACompileTimeTemplateArgument(v)
2628
select v, "Variable " + v.getQualifiedName() + " is unused."

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ where
2525
// No variable access
2626
not exists(v.getAnAccess()) and
2727
// No explicit initialization in a constructor
28-
not exists(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v)
28+
not exists(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v) and
29+
// Exclude members whose value is compile time and is potentially used to inintialize a template
30+
not maybeACompileTimeTemplateArgument(v)
2931
select v, "Member variable " + v.getName() + " is unused."

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,22 @@ void test_ns() { x2 = 1; }
4141
m1(); // ignore dead code in macros
4242
} // namespace N1
4343

44-
int test_access_variable() { return N1::x5; }
44+
int test_access_variable() { return N1::x5; }
45+
46+
template <int t> struct C1 {
47+
int array[t]; // COMPLIANT
48+
};
49+
50+
constexpr int g5 = 1; // COMPLIANT - used as template parameter
51+
52+
namespace ns1 {
53+
constexpr int m1 = 1; // COMPLIANT - used a template parameter
54+
}
55+
56+
void test_fp_reported_in_384() {
57+
struct C1<g5> l1;
58+
struct C1<ns1::m1> l2;
59+
60+
l1.array[0] = 1;
61+
l2.array[0] = 1;
62+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,18 @@ void test_d() {
4747
d.getT();
4848
}
4949

50+
template <int t> struct C1 {
51+
int array[t]; // COMPLIANT
52+
};
53+
54+
struct C2 {
55+
static constexpr int m1 = 1; // COMPLIANT - used as template parameter
56+
};
57+
58+
void test_fp_reported_in_384() {
59+
struct C1<C2::m1> l1;
60+
61+
l1.array[0] = 1;
62+
}
63+
5064
} // namespace test

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cpp
22
import codingstandards.cpp.FunctionEquivalence
3+
import codingstandards.cpp.Scope
34

45
/**
56
* A type that contains a template parameter type (doesn't count pointers or references).
@@ -121,3 +122,17 @@ class UserProvidedConstructorFieldInit extends ConstructorFieldInit {
121122
not getEnclosingFunction().isCompilerGenerated()
122123
}
123124
}
125+
126+
predicate maybeACompileTimeTemplateArgument(Variable v) {
127+
v.isConstexpr() and
128+
exists(ClassTemplateInstantiation cti, TranslationUnit tu |
129+
cti.getATemplateArgument().(Expr).getValue() = v.getInitializer().getExpr().getValue() and
130+
(
131+
cti.getFile() = tu and
132+
(
133+
v.getADeclarationEntry().getFile() = tu or
134+
tu.getATransitivelyIncludedFile() = v.getADeclarationEntry().getFile()
135+
)
136+
)
137+
)
138+
}

0 commit comments

Comments
 (0)