Implement the final (hopefully) wrinkle to i-c-e + builtin_constant_p
processing: it allows arbitrary foldable constants as the operand of ?: when
builtin_constant_p is the condition.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60954 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index bdc5096..c01c973 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -1028,14 +1028,18 @@
// If the condition (ignoring parens) is a __builtin_constant_p call,
// then only the true side is actually considered in an integer constant
- // expression. This is an important GNU extension.
- //
- // FIXME: ?: with a conditional expr should arguably be an i-c-e if the true
- // side can be folded in any way to a constant. See GCC PR38377 for
- // discussion.
+ // expression, and it is fully evaluated. This is an important GNU
+ // extension. See GCC PR38377 for discussion.
if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
- if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
- FalseExp = 0;
+ if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) {
+ EvalResult EVResult;
+ if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
+ return false;
+ assert(EVResult.Val.isInt() && "FP conditional expr not expected");
+ Result = EVResult.Val.getInt();
+ if (Loc) *Loc = EVResult.DiagLoc;
+ return true;
+ }
// Evaluate the false one first, discard the result.
if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))