From: Richard Smith Date: Sat, 3 Nov 2018 02:23:33 +0000 (+0000) Subject: Revert r345562: "PR23833, DR2140: an lvalue-to-rvalue conversion on a glvalue of... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ebc4635e79d821d484c241908398981d4921d5a6;p=clang Revert r345562: "PR23833, DR2140: an lvalue-to-rvalue conversion on a glvalue of type" This exposes a (known) CodeGen bug: it can't cope with emitting lvalue expressions that denote non-odr-used but usable-in-constant-expression variables. See PR39528 for a testcase. Reverted for now until that issue can be fixed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@346065 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 4f7f807a84..8b512815e8 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -1816,11 +1816,6 @@ ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T, void *Buffer = C.Allocate(totalSizeToAlloc( PathSize ? 1 : 0, PathSize)); - // Per C++ [conv.lval]p3, lvalue-to-rvalue conversions on class and - // std::nullptr_t have special semantics not captured by CK_LValueToRValue. - assert((Kind != CK_LValueToRValue || - !(T->isNullPtrType() || T->getAsCXXRecordDecl())) && - "invalid type for lvalue-to-rvalue conversion"); ImplicitCastExpr *E = new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK); if (PathSize) diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp index 0bbf3a6c56..42481939fa 100644 --- a/lib/CodeGen/CGExprAgg.cpp +++ b/lib/CodeGen/CGExprAgg.cpp @@ -1300,8 +1300,7 @@ static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { // (int*)0 - Null pointer expressions. if (const CastExpr *ICE = dyn_cast(E)) return ICE->getCastKind() == CK_NullToPointer && - CGF.getTypes().isPointerZeroInitializable(E->getType()) && - !E->HasSideEffects(CGF.getContext()); + CGF.getTypes().isPointerZeroInitializable(E->getType()); // '\0' if (const CharacterLiteral *CL = dyn_cast(E)) return CL->getValue() == 0; diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index c77f1bab5b..519606e821 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -2072,14 +2072,14 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { case CK_NullToPointer: if (MustVisitNullValue(E)) - CGF.EmitIgnoredExpr(E); + (void) Visit(E); return CGF.CGM.getNullPointer(cast(ConvertType(DestTy)), DestTy); case CK_NullToMemberPointer: { if (MustVisitNullValue(E)) - CGF.EmitIgnoredExpr(E); + (void) Visit(E); const MemberPointerType *MPT = CE->getType()->getAs(); return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index d7fc1aa04d..a7dc08248e 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -623,11 +623,8 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { if (E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) Cleanup.setExprNeedsCleanups(true); - // C++ [conv.lval]p3: - // If T is cv std::nullptr_t, the result is a null pointer constant. - CastKind CK = T->isNullPtrType() ? CK_NullToPointer : CK_LValueToRValue; - ExprResult Res = - ImplicitCastExpr::Create(Context, T, CK, E, nullptr, VK_RValue); + ExprResult Res = ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, E, + nullptr, VK_RValue); // C11 6.3.2.1p2: // ... if the lvalue has atomic type, the value has the non-atomic version diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 0cfac99719..1e4d12d20c 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -7644,13 +7644,9 @@ InitializationSequence::Perform(Sema &S, case SK_LValueToRValue: { assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); - // C++ [conv.lval]p3: - // If T is cv std::nullptr_t, the result is a null pointer constant. - CastKind CK = - Step->Type->isNullPtrType() ? CK_NullToPointer : CK_LValueToRValue; - CurInit = - ImplicitCastExpr::Create(S.Context, Step->Type, CK, CurInit.get(), - /*BasePath=*/nullptr, VK_RValue); + CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, + CK_LValueToRValue, CurInit.get(), + /*BasePath=*/nullptr, VK_RValue); break; } diff --git a/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/lib/StaticAnalyzer/Core/ExprEngineC.cpp index 74345b5eba..fb13e89b5f 100644 --- a/lib/StaticAnalyzer/Core/ExprEngineC.cpp +++ b/lib/StaticAnalyzer/Core/ExprEngineC.cpp @@ -379,6 +379,7 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, case CK_BitCast: case CK_AddressSpaceConversion: case CK_BooleanToSignedIntegral: + case CK_NullToPointer: case CK_IntegralToPointer: case CK_PointerToIntegral: { SVal V = state->getSVal(Ex, LCtx); @@ -501,12 +502,6 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, Bldr.generateNode(CastE, Pred, state); continue; } - case CK_NullToPointer: { - SVal V = svalBuilder.makeNull(); - state = state->BindExpr(CastE, LCtx, V); - Bldr.generateNode(CastE, Pred, state); - continue; - } case CK_NullToMemberPointer: { SVal V = svalBuilder.getMemberPointer(nullptr); state = state->BindExpr(CastE, LCtx, V); diff --git a/test/Analysis/nullptr.cpp b/test/Analysis/nullptr.cpp index e9b975c148..38e099b7fb 100644 --- a/test/Analysis/nullptr.cpp +++ b/test/Analysis/nullptr.cpp @@ -126,12 +126,20 @@ struct Type { void shouldNotCrash() { decltype(nullptr) p; // expected-note{{'p' declared without an initial value}} + if (getSymbol()) // expected-note {{Assuming the condition is false}} + // expected-note@-1{{Taking false branch}} + // expected-note@-2{{Assuming the condition is false}} + // expected-note@-3{{Taking false branch}} + // expected-note@-4{{Assuming the condition is true}} + // expected-note@-5{{Taking true branch}} + invokeF(p); // expected-warning{{1st function call argument is an uninitialized value}} + // expected-note@-1{{1st function call argument is an uninitialized value}} if (getSymbol()) // expected-note {{Assuming the condition is false}} // expected-note@-1{{Taking false branch}} // expected-note@-2{{Assuming the condition is true}} // expected-note@-3{{Taking true branch}} - invokeF(p); // expected-note {{Calling 'invokeF'}} - // expected-note@-1{{Passing null pointer value via 1st parameter 'x'}} + invokeF(nullptr); // expected-note {{Calling 'invokeF'}} + // expected-note@-1{{Passing null pointer value via 1st parameter 'x'}} if (getSymbol()) { // expected-note {{Assuming the condition is true}} // expected-note@-1{{Taking true branch}} X *xx = Type().x; // expected-note {{Null pointer value stored to field 'x'}} diff --git a/test/CXX/drs/dr21xx.cpp b/test/CXX/drs/dr21xx.cpp index b111529b09..2522ff7dbd 100644 --- a/test/CXX/drs/dr21xx.cpp +++ b/test/CXX/drs/dr21xx.cpp @@ -19,16 +19,6 @@ namespace dr2120 { // dr2120: 7 static_assert(!__is_standard_layout(E), ""); } -namespace dr2140 { // dr2140: 8 -#if __cplusplus >= 201103L - union U { int a; decltype(nullptr) b; }; - constexpr int *test(U u) { - return u.b; - } - static_assert(!test({123}), "u.b should be valid even when b is inactive"); -#endif -} - namespace dr2180 { // dr2180: yes class A { A &operator=(const A &); // expected-note 0-2{{here}} diff --git a/test/CodeGenCXX/nullptr.cpp b/test/CodeGenCXX/nullptr.cpp index 222e5e5803..e93f7061bd 100644 --- a/test/CodeGenCXX/nullptr.cpp +++ b/test/CodeGenCXX/nullptr.cpp @@ -22,44 +22,3 @@ void g() { const std::type_info& f2() { return typeid(nullptr_t); } - -union U { - int n; - nullptr_t b; -}; -// CHECK-LABEL: define {{.*}}pr23833_a -// CHECK: store -// CHECK: load -// CHECK-NOT: load -// CHECK: ret i1 false -bool pr23833_a(U &u) { return u.b; } - -// CHECK-LABEL: define {{.*}}pr23833_b -// CHECK: store -// CHECK: load -// CHECK-NOT: load -// CHECK: ret i8* null -nullptr_t pr23833_b(nullptr_t &n) { return n; } - -struct X1 { operator int*(); }; -struct X2 { operator const nullptr_t&(); }; - -// CHECK-LABEL: define {{.*}}pr23833_c -// CHECK: call {{.*}}X1 -// CHECK: call {{.*}}X2 -// CHECK-NOT: load -// CHECK: ret i32 -int pr23833_c() { - return X1() != X2(); -} - -// CHECK-LABEL: define {{.*}}pr23833_d -// CHECK: call {{.*}}X2 -// CHECK-NOT: load -// CHECK: store -// CHECK: load -// CHECK: ret i32* -int *pr23833_d() { - int *p = X2(); - return p; -} diff --git a/www/cxx_dr_status.html b/www/cxx_dr_status.html index 69b0b9d075..460978260f 100755 --- a/www/cxx_dr_status.html +++ b/www/cxx_dr_status.html @@ -12655,7 +12655,7 @@ and POD class 2140 CD4 Lvalue-to-rvalue conversion of std::nullptr_t - SVN + Unknown 2141