Skip to content

Commit 03be7b8

Browse files
authored
Merge pull request #794 from rak3-sh/rp/fix-789
Fix #789: Reduce False positives on A7-1-2 (VariableMissingConstexpr.ql)
2 parents f97ec0f + 03287b3 commit 03be7b8

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `A7-1-2` - `VariableMissingConstexpr.ql`:
2+
- Do not report on member variables if the class has un-instantiated member function(s).
3+
- Check a call's qualifier as well whether it can be compile time evaluated or not.

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ predicate isTypeZeroInitializable(Type t) {
3535
t.getUnderlyingType() instanceof ArrayType
3636
}
3737

38-
from Variable v
38+
from Variable v, string msg
3939
where
4040
not isExcluded(v, ConstPackage::variableMissingConstexprQuery()) and
4141
v.hasDefinition() and
4242
not v.isConstexpr() and
4343
not v instanceof Parameter and
4444
not v.isAffectedByMacro() and
45+
(
46+
not v instanceof MemberVariable
47+
or
48+
// In case member functions are left un-instantiated, it is possible
49+
// the member variable could be modified in them.
50+
// Hence, don't raise an alert in case this member variable's class
51+
// has a member function that doesn't have a definition.
52+
not exists(MemberFunction mf |
53+
mf.getDeclaringType() = v.getDeclaringType() and
54+
mf.isFromUninstantiatedTemplate(_)
55+
)
56+
) and
4557
isLiteralType(v.getType()) and
4658
// Unfortunately, `isConstant` is not sufficient here because it doesn't include calls to
4759
// constexpr constructors, and does not take into account zero initialization
@@ -66,5 +78,6 @@ where
6678
// Exclude variables in uninstantiated templates, as they may be incomplete
6779
not v.isFromUninstantiatedTemplate(_) and
6880
// Exclude compiler generated variables, which are not user controllable
69-
not v.isCompilerGenerated()
70-
select v, "Variable '" + v.getName() + "' could be marked 'constexpr'."
81+
not v.isCompilerGenerated() and
82+
if v instanceof MemberVariable and not v.isStatic() then msg = " and static." else msg = "."
83+
select v, "Variable '" + v.getName() + "' could be marked 'constexpr'" + msg

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
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:7:55:8 | m2 | Variable 'm2' could be marked 'constexpr' and static. |
11+
| test.cpp:130:7:130:8 | m1 | Variable 'm1' could be marked 'constexpr' and static. |
12+
| test.cpp:141:7:141:8 | m1 | Variable 'm1' could be marked 'constexpr' and static. |
1313
| test.cpp:221:7:221:8 | l1 | Variable 'l1' could be marked 'constexpr'. |
1414
| test.cpp:235:7:235:8 | l6 | Variable 'l6' could be marked 'constexpr'. |
1515
| test.cpp:237:7:237:8 | l8 | Variable 'l8' could be marked 'constexpr'. |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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; // NON_COMPLIANT
131131
};
132132

133133
class VirtualBaseClass {};
@@ -138,7 +138,7 @@ 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+
int m1 = 0; // NON_COMPLIANT
142142
};
143143

144144
class NotAllMembersInitializedClass {
@@ -274,4 +274,4 @@ 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>(); }

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)