r345562 - PR23833, DR2140: an lvalue-to-rvalue conversion on a glvalue of type

Petr Hosek via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 1 20:31:00 PDT 2018


This is triggering an assertion failure in our build, I've filed
PR39528 which also has a reproducer.

On Mon, Oct 29, 2018 at 7:04 PM Richard Smith via cfe-commits
<cfe-commits at lists.llvm.org> wrote:
>
> Author: rsmith
> Date: Mon Oct 29 19:02:49 2018
> New Revision: 345562
>
> URL: http://llvm.org/viewvc/llvm-project?rev=345562&view=rev
> Log:
> 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.
>
> Modified:
>     cfe/trunk/lib/AST/Expr.cpp
>     cfe/trunk/lib/CodeGen/CGExprAgg.cpp
>     cfe/trunk/lib/CodeGen/CGExprScalar.cpp
>     cfe/trunk/lib/Sema/SemaExpr.cpp
>     cfe/trunk/lib/Sema/SemaInit.cpp
>     cfe/trunk/lib/StaticAnalyzer/Core/ExprEngineC.cpp
>     cfe/trunk/test/Analysis/nullptr.cpp
>     cfe/trunk/test/CXX/drs/dr21xx.cpp
>     cfe/trunk/test/CodeGenCXX/nullptr.cpp
>     cfe/trunk/www/cxx_dr_status.html
>
> Modified: cfe/trunk/lib/AST/Expr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/Expr.cpp (original)
> +++ cfe/trunk/lib/AST/Expr.cpp Mon Oct 29 19:02:49 2018
> @@ -1816,6 +1816,11 @@ ImplicitCastExpr *ImplicitCastExpr::Crea
>    void *Buffer =
>        C.Allocate(totalSizeToAlloc<CastExpr::BasePathSizeTy, CXXBaseSpecifier *>(
>            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)
>
> Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Mon Oct 29 19:02:49 2018
> @@ -1300,7 +1300,8 @@ static bool isSimpleZero(const Expr *E,
>    // (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;
>
> Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Mon Oct 29 19:02:49 2018
> @@ -1895,14 +1895,14 @@ Value *ScalarExprEmitter::VisitCastExpr(
>
>    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);
>
> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Oct 29 19:02:49 2018
> @@ -623,8 +623,11 @@ ExprResult Sema::DefaultLvalueConversion
>    if (E->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
>      Cleanup.setExprNeedsCleanups(true);
>
> -  ExprResult 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;
> +  ExprResult 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
>
> Modified: cfe/trunk/lib/Sema/SemaInit.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaInit.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaInit.cpp Mon Oct 29 19:02:49 2018
> @@ -7644,9 +7644,13 @@ 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;
>      }
>
>
> Modified: cfe/trunk/lib/StaticAnalyzer/Core/ExprEngineC.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/ExprEngineC.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/lib/StaticAnalyzer/Core/ExprEngineC.cpp (original)
> +++ cfe/trunk/lib/StaticAnalyzer/Core/ExprEngineC.cpp Mon Oct 29 19:02:49 2018
> @@ -379,7 +379,6 @@ void ExprEngine::VisitCast(const CastExp
>        case CK_BitCast:
>        case CK_AddressSpaceConversion:
>        case CK_BooleanToSignedIntegral:
> -      case CK_NullToPointer:
>        case CK_IntegralToPointer:
>        case CK_PointerToIntegral: {
>          SVal V = state->getSVal(Ex, LCtx);
> @@ -502,6 +501,12 @@ void ExprEngine::VisitCast(const CastExp
>          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);
>
> Modified: cfe/trunk/test/Analysis/nullptr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/nullptr.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/test/Analysis/nullptr.cpp (original)
> +++ cfe/trunk/test/Analysis/nullptr.cpp Mon Oct 29 19:02:49 2018
> @@ -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 at -1{{Taking false branch}}
> -                   // expected-note at -2{{Assuming the condition is false}}
> -                   // expected-note at -3{{Taking false branch}}
> -                   // expected-note at -4{{Assuming the condition is true}}
> -                   // expected-note at -5{{Taking true branch}}
> -    invokeF(p); // expected-warning{{1st function call argument is an uninitialized value}}
> -                // expected-note at -1{{1st function call argument is an uninitialized value}}
> -  if (getSymbol()) // expected-note   {{Assuming the condition is false}}
> -                   // expected-note at -1{{Taking false branch}}
>                     // expected-note at -2{{Assuming the condition is true}}
>                     // expected-note at -3{{Taking true branch}}
> -    invokeF(nullptr); // expected-note   {{Calling 'invokeF'}}
> -                      // expected-note at -1{{Passing null pointer value via 1st parameter 'x'}}
> +    invokeF(p);    // expected-note   {{Calling 'invokeF'}}
> +                   // expected-note at -1{{Passing null pointer value via 1st parameter 'x'}}
>    if (getSymbol()) {  // expected-note  {{Assuming the condition is true}}
>                        // expected-note at -1{{Taking true branch}}
>      X *xx = Type().x; // expected-note   {{Null pointer value stored to field 'x'}}
>
> Modified: cfe/trunk/test/CXX/drs/dr21xx.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/drs/dr21xx.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/test/CXX/drs/dr21xx.cpp (original)
> +++ cfe/trunk/test/CXX/drs/dr21xx.cpp Mon Oct 29 19:02:49 2018
> @@ -19,6 +19,16 @@ 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}}
>
> Modified: cfe/trunk/test/CodeGenCXX/nullptr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/nullptr.cpp?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/test/CodeGenCXX/nullptr.cpp (original)
> +++ cfe/trunk/test/CodeGenCXX/nullptr.cpp Mon Oct 29 19:02:49 2018
> @@ -22,3 +22,44 @@ 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;
> +}
>
> Modified: cfe/trunk/www/cxx_dr_status.html
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/www/cxx_dr_status.html?rev=345562&r1=345561&r2=345562&view=diff
> ==============================================================================
> --- cfe/trunk/www/cxx_dr_status.html (original)
> +++ cfe/trunk/www/cxx_dr_status.html Mon Oct 29 19:02:49 2018
> @@ -12655,7 +12655,7 @@ and <I>POD class</I></td>
>      <td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2140">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://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2141">2141</a></td>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


More information about the cfe-commits mailing list