diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-10-08 21:26:03 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-10-08 21:26:03 +0000 |
commit | 48632af25cfbe05c8e29307a397d60c9998e6231 (patch) | |
tree | b2e0316e42b77bd1a8c3a856015e2fecb9eaf36a | |
parent | f61f13d4e7e08b44629a1411ace53c5c6aeaf32b (diff) | |
download | bcm5719-llvm-48632af25cfbe05c8e29307a397d60c9998e6231.tar.gz bcm5719-llvm-48632af25cfbe05c8e29307a397d60c9998e6231.zip |
Fix crash or wrong code bug if a lifetime-extended temporary contains a
"non-constant" value.
If the constant evaluator evaluates part of a variable initializer,
including the initializer for some lifetime-extended temporary, but
fails to fully evaluate the initializer, it can leave behind wrong
values for temporaries encountered in that initialization. Don't try to
emit those from CodeGen! Instead, look at the values that constant
evaluation produced if (and only if) it actually succeeds and we're
emitting the lifetime-extending declaration's initializer as a constant.
llvm-svn: 374119
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 6 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 13 | ||||
-rw-r--r-- | clang/test/CodeGenCXX/no-const-init-cxx2a.cpp | 18 |
3 files changed, 28 insertions, 9 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index c32f516aec7..55ed550a1e7 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -13569,8 +13569,10 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, ConstExprUsage Usage, if (!Info.discardCleanups()) llvm_unreachable("Unhandled cleanup; missing full expression marker?"); - return CheckConstantExpression(Info, getExprLoc(), getType(), Result.Val, - Usage) && + QualType T = getType(); + if (!isRValue()) + T = Ctx.getLValueReferenceType(T); + return CheckConstantExpression(Info, getExprLoc(), T, Result.Val, Usage) && CheckMemoryLeaks(Info); } diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 140f8c38cf5..080914afcc9 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -4978,14 +4978,13 @@ ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( VD, E->getManglingNumber(), Out); APValue *Value = nullptr; - if (E->getStorageDuration() == SD_Static) { - // We might have a cached constant initializer for this temporary. Note - // that this might have a different value from the value computed by - // evaluating the initializer if the surrounding constant expression - // modifies the temporary. + if (E->getStorageDuration() == SD_Static && VD && VD->evaluateValue()) { + // If the initializer of the extending declaration is a constant + // initializer, we should have a cached constant initializer for this + // temporary. Note that this might have a different value from the value + // computed by evaluating the initializer if the surrounding constant + // expression modifies the temporary. Value = getContext().getMaterializedTemporaryValue(E, false); - if (Value && Value->isAbsent()) - Value = nullptr; } // Try evaluating it now, it might have a constant initializer. diff --git a/clang/test/CodeGenCXX/no-const-init-cxx2a.cpp b/clang/test/CodeGenCXX/no-const-init-cxx2a.cpp new file mode 100644 index 00000000000..a945c066e3b --- /dev/null +++ b/clang/test/CodeGenCXX/no-const-init-cxx2a.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -std=c++2a | FileCheck %s + +// CHECK-DAG: @p = {{.*}} null +// CHECK-DAG: @_ZGR1p_ = {{.*}} null +int *const &p = new int; + +struct d { + constexpr d(int &&f) : e(f) {} + int &e; +}; + +// CHECK-DAG: @g = {{.*}} null +// CHECK-DAG: @_ZGR1g_ = {{.*}} zeroinitializer +d &&g{{0}}; + +// CHECK: define {{.*}} @__cxx_global_var_init +// CHECK: define {{.*}} @__cxx_global_var_init +// CHECK-NOT: define {{.*}} @__cxx_global_var_init |