Skip to content

Commit 035fcbd

Browse files
committed
Address incomplete constexpr function behavior
A call to a `constexpr` function is insufficient to determine that the return value is compile time computed. We need to also validate that its arguments are compile time computed.
1 parent e1f822a commit 035fcbd

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ predicate isTypeZeroInitializable(Type t) {
3333
t.getUnderlyingType() instanceof ArrayType
3434
}
3535

36+
predicate isCompileTimeEvaluated(Call call) {
37+
call.getTarget().isConstexpr() and
38+
forall(Expr arg | arg = call.getAnArgument() |
39+
DataFlow::localExprFlow(any(Literal l), arg)
40+
or
41+
DataFlow::localExprFlow(any(Call c | isCompileTimeEvaluated(call)), arg)
42+
)
43+
}
44+
3645
from Variable v
3746
where
3847
not isExcluded(v, ConstPackage::variableMissingConstexprQuery()) and
@@ -46,7 +55,7 @@ where
4655
(
4756
v.getInitializer().getExpr().isConstant()
4857
or
49-
v.getInitializer().getExpr().(Call).getTarget().isConstexpr()
58+
any(Call call | isCompileTimeEvaluated(call)) = v.getInitializer().getExpr()
5059
or
5160
isZeroInitializable(v)
5261
or

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
| test.cpp:55:7:55:8 | m2 | Variable m2 could be marked 'constexpr'. |
1111
| test.cpp:130:7:130:8 | m1 | Variable m1 could be marked 'constexpr'. |
1212
| test.cpp:141:7:141:8 | m1 | Variable m1 could be marked 'constexpr'. |
13+
| test.cpp:215:7:215:7 | x | Variable x could be marked 'constexpr'. |

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,14 @@ class ExcludedCases {
204204

205205
void operator=(ExcludedCases &) {} // COMPLIANT
206206
void operator=(ExcludedCases &&) {} // COMPLIANT
207-
};
207+
};
208+
209+
210+
constexpr int add(int x, int y) {
211+
return x + y;
212+
}
213+
214+
void fp_reported_in_466(int p) {
215+
int x = add(1,2); // NON_COMPLIANT
216+
int y = add(1,p); // COMPLIANT
217+
}

0 commit comments

Comments
 (0)