Skip to content

Commit 13df5e3

Browse files
authored
Merge branch 'main' into a5-2-2/issue-32/c-style-casts-from-macros
2 parents acb7c90 + 504d5f8 commit 13df5e3

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M0-1-4` - `SingleUseMemberPODVariable.ql`
2+
- Reduce false positives by excluding any constexpr variable whose constant value is used as an argument to a template.

cpp/autosar/src/rules/M0-1-4/SingleUsePODVariable.qll

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import cpp
44
import codingstandards.cpp.TrivialType
55
import codingstandards.cpp.deadcode.UnusedVariables
66

7+
/** Gets the constant value of a constexpr variable. */
8+
private string getConstExprValue(Variable v) {
9+
result = v.getInitializer().getExpr().getValue() and
10+
v.isConstexpr()
11+
}
12+
713
/** Gets a "use" count according to rule M0-1-4. */
814
int getUseCount(Variable v) {
915
exists(int initializers |
@@ -12,7 +18,14 @@ int getUseCount(Variable v) {
1218
result =
1319
initializers +
1420
count(VariableAccess access | access = v.getAnAccess() and not access.isCompilerGenerated())
15-
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v)
21+
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v) +
22+
// For constexpr variables used as template arguments, we don't see accesses (just the
23+
// appropriate literals). We therefore take a conservative approach and count the number of
24+
// template instantiations that use the given constant, and consider each one to be a use
25+
// of the variable
26+
count(ClassTemplateInstantiation cti |
27+
cti.getTemplateArgument(_).(Expr).getValue() = getConstExprValue(v)
28+
)
1629
)
1730
}
1831

cpp/autosar/test/rules/M0-1-4/SingleUseMemberPODVariable.expected

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| test.cpp:36:24:36:29 | unused | Member POD variable unused in C1 is only $@. | test.cpp:36:31:36:31 | initializer for unused | used once |
12
| test_global_or_namespace.cpp:16:7:16:7 | x | Member POD variable x in GA is only $@. | test_global_or_namespace.cpp:38:6:38:6 | x | used once |
23
| test_global_or_namespace.cpp:54:7:54:7 | x | Member POD variable x in N1A is only $@. | test_global_or_namespace.cpp:76:6:76:6 | x | used once |
34
| test_member.cpp:5:7:5:8 | m2 | Member POD variable m2 in A is only $@. | test_member.cpp:9:21:9:25 | constructor init of field m2 | used once |

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Test cases for `SingleUseLocalPODVariable.ql` */
2-
2+
#include <array>
33
class A {};
44

55
class B {
@@ -30,4 +30,12 @@ void test_templates() {
3030
f1<B>(); // Triggers a NON_COMPLIANT case in f1(), because B is a POD type
3131
f1<C>(); // Does not trigger a NON_COMPLIANT case in f1(), because C is not a
3232
// POD type
33-
}
33+
}
34+
35+
class C1 {
36+
static constexpr int unused{1}; // NON_COMPLIANT
37+
static constexpr int used{2}; // COMPLIANT
38+
int test_use() { return used; }
39+
static constexpr int size{3}; // COMPLIANT
40+
std::array<bool, size> array{false, false}; // size is used here
41+
};

0 commit comments

Comments
 (0)