[cfe-commits] r107372 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaOverload.cpp test/SemaCXX/overload-call.cpp test/SemaObjCXX/objc-pointer-conv.mm

Douglas Gregor dgregor at apple.com
Wed Jun 30 19:14:46 PDT 2010


Author: dgregor
Date: Wed Jun 30 21:14:45 2010
New Revision: 107372

URL: http://llvm.org/viewvc/llvm-project?rev=107372&view=rev
Log:
Extend the "cannot convert from base class pointer to derived class
pointer" diagnostic to handle references, too.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaOverload.cpp
    cfe/trunk/test/SemaCXX/overload-call.cpp
    cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=107372&r1=107371&r2=107372&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jun 30 21:14:45 2010
@@ -1203,14 +1203,16 @@
     "%select{const|volatile|const and volatile|restrict|const and restrict|"
     "volatile and restrict|const, volatile, and restrict}3 qualifier"
     "%select{||s||s|s|s}3">;
-def note_ovl_candidate_bad_base_to_derived_ptr_conv : Note<"candidate "
+def note_ovl_candidate_bad_base_to_derived_conv : Note<"candidate "
     "%select{function|function|constructor|"
     "function |function |constructor |"
     "constructor (the implicit default constructor)|"
     "constructor (the implicit copy constructor)|"
     "function (the implicit copy assignment operator)}0%1"
-    " not viable: cannot convert from base class pointer %2 to derived class "
-    "pointer %3 for %ordinal4 argument">;
+    " not viable: cannot %select{convert from|convert from|bind}2 "
+    "%select{base class pointer|superclass|base class object of type}2 %3 to "
+    "%select{derived class pointer|subclass|derived class reference}2 %4 for "
+    "%ordinal5 argument">;
 
 def note_ambiguous_type_conversion: Note<
     "because of ambiguity in conversion of %0 to %1">;

Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=107372&r1=107371&r2=107372&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Wed Jun 30 21:14:45 2010
@@ -5486,7 +5486,7 @@
   }
 
   // Diagnose base -> derived pointer conversions.
-  bool IsBaseToDerivedConversion = false;
+  unsigned BaseToDerivedConversion = 0;
   if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) {
     if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) {
       if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs(
@@ -5495,7 +5495,7 @@
           !ToPtrTy->getPointeeType()->isIncompleteType() &&
           S.IsDerivedFrom(ToPtrTy->getPointeeType(), 
                           FromPtrTy->getPointeeType()))
-        IsBaseToDerivedConversion = true;
+        BaseToDerivedConversion = 1;
     }
   } else if (const ObjCObjectPointerType *FromPtrTy
                                     = FromTy->getAs<ObjCObjectPointerType>()) {
@@ -5506,13 +5506,21 @@
           if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs(
                                                 FromPtrTy->getPointeeType()) &&
               FromIface->isSuperClassOf(ToIface))
-            IsBaseToDerivedConversion = true;
-  }
-  if (IsBaseToDerivedConversion) {
+            BaseToDerivedConversion = 2;
+  } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) {
+      if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) &&
+          !FromTy->isIncompleteType() &&
+          !ToRefTy->getPointeeType()->isIncompleteType() &&
+          S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy))
+        BaseToDerivedConversion = 3;
+    }
+    
+  if (BaseToDerivedConversion) {
     S.Diag(Fn->getLocation(), 
-           diag::note_ovl_candidate_bad_base_to_derived_ptr_conv)
+           diag::note_ovl_candidate_bad_base_to_derived_conv)
       << (unsigned) FnKind << FnDesc
       << (FromExpr ? FromExpr->getSourceRange() : SourceRange())
+      << (BaseToDerivedConversion - 1)
       << FromTy << ToTy << I+1;    
     return;
   }

Modified: cfe/trunk/test/SemaCXX/overload-call.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/overload-call.cpp?rev=107372&r1=107371&r2=107372&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/overload-call.cpp (original)
+++ cfe/trunk/test/SemaCXX/overload-call.cpp Wed Jun 30 21:14:45 2010
@@ -236,6 +236,12 @@
   float* ir2 = intref(5.5);
 }
 
+void derived5(C&); // expected-note{{candidate function not viable: cannot bind base class object of type 'A' to derived class reference 'C &' for 1st argument}}
+
+void test_base(A& a) {
+  derived5(a); // expected-error{{no matching function for call to 'derived5}}
+}
+
 // Test reference binding vs. standard conversions.
 int& bind_vs_conv(const double&);
 float& bind_vs_conv(int);

Modified: cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm?rev=107372&r1=107371&r2=107372&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm (original)
+++ cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm Wed Jun 30 21:14:45 2010
@@ -39,7 +39,7 @@
 @interface DerivedFromI : I
 @end
 
-void accept_derived(DerivedFromI*); // expected-note{{candidate function not viable: cannot convert from base class pointer 'I *' to derived class pointer 'DerivedFromI *' for 1st argument}}
+void accept_derived(DerivedFromI*); // expected-note{{candidate function not viable: cannot convert from superclass 'I *' to subclass 'DerivedFromI *' for 1st argument}}
 
 void test_base_to_derived(I* i) {
   accept_derived(i); // expected-error{{no matching function for call to 'accept_derived'}}





More information about the cfe-commits mailing list