[cfe-commits] r83169 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDeclCXX.cpp test/SemaCXX/decl-init-ref.cpp

Fariborz Jahanian fjahanian at apple.com
Wed Sep 30 14:23:30 PDT 2009


Author: fjahanian
Date: Wed Sep 30 16:23:30 2009
New Revision: 83169

URL: http://llvm.org/viewvc/llvm-project?rev=83169&view=rev
Log:
Issue good diagnostics when initializing a refernce type with
a bad initializer. Fixes pr4274.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/decl-init-ref.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=83169&r1=83168&r2=83169&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Sep 30 16:23:30 2009
@@ -474,6 +474,8 @@
 
 // C++ initialization
 def err_lvalue_to_rvalue_ref : Error<"rvalue reference cannot bind to lvalue">;
+def err_invalid_initialization : Error<
+"invalid initialization of reference of type %0 from expression of type %1">;
 def err_lvalue_to_rvalue_ambig_ref : Error<"rvalue reference cannot bind to lvalue "
                                            "due to multiple conversion functions">;
 // FIXME: passing in an English string as %1!

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=83169&r1=83168&r2=83169&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Wed Sep 30 16:23:30 2009
@@ -3703,9 +3703,14 @@
           Diag(Func->getLocation(), diag::err_ovl_candidate);
         }
       }
-      else
-        Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref)
-          << Init->getSourceRange();
+      else {
+        if (isRValRef)
+          Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) 
+            << Init->getSourceRange();
+        else
+          Diag(DeclLoc, diag::err_invalid_initialization)
+            << DeclType << Init->getType() << Init->getSourceRange();
+      }
     }
     return badConversion;
   }

Modified: cfe/trunk/test/SemaCXX/decl-init-ref.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/decl-init-ref.cpp?rev=83169&r1=83168&r2=83169&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/decl-init-ref.cpp (original)
+++ cfe/trunk/test/SemaCXX/decl-init-ref.cpp Wed Sep 30 16:23:30 2009
@@ -18,6 +18,8 @@
 
 extern B f();
 
+const int& ri = (void)0; // expected-error {{invalid initialization of reference of type 'int const &' from expression of type 'void'}}
+
 int main() {
         const A& rca = f(); // expected-error {{rvalue reference cannot bind to lvalue due to multiple conversion functions}}
         A& ra = f(); // expected-error {{non-const lvalue reference to type 'struct A' cannot be initialized with a temporary of type 'class B'}}





More information about the cfe-commits mailing list