Improve diagnostic location information when checking the initialization of a reference

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82666 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 771af4b..542acb6 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -3571,6 +3571,7 @@
                                                       bool& DerivedToBase);
 
   bool CheckReferenceInit(Expr *&simpleInit_or_initList, QualType declType,
+                          SourceLocation DeclLoc,
                           bool SuppressUserConversions,
                           bool AllowExplicit,
                           bool ForceRValue,
diff --git a/lib/Sema/SemaCXXCast.cpp b/lib/Sema/SemaCXXCast.cpp
index f0ae9a1..d1a8b6d 100644
--- a/lib/Sema/SemaCXXCast.cpp
+++ b/lib/Sema/SemaCXXCast.cpp
@@ -787,6 +787,7 @@
     // get error messages.
     ImplicitConversionSequence ICS;
     bool failed = Self.CheckReferenceInit(SrcExpr, DestType,
+                                          OpRange.getBegin(),
                                           /*SuppressUserConversions=*/false,
                                           /*AllowExplicit=*/false,
                                           /*ForceRValue=*/false,
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 3e83202..c2adc4f 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -3363,6 +3363,7 @@
 /// When @p ForceRValue, we unconditionally treat the initializer as an rvalue.
 bool
 Sema::CheckReferenceInit(Expr *&Init, QualType DeclType,
+                         SourceLocation DeclLoc,
                          bool SuppressUserConversions,
                          bool AllowExplicit, bool ForceRValue,
                          ImplicitConversionSequence *ICS) {
@@ -3381,7 +3382,7 @@
       // Since we're performing this reference-initialization for
       // real, update the initializer with the resulting function.
       if (!ICS) {
-        if (DiagnoseUseOfDecl(Fn, Init->getSourceRange().getBegin()))
+        if (DiagnoseUseOfDecl(Fn, DeclLoc))
           return true;
 
         FixOverloadedFunctionReference(Init, Fn);
@@ -3416,7 +3417,7 @@
   //   A&& r = b;
   if (isRValRef && InitLvalue == Expr::LV_Valid) {
     if (!ICS)
-      Diag(Init->getSourceRange().getBegin(), diag::err_lvalue_to_rvalue_ref)
+      Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref)
         << Init->getSourceRange();
     return true;
   }
@@ -3502,7 +3503,7 @@
     }
 
     OverloadCandidateSet::iterator Best;
-    switch (BestViableFunction(CandidateSet, Init->getLocStart(), Best)) {
+    switch (BestViableFunction(CandidateSet, DeclLoc, Best)) {
     case OR_Success:
       // This is a direct binding.
       BindsDirectly = true;
@@ -3528,7 +3529,7 @@
         return false;
       } else {
         OwningExprResult InitConversion =
-          BuildCXXCastArgument(Init->getLocStart(), QualType(),
+          BuildCXXCastArgument(DeclLoc, QualType(),
                                CastExpr::CK_UserDefinedConversion,
                                cast<CXXMethodDecl>(Best->Function), 
                                Owned(Init));
@@ -3565,8 +3566,7 @@
     // ambiguity (or inaccessibility) unless the reference binding
     // actually happens.
     if (DerivedToBase)
-      return CheckDerivedToBaseConversion(T2, T1,
-                                          Init->getSourceRange().getBegin(),
+      return CheckDerivedToBaseConversion(T2, T1, DeclLoc,
                                           Init->getSourceRange());
     else
       return false;
@@ -3577,8 +3577,7 @@
   //        rvalue reference and the initializer expression shall be an rvalue.
   if (!isRValRef && T1.getCVRQualifiers() != QualType::Const) {
     if (!ICS)
-      Diag(Init->getSourceRange().getBegin(),
-           diag::err_not_reference_to_const_init)
+      Diag(DeclLoc, diag::err_not_reference_to_const_init)
         << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value")
         << T2 << Init->getSourceRange();
     return true;
@@ -3642,8 +3641,7 @@
     // added qualification. But that wasn't the case, so the reference
     // initialization fails.
     if (!ICS)
-      Diag(Init->getSourceRange().getBegin(),
-           diag::err_reference_init_drops_quals)
+      Diag(DeclLoc, diag::err_reference_init_drops_quals)
         << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value")
         << T2 << Init->getSourceRange();
     return true;
@@ -3657,8 +3655,7 @@
   if (SuppressUserConversions && RefRelationship == Ref_Incompatible &&
       (T1->isRecordType() || T2->isRecordType())) {
     if (!ICS)
-      Diag(Init->getSourceRange().getBegin(),
-           diag::err_typecheck_convert_incompatible)
+      Diag(DeclLoc, diag::err_typecheck_convert_incompatible)
         << DeclType << Init->getType() << "initializing" << Init->getSourceRange();
     return true;
   }
@@ -3709,8 +3706,8 @@
         }
       }
       else
-        Diag(Init->getSourceRange().getBegin(), diag::err_lvalue_to_rvalue_ref)
-              << Init->getSourceRange();
+        Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref)
+          << Init->getSourceRange();
     }
     return badConversion;
   }
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index f500e13..ff6ae50 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -1356,6 +1356,7 @@
     //   conversion the reference must bind directly to E1.
     if (!Self.CheckReferenceInit(From,
                             Self.Context.getLValueReferenceType(To->getType()),
+                                 To->getLocStart(),
                                  /*SuppressUserConversions=*/false,
                                  /*AllowExplicit=*/false,
                                  /*ForceRValue=*/false,
@@ -1477,6 +1478,7 @@
     // redoing all the work.
     return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType(
                                         TargetType(ICS)),
+                                   /*FIXME:*/E->getLocStart(),
                                    /*SuppressUserConversions=*/false,
                                    /*AllowExplicit=*/false,
                                    /*ForceRValue=*/false);
@@ -1487,6 +1489,7 @@
            "TryClassUnification should never generate indirect ref bindings");
     return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType(
                                         TargetType(ICS)),
+                                   /*FIXME:*/E->getLocStart(),
                                    /*SuppressUserConversions=*/false,
                                    /*AllowExplicit=*/false,
                                    /*ForceRValue=*/false);
diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp
index c430751..27f0896 100644
--- a/lib/Sema/SemaInit.cpp
+++ b/lib/Sema/SemaInit.cpp
@@ -144,7 +144,7 @@
   //   (8.3.2), shall be initialized by an object, or function, of
   //   type T or by an object that can be converted into a T.
   if (DeclType->isReferenceType())
-    return CheckReferenceInit(Init, DeclType,
+    return CheckReferenceInit(Init, DeclType, InitLoc,
                               /*SuppressUserConversions=*/false,
                               /*AllowExplicit=*/DirectInit,
                               /*ForceRValue=*/false);
@@ -801,6 +801,7 @@
 
     Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
     if (SemaRef.CheckReferenceInit(expr, DeclType,
+                                   /*FIXME:*/expr->getLocStart(),
                                    /*SuppressUserConversions=*/false,
                                    /*AllowExplicit=*/false,
                                    /*ForceRValue=*/false))
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp
index 12eb587..4232d54 100644
--- a/lib/Sema/SemaOverload.cpp
+++ b/lib/Sema/SemaOverload.cpp
@@ -1963,6 +1963,7 @@
   if (ToType->isReferenceType()) {
     ImplicitConversionSequence ICS;
     CheckReferenceInit(From, ToType,
+                       /*FIXME:*/From->getLocStart(),
                        SuppressUserConversions,
                        /*AllowExplicit=*/false,
                        ForceRValue,
@@ -2000,6 +2001,7 @@
 
   if (ToType->isReferenceType())
     return CheckReferenceInit(From, ToType,
+                              /*FIXME:*/From->getLocStart(),
                               /*SuppressUserConversions=*/false,
                               /*AllowExplicit=*/false,
                               /*ForceRValue=*/false);