1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 429891 - Fold conditional expression when condition's value becomes

known

Change-Id: Ic241428937864d2c93f6d252386c89fcd3172974
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
Reviewed-on: https://git.eclipse.org/r/23768
Tested-by: Hudson CI
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
Nathan Ridge 2014-03-24 02:10:08 -04:00 committed by Sergey Prigogin
parent 35daa1d321
commit 2384c6442a
2 changed files with 25 additions and 0 deletions

View file

@ -8477,4 +8477,17 @@ public class AST2TemplateTests extends AST2TestBase {
public void testConstexprFunctionCallWithNonConstexprArguments_429891() throws Exception {
parseAndCheckBindings();
}
// constexpr int naive_fibonacci(int x) {
// return x == 0 ? 0
// : x == 1 ? 1
// : naive_fibonacci(x - 2) + naive_fibonacci(x - 1);
// }
//
// constexpr int waldo = naive_fibonacci(5);
public void testConditionalExpressionFolding_429891() throws Exception {
BindingAssertionHelper helper = getAssertionHelper();
IVariable waldo = helper.assertNonProblem("waldo");
assertEquals(5, waldo.getInitialValue().numericalValue().longValue());
}
}

View file

@ -356,6 +356,18 @@ public class EvalConditional extends CPPDependentEvaluation {
public ICPPEvaluation computeForFunctionCall(CPPFunctionParameterMap parameterMap,
int maxdepth, IASTNode point) {
ICPPEvaluation condition = fCondition.computeForFunctionCall(parameterMap, maxdepth, point);
// If the condition can be evaluated, fold the conditional into
// just the branch that is taken. This avoids infinite recursion
// when computing a recursive constexpr function where the base
// case of the recursion is one of the branches of the conditional.
Long conditionValue = condition.getValue(point).numericalValue();
if (conditionValue != null) {
if (conditionValue.longValue() != 0) {
return fPositive == null ? null : fPositive.computeForFunctionCall(parameterMap, maxdepth, point);
} else {
return fNegative.computeForFunctionCall(parameterMap, maxdepth, point);
}
}
ICPPEvaluation positive = fPositive == null ?
null : fPositive.computeForFunctionCall(parameterMap, maxdepth, point);
ICPPEvaluation negative = fNegative.computeForFunctionCall(parameterMap, maxdepth, point);