Skip to content
Merged
2 changes: 2 additions & 0 deletions change_notes/2024-01-31-fix-fp-a7-1-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`A7-1-2` - `VariableMissingConstexpr.ql`:
- Fix FP reported in #466. Addresses incorrect assumption that calls to `constexpr` functions are always compile-time evaluated.
11 changes: 10 additions & 1 deletion cpp/autosar/src/rules/A7-1-2/VariableMissingConstexpr.ql
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ predicate isTypeZeroInitializable(Type t) {
t.getUnderlyingType() instanceof ArrayType
}

predicate isCompileTimeEvaluated(Call call) {
call.getTarget().isConstexpr() and
forall(Expr arg | arg = call.getAnArgument() |
Comment thread
rvermeulen marked this conversation as resolved.
Outdated
DataFlow::localExprFlow(any(Literal l), arg)
or
DataFlow::localExprFlow(any(Call c | isCompileTimeEvaluated(call)), arg)
)
}

from Variable v
where
not isExcluded(v, ConstPackage::variableMissingConstexprQuery()) and
Expand All @@ -46,7 +55,7 @@ where
(
v.getInitializer().getExpr().isConstant()
or
v.getInitializer().getExpr().(Call).getTarget().isConstexpr()
any(Call call | isCompileTimeEvaluated(call)) = v.getInitializer().getExpr()
or
isZeroInitializable(v)
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
| test.cpp:55:7:55:8 | m2 | Variable m2 could be marked 'constexpr'. |
| test.cpp:130:7:130:8 | m1 | Variable m1 could be marked 'constexpr'. |
| test.cpp:141:7:141:8 | m1 | Variable m1 could be marked 'constexpr'. |
| test.cpp:212:7:212:7 | x | Variable x could be marked 'constexpr'. |
9 changes: 8 additions & 1 deletion cpp/autosar/test/rules/A7-1-2/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ class ExcludedCases {

void operator=(ExcludedCases &) {} // COMPLIANT
void operator=(ExcludedCases &&) {} // COMPLIANT
};
};

constexpr int add(int x, int y) { return x + y; }

constexpr void fp_reported_in_466(int p) {
int x = add(1, 2); // NON_COMPLIANT
int y = add(1, p); // COMPLIANT
}