Skip to content

M0-1-4: Exclude constexpr variables whose constant is used in a template argument #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2022-11-02-m0-1-4-single-use-with-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `M0-1-4` - `SingleUseMemberPODVariable.ql`
- Reduce false positives by excluding any constexpr variable whose constant value is used as an argument to a template.
15 changes: 14 additions & 1 deletion cpp/autosar/src/rules/M0-1-4/SingleUsePODVariable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import cpp
import codingstandards.cpp.TrivialType
import codingstandards.cpp.deadcode.UnusedVariables

/** Gets the constant value of a constexpr variable. */
private string getConstExprValue(Variable v) {
result = v.getInitializer().getExpr().getValue() and
v.isConstexpr()
}

/** Gets a "use" count according to rule M0-1-4. */
int getUseCount(Variable v) {
exists(int initializers |
Expand All @@ -12,7 +18,14 @@ int getUseCount(Variable v) {
result =
initializers +
count(VariableAccess access | access = v.getAnAccess() and not access.isCompilerGenerated())
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v)
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v) +
// For constexpr variables used as template arguments, we don't see accesses (just the
// appropriate literals). We therefore take a conservative approach and count the number of
// template instantiations that use the given constant, and consider each one to be a use
// of the variable
count(ClassTemplateInstantiation cti |
cti.getTemplateArgument(_).(Expr).getValue() = getConstExprValue(v)
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| 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 |
| 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 |
| 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 |
| 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 |
Expand Down
12 changes: 10 additions & 2 deletions cpp/autosar/test/rules/M0-1-4/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** Test cases for `SingleUseLocalPODVariable.ql` */

#include <array>
class A {};

class B {
Expand Down Expand Up @@ -30,4 +30,12 @@ void test_templates() {
f1<B>(); // Triggers a NON_COMPLIANT case in f1(), because B is a POD type
f1<C>(); // Does not trigger a NON_COMPLIANT case in f1(), because C is not a
// POD type
}
}

class C1 {
static constexpr int unused{1}; // NON_COMPLIANT
static constexpr int used{2}; // COMPLIANT
int test_use() { return used; }
static constexpr int size{3}; // COMPLIANT[FALSE_POSITIVE]
std::array<bool, size> array{false, false}; // size is used here
};