diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-06-13 23:31:04 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-06-13 23:31:04 +0000 |
commit | 0476d069e394fec587858985ddee870360aec1ee (patch) | |
tree | 49febc5aec69871d253b5bd5daf92db6ca8e6ce2 | |
parent | b1027a480ac3d0545681e06b94012c95413b2116 (diff) | |
download | bcm5719-llvm-0476d069e394fec587858985ddee870360aec1ee.tar.gz bcm5719-llvm-0476d069e394fec587858985ddee870360aec1ee.zip |
PR23833, DR2140: an lvalue-to-rvalue conversion on a glvalue of type
nullptr_t does not access memory.
We now reuse CK_NullToPointer to represent a conversion from a glvalue
of type nullptr_t to a prvalue of nullptr_t where necessary.
This reinstates r345562, reverted in r346065, now that CodeGen's
handling of non-odr-used variables has been fixed.
llvm-svn: 363337
-rw-r--r-- | clang/lib/AST/Expr.cpp | 5 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExprAgg.cpp | 3 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExprScalar.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 6 | ||||
-rw-r--r-- | clang/lib/Sema/SemaInit.cpp | 10 | ||||
-rw-r--r-- | clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp | 7 | ||||
-rw-r--r-- | clang/test/Analysis/nullptr.cpp | 12 | ||||
-rw-r--r-- | clang/test/CXX/drs/dr21xx.cpp | 10 | ||||
-rw-r--r-- | clang/test/CodeGenCXX/nullptr.cpp | 47 | ||||
-rwxr-xr-x | clang/www/cxx_dr_status.html | 2 |
10 files changed, 86 insertions, 20 deletions
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 77d444151c3..72e1119851b 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1885,6 +1885,11 @@ ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK) { unsigned PathSize = (BasePath ? BasePath->size() : 0); void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(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/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index a76058c1472..3b1c5bf876e 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -1352,7 +1352,8 @@ static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { // (int*)0 - Null pointer expressions. if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) return ICE->getCastKind() == CK_NullToPointer && - CGF.getTypes().isPointerZeroInitializable(E->getType()); + CGF.getTypes().isPointerZeroInitializable(E->getType()) && + !E->HasSideEffects(CGF.getContext()); // '\0' if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) return CL->getValue() == 0; diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 267402dbef4..c4167537e79 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2148,14 +2148,14 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { case CK_NullToPointer: if (MustVisitNullValue(E)) - (void) Visit(E); + CGF.EmitIgnoredExpr(E); return CGF.CGM.getNullPointer(cast<llvm::PointerType>(ConvertType(DestTy)), DestTy); case CK_NullToMemberPointer: { if (MustVisitNullValue(E)) - (void) Visit(E); + CGF.EmitIgnoredExpr(E); const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>(); return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index bd54681b7e0..a561b045f9a 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -635,8 +635,10 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) { if (E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) Cleanup.setExprNeedsCleanups(true); - Res = ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, E, nullptr, - VK_RValue); + // 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; + Res = ImplicitCastExpr::Create(Context, T, CK, 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/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 1a1d9347985..1301de4b111 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -7779,9 +7779,13 @@ ExprResult InitializationSequence::Perform(Sema &S, case SK_LValueToRValue: { assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); - CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, - CK_LValueToRValue, CurInit.get(), - /*BasePath=*/nullptr, VK_RValue); + // 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); break; } diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp index df78b49130c..cc62cf1048c 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp @@ -378,7 +378,6 @@ 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); @@ -503,6 +502,12 @@ 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/clang/test/Analysis/nullptr.cpp b/clang/test/Analysis/nullptr.cpp index 38e099b7fbd..e9b975c148a 100644 --- a/clang/test/Analysis/nullptr.cpp +++ b/clang/test/Analysis/nullptr.cpp @@ -128,18 +128,10 @@ 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(nullptr); // expected-note {{Calling 'invokeF'}} - // expected-note@-1{{Passing null pointer value via 1st parameter 'x'}} + invokeF(p); // 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/clang/test/CXX/drs/dr21xx.cpp b/clang/test/CXX/drs/dr21xx.cpp index 83c59d01202..7f76440de2e 100644 --- a/clang/test/CXX/drs/dr21xx.cpp +++ b/clang/test/CXX/drs/dr21xx.cpp @@ -32,6 +32,16 @@ namespace dr2120 { // dr2120: 7 static_assert(!__is_standard_layout(E), ""); } +namespace dr2140 { // dr2140: 9 +#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 dr2170 { // dr2170: 9 #if __cplusplus >= 201103L void f() { diff --git a/clang/test/CodeGenCXX/nullptr.cpp b/clang/test/CodeGenCXX/nullptr.cpp index e93f7061bdd..823c0d7d18a 100644 --- a/clang/test/CodeGenCXX/nullptr.cpp +++ b/clang/test/CodeGenCXX/nullptr.cpp @@ -22,3 +22,50 @@ 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; +} + +namespace PR39528 { + constexpr nullptr_t null = nullptr; + void f(nullptr_t); + void g() { f(null); } +} diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html index cda1d2001db..f36db8aae96 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -12655,7 +12655,7 @@ and <I>POD class</I></td> <td><a href="http://wg21.link/cwg2140">2140</a></td> <td>CD4</td> <td>Lvalue-to-rvalue conversion of <TT>std::nullptr_t</TT></td> - <td class="none" align="center">Unknown</td> + <td class="svn" align="center">SVN</td> </tr> <tr id="2141"> <td><a href="http://wg21.link/cwg2141">2141</a></td> |