Skip to content

Commit de24d43

Browse files
committed
Fix #789
1 parent 02dfac9 commit de24d43

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

change_notes/2024-11-11-fix-fp-789.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A7-1-2` - `VariableMissingConstexpr.ql`:
2+
- Fixes #789. Doesn't alert on non-static member variables and compiler generated variables of range based for-loops.

cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ where
4242
not v.isConstexpr() and
4343
not v instanceof Parameter and
4444
not v.isAffectedByMacro() and
45+
// Don't consider non-static member variables.
46+
(
47+
not v instanceof MemberVariable
48+
or
49+
v.isStatic()
50+
) and
4551
isLiteralType(v.getType()) and
4652
// Unfortunately, `isConstant` is not sufficient here because it doesn't include calls to
4753
// constexpr constructors, and does not take into account zero initialization

cpp/autosar/test/rules/A7-1-2/VariableMissingConstexpr.expected

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
| test.cpp:41:14:41:15 | l2 | Variable 'l2' could be marked 'constexpr'. |
88
| test.cpp:44:16:44:17 | lc | Variable 'lc' could be marked 'constexpr'. |
99
| test.cpp:45:17:45:19 | lc2 | Variable 'lc2' could be marked 'constexpr'. |
10-
| test.cpp:55:7:55:8 | m2 | Variable 'm2' could be marked 'constexpr'. |
11-
| test.cpp:130:7:130:8 | m1 | Variable 'm1' could be marked 'constexpr'. |
12-
| test.cpp:141:7:141:8 | m1 | Variable 'm1' could be marked 'constexpr'. |
10+
| test.cpp:55:20:55:21 | m2 | Variable 'm2' could be marked 'constexpr'. |
11+
| test.cpp:143:5:143:20 | m1 | Variable 'm1' could be marked 'constexpr'. |
1312
| test.cpp:221:7:221:8 | l1 | Variable 'l1' could be marked 'constexpr'. |
1413
| test.cpp:235:7:235:8 | l6 | Variable 'l6' could be marked 'constexpr'. |
1514
| test.cpp:237:7:237:8 | l8 | Variable 'l8' could be marked 'constexpr'. |

cpp/autosar/test/rules/A7-1-2/test.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class MemberConstExpr {
5151
MemberConstExpr(int p3) : m3(p3) {}
5252

5353
private:
54-
int m1; // COMPLIANT - is not always zero initialized
55-
int m2 = 0; // NON_COMPLIANT
56-
int m3 = 0; // COMPLIANT - can be set by constructor
54+
int m1; // COMPLIANT - is not always zero initialized
55+
static const int m2 = 0; // NON_COMPLIANT
56+
int m3 = 0; // COMPLIANT - can be set by constructor
5757
};
5858

5959
int h1(int x, int y) { // NON_COMPLIANT
@@ -127,7 +127,7 @@ class MissingConstexprClass {
127127
MissingConstexprClass(int i) = delete; // NON_COMPLIANT
128128
MissingConstexprClass(int i, LiteralClass lc) {} // NON_COMPLIANT
129129
private:
130-
int m1 = 0;
130+
int m1 = 0; // COMPLIANT - non-static member variable
131131
};
132132

133133
class VirtualBaseClass {};
@@ -138,9 +138,9 @@ class DerivedClass : public virtual VirtualBaseClass {
138138
DerivedClass(int i) = delete; // COMPLIANT
139139
DerivedClass(int i, LiteralClass lc) {} // COMPLIANT
140140
private:
141-
int m1 = 0;
141+
static int m1; // NON_COMPLAINT - static member variable can be constexpr
142142
};
143-
143+
int DerivedClass::m1 = 0;
144144
class NotAllMembersInitializedClass {
145145
public:
146146
NotAllMembersInitializedClass() = default; // COMPLIANT
@@ -274,4 +274,15 @@ template <typename T> T *init() {
274274
return t;
275275
}
276276

277-
void test_template_instantiation() { int *t = init<int>(); }
277+
void test_template_instantiation() { int *t = init<int>(); }
278+
279+
#include <memory>
280+
#include <vector>
281+
void a_function() {
282+
auto origin = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9};
283+
auto values = std::vector<std::unique_ptr<int>>{};
284+
for (auto &value :
285+
origin) { // Sometimes, CodeQL reports "value" should be constexpr
286+
values.emplace_back(std::make_unique<int>(value));
287+
}
288+
}

cpp/common/src/codingstandards/cpp/Expr.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ predicate isCompileTimeEvaluatedCall(Call call) {
267267
parameterUsingDefaultValue.getAnAssignedValue() = defaultValue
268268
|
269269
isDirectCompileTimeEvaluatedExpression(defaultValue)
270-
)
270+
) and
271+
// 4. the call's qualifier is compile time evaluated.
272+
(not call.hasQualifier() or isCompileTimeEvaluatedExpression(call.getQualifier()))
271273
}
272274

273275
/*

0 commit comments

Comments
 (0)