Assume that we're in a constant context if we're asking if the expression can
be compiled into a constant initializer. This fixes the issue where a
__builtin_constant_p() in a compound literal was diagnosed as not being
constant, even though it's always possible to convert the builtin into a
constant.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@347512
91177308-0d34-0410-b5e6-
96231b3b80d8
/// this function returns true, it returns the folded constant in Result. If
/// the expression is a glvalue, an lvalue-to-rvalue conversion will be
/// applied.
- bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const;
+ bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
+ bool InConstantContext = false) const;
/// EvaluateAsBooleanCondition - Return true if this is a constant
/// which we can fold and convert to a boolean condition using
/// we want to. If this function returns true, it returns the folded constant
/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
/// will be applied to the result.
-bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const {
+bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
+ bool InConstantContext) const {
EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects);
+ Info.InConstantContext = InConstantContext;
return ::EvaluateAsRValue(this, Result, Ctx, Info);
}
/// constant folded, but discard the result.
bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
EvalResult Result;
- return EvaluateAsRValue(Result, Ctx) &&
+ return EvaluateAsRValue(Result, Ctx, /* in constant context */ true) &&
!hasUnacceptableSideEffect(Result, SEK);
}
: VK_LValue;
if (isFileScope)
- LiteralExpr = ConstantExpr::Create(Context, LiteralExpr);
+ if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr))
+ for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) {
+ Expr *Init = ILE->getInit(i);
+ ILE->setInit(i, ConstantExpr::Create(Context, Init));
+ }
+
Expr *E = new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType,
VK, LiteralExpr, isFileScope);
if (isFileScope) {
struct foo { int x, y; };
+int y;
+struct foo f = (struct foo){ __builtin_constant_p(y), 42 };
+
struct foo test0(int expr) {
// CHECK: define i64 @test0(i32 %expr)
// CHECK: call i1 @llvm.is.constant.i32(i32 %expr)
POD p = (POD){1, 2};\r
// CHECK-NOT: CXXBindTemporaryExpr {{.*}} 'brace_initializers::POD'\r
// CHECK: CompoundLiteralExpr {{.*}} 'brace_initializers::POD'\r
- // CHECK-NEXT: ConstantExpr {{.*}} 'brace_initializers::POD'\r
// CHECK-NEXT: InitListExpr {{.*}} 'brace_initializers::POD'\r
+ // CHECK-NEXT: ConstantExpr {{.*}}\r
// CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}\r
+ // CHECK-NEXT: ConstantExpr {{.*}}\r
// CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}\r
\r
void test() {\r