commit b1eacdf947a040d11cf7c1fe5d1a78889c75d36b
Author: Richard Smith <richard-llvm@metafoo.co.uk>
Date:   Fri Dec 14 14:41:39 2018 -0800

    Add distinct cast kind for an lvalue-to-rvalue conversion on nullptr_t.
    
    This represents in the AST the fact that such an lvalue-to-rvalue
    conversion is not like any others (it does not actually read a value
    from memory).

diff --git a/include/clang/AST/OperationKinds.def b/include/clang/AST/OperationKinds.def
index cd19091e31..2cc6ab8322 100644
--- a/include/clang/AST/OperationKinds.def
+++ b/include/clang/AST/OperationKinds.def
@@ -112,6 +112,14 @@ CAST_OPERATION(ArrayToPointerDecay)
 ///   void(int) -> void(*)(int)
 CAST_OPERATION(FunctionToPointerDecay)
 
+/// CK_NullptrLValueToRValue - An lvalue-to-rvalue conversion on an
+/// lvalue of type std::nullptr_t. Unlike a regular lvalue-to-rvalue
+/// conversion, this does not access the object.
+///   std::nullptr_t a, b = a;
+/// The semantics of this are largely the same as CK_NullToPointer,
+/// but it still counts as an lvalue-to-rvalue conversion.
+CAST_OPERATION(NullptrLValueToRValue)
+
 /// CK_NullToPointer - Null pointer constant to pointer, ObjC
 /// pointer, or block pointer.
 ///   (void*) 0
diff --git a/include/clang/AST/OperationKinds.h b/include/clang/AST/OperationKinds.h
index ac512d721e..2a2461b80c 100644
--- a/include/clang/AST/OperationKinds.h
+++ b/include/clang/AST/OperationKinds.h
@@ -23,6 +23,13 @@ enum CastKind {
 #include "clang/AST/OperationKinds.def"
 };
 
+/// Determine whether this cast kind is any kind of lvalue-to-rvalue conversion,
+/// including the oddball nullptr_t lvalue-to-rvalue conversion which doesn't
+/// actually read from memory and so does not constitute an 'access'.
+inline bool isLValueToRValue(CastKind CK) {
+  return CK == CK_LValueToRValue || CK == CK_NullptrLValueToRValue;
+}
+
 enum BinaryOperatorKind {
 #define BINARY_OPERATION(Name, Spelling) BO_##Name,
 #include "clang/AST/OperationKinds.def"
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index 5a80016850..f02246423b 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -1629,6 +1629,7 @@ bool CastExpr::CastConsistency() const {
   case CK_ArrayToPointerDecay:
   case CK_NullToMemberPointer:
   case CK_NullToPointer:
+  case CK_NullptrLValueToRValue:
   case CK_ConstructorConversion:
   case CK_IntegralToPointer:
   case CK_PointerToIntegral:
@@ -2613,7 +2614,7 @@ Expr *Expr::IgnoreParenLValueCasts() {
   while (true) {
     E = E->IgnoreParens();
     if (CastExpr *P = dyn_cast<CastExpr>(E)) {
-      if (P->getCastKind() == CK_LValueToRValue) {
+      if (isLValueToRValue(P->getCastKind())) {
         E = P->getSubExpr();
         continue;
       }
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 837dc9c2a8..d9c3de77f0 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -5908,6 +5908,7 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
       return true;
     return HandleBaseToDerivedCast(Info, E, Result);
 
+  case CK_NullptrLValueToRValue:
   case CK_NullToPointer:
     VisitIgnoredValue(E->getSubExpr());
     return ZeroInitialization(E);
@@ -9699,6 +9700,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_ToUnion:
   case CK_ArrayToPointerDecay:
   case CK_FunctionToPointerDecay:
+  case CK_NullptrLValueToRValue:
   case CK_NullToPointer:
   case CK_NullToMemberPointer:
   case CK_BaseToDerivedMemberPointer:
@@ -10231,6 +10233,7 @@ bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_ToUnion:
   case CK_ArrayToPointerDecay:
   case CK_FunctionToPointerDecay:
+  case CK_NullptrLValueToRValue:
   case CK_NullToPointer:
   case CK_NullToMemberPointer:
   case CK_BaseToDerivedMemberPointer:
diff --git a/lib/Analysis/UninitializedValues.cpp b/lib/Analysis/UninitializedValues.cpp
index 31c88a1095..51afd5d59f 100644
--- a/lib/Analysis/UninitializedValues.cpp
+++ b/lib/Analysis/UninitializedValues.cpp
@@ -490,6 +490,9 @@ void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
 void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
   if (CE->getCastKind() == CK_LValueToRValue)
     classify(CE->getSubExpr(), Use);
+  else if (CE->getCastKind() == CK_NullptrLValueToRValue)
+    // Objects of type 'nullptr_t' are never really used.
+    classify(CE->getSubExpr(), Ignore);
   else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) {
     if (CSE->getType()->isVoidType()) {
       // Squelch any detected load of an uninitialized value if
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index 6ef1091cc0..6271d0e16c 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -4137,6 +4137,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
   case CK_FunctionToPointerDecay:
   case CK_NullToMemberPointer:
   case CK_NullToPointer:
+  case CK_NullptrLValueToRValue:
   case CK_IntegralToPointer:
   case CK_PointerToIntegral:
   case CK_PointerToBoolean:
diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp
index db49b3f28a..d9ec72c430 100644
--- a/lib/CodeGen/CGExprAgg.cpp
+++ b/lib/CodeGen/CGExprAgg.cpp
@@ -813,6 +813,7 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
   case CK_BitCast:
   case CK_ArrayToPointerDecay:
   case CK_FunctionToPointerDecay:
+  case CK_NullptrLValueToRValue:
   case CK_NullToPointer:
   case CK_NullToMemberPointer:
   case CK_BaseToDerivedMemberPointer:
diff --git a/lib/CodeGen/CGExprComplex.cpp b/lib/CodeGen/CGExprComplex.cpp
index 2db693b44c..58a039026a 100644
--- a/lib/CodeGen/CGExprComplex.cpp
+++ b/lib/CodeGen/CGExprComplex.cpp
@@ -475,6 +475,7 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
   case CK_ToUnion:
   case CK_ArrayToPointerDecay:
   case CK_FunctionToPointerDecay:
+  case CK_NullptrLValueToRValue:
   case CK_NullToPointer:
   case CK_NullToMemberPointer:
   case CK_BaseToDerivedMemberPointer:
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp
index c9475840ae..68f6b6537a 100644
--- a/lib/CodeGen/CGExprConstant.cpp
+++ b/lib/CodeGen/CGExprConstant.cpp
@@ -865,6 +865,7 @@ public:
     case CK_PointerToIntegral:
     case CK_PointerToBoolean:
     case CK_NullToPointer:
+    case CK_NullptrLValueToRValue:
     case CK_IntegralCast:
     case CK_BooleanToSignedIntegral:
     case CK_IntegralToPointer:
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp
index f53bb33e46..8b7f3086b5 100644
--- a/lib/CodeGen/CGExprScalar.cpp
+++ b/lib/CodeGen/CGExprScalar.cpp
@@ -2082,6 +2082,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
     return EmitLValue(E).getPointer();
 
   case CK_NullToPointer:
+  case CK_NullptrLValueToRValue:
     if (MustVisitNullValue(E))
       (void) Visit(E);
 
diff --git a/lib/Edit/RewriteObjCFoundationAPI.cpp b/lib/Edit/RewriteObjCFoundationAPI.cpp
index 7c9ab17009..b2bf0eecb2 100644
--- a/lib/Edit/RewriteObjCFoundationAPI.cpp
+++ b/lib/Edit/RewriteObjCFoundationAPI.cpp
@@ -1050,6 +1050,7 @@ static bool rewriteToNumericBoxedExpression(const ObjCMessageExpr *Msg,
     case CK_ToUnion:
     case CK_ArrayToPointerDecay:
     case CK_FunctionToPointerDecay:
+    case CK_NullptrLValueToRValue:
     case CK_NullToPointer:
     case CK_NullToMemberPointer:
     case CK_BaseToDerivedMemberPointer:
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index a8e3b85fe0..a5e82efee5 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -482,6 +482,7 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
       llvm_unreachable("can't implicitly cast lvalue to rvalue with this cast "
                        "kind");
     case CK_LValueToRValue:
+    case CK_NullptrLValueToRValue:
     case CK_ArrayToPointerDecay:
     case CK_FunctionToPointerDecay:
     case CK_ToVoid:
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 4ba0fb12e7..fa7a412ccf 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -623,8 +623,12 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) {
   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_NullptrLValueToRValue : 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
@@ -15899,7 +15903,7 @@ namespace {
     void VisitImplicitCastExpr(ImplicitCastExpr *E) {
       Inherited::VisitImplicitCastExpr(E);
 
-      if (E->getCastKind() == CK_LValueToRValue)
+      if (isLValueToRValue(E->getCastKind()))
         S.UpdateMarkingForLValueToRValue(E->getSubExpr());
     }
   };
diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp
index 57277f6e82..d71c632420 100644
--- a/lib/Sema/SemaInit.cpp
+++ b/lib/Sema/SemaInit.cpp
@@ -7686,9 +7686,12 @@ 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);
+      CurInit = S.DefaultLvalueConversion(CurInit.get());
+      if (!CurInit.isInvalid()) {
+        assert(CurInit.get()->isRValue() && "lvalue-to-rvalue failed");
+        assert(S.Context.hasSameType(CurInit.get()->getType(), Step->Type) &&
+               "lvalue-to-rvalue produced unexpected type");
+      }
       break;
     }
 
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index cc3c25cfb5..fe05e145bf 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -1458,7 +1458,7 @@ namespace {
     }
 
     void VisitCastExpr(CastExpr *E) {
-      if (E->getCastKind() == CK_LValueToRValue)
+      if (isLValueToRValue(E->getCastKind()))
         CheckLValueToRValueCast(E->getSubExpr());
       else
         Visit(E->getSubExpr());
@@ -2766,7 +2766,7 @@ static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef,
     if (!CE->getConstructor()->isCopyConstructor())
       return;
   } else if (const CastExpr *CE = dyn_cast<CastExpr>(InitExpr)) {
-    if (CE->getCastKind() != CK_LValueToRValue)
+    if (isLValueToRValue(CE->getCastKind()))
       return;
   } else {
     return;
diff --git a/lib/Sema/SemaStmtAsm.cpp b/lib/Sema/SemaStmtAsm.cpp
index d209266049..439d5c056c 100644
--- a/lib/Sema/SemaStmtAsm.cpp
+++ b/lib/Sema/SemaStmtAsm.cpp
@@ -48,7 +48,7 @@ static void removeLValueToRValueCast(Expr *E) {
       return;
 
     if (auto *CastE = dyn_cast<CastExpr>(Child))
-      if (CastE->getCastKind() == CK_LValueToRValue) {
+      if (isLValueToRValue(CastE->getCastKind())) {
         ExprUnderCast = CastE->getSubExpr();
         // LValueToRValue cast inside GCCAsmStmt requires an explicit cast.
         ParentCast->setSubExpr(ExprUnderCast);
diff --git a/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/lib/StaticAnalyzer/Core/ExprEngineC.cpp
index 7d47cf4f33..e95bed6b2b 100644
--- a/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -379,7 +379,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);
@@ -502,6 +501,13 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
         Bldr.generateNode(CastE, Pred, state);
         continue;
       }
+      case CK_NullptrLValueToRValue:
+      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);
