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

Douglas Gregor dgregor at apple.com
Wed Jun 30 16:01:40 PDT 2010


Author: dgregor
Date: Wed Jun 30 18:01:39 2010
New Revision: 107349

URL: http://llvm.org/viewvc/llvm-project?rev=107349&view=rev
Log:
Improve diagnostic when we fail to pick an overload because it would
require a base-to-derived pointer conversion.

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
    cfe/trunk/www/compatibility.html

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=107349&r1=107348&r2=107349&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jun 30 18:01:39 2010
@@ -1203,6 +1203,15 @@
     "%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 "
+    "%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">;
+
 def note_ambiguous_type_conversion: Note<
     "because of ambiguity in conversion of %0 to %1">;
 def note_ovl_builtin_binary_candidate : Note<

Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=107349&r1=107348&r2=107349&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Wed Jun 30 18:01:39 2010
@@ -5485,6 +5485,38 @@
     return;
   }
 
+  // Diagnose base -> derived pointer conversions.
+  bool IsBaseToDerivedConversion = false;
+  if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) {
+    if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) {
+      if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs(
+                                               FromPtrTy->getPointeeType()) &&
+          !FromPtrTy->getPointeeType()->isIncompleteType() &&
+          !ToPtrTy->getPointeeType()->isIncompleteType() &&
+          S.IsDerivedFrom(ToPtrTy->getPointeeType(), 
+                          FromPtrTy->getPointeeType()))
+        IsBaseToDerivedConversion = true;
+    }
+  } else if (const ObjCObjectPointerType *FromPtrTy
+                                    = FromTy->getAs<ObjCObjectPointerType>()) {
+    if (const ObjCObjectPointerType *ToPtrTy
+                                        = ToTy->getAs<ObjCObjectPointerType>())
+      if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl())
+        if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl())
+          if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs(
+                                                FromPtrTy->getPointeeType()) &&
+              FromIface->isSuperClassOf(ToIface))
+            IsBaseToDerivedConversion = true;
+  }
+  if (IsBaseToDerivedConversion) {
+    S.Diag(Fn->getLocation(), 
+           diag::note_ovl_candidate_bad_base_to_derived_ptr_conv)
+      << (unsigned) FnKind << FnDesc
+      << (FromExpr ? FromExpr->getSourceRange() : SourceRange())
+      << FromTy << ToTy << I+1;    
+    return;
+  }
+      
   // TODO: specialize more based on the kind of mismatch
   S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv)
     << (unsigned) FnKind << FnDesc

Modified: cfe/trunk/test/SemaCXX/overload-call.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/overload-call.cpp?rev=107349&r1=107348&r2=107349&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/overload-call.cpp (original)
+++ cfe/trunk/test/SemaCXX/overload-call.cpp Wed Jun 30 18:01:39 2010
@@ -219,6 +219,12 @@
   char* d8 = derived3(d);
 }
 
+void derived4(C*); // expected-note{{candidate function not viable: cannot convert from base class pointer 'A *' to derived class pointer 'C *' for 1st argument}}
+
+void test_base(A* a) {
+  derived4(a); // expected-error{{no matching function for call to 'derived4}}
+}
+
 // Test overloading of references. 
 // (FIXME: tests binding to determine candidate sets, not overload 
 //  resolution per se).

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=107349&r1=107348&r2=107349&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm (original)
+++ cfe/trunk/test/SemaObjCXX/objc-pointer-conv.mm Wed Jun 30 18:01:39 2010
@@ -36,3 +36,11 @@
   Func(p);		// expected-error {{no matching function for call to 'Func'}}
 }
 
+ at interface DerivedFromI : I
+ at 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 test_base_to_derived(I* i) {
+  accept_derived(i); // expected-error{{no matching function for call to 'accept_derived'}}
+}

Modified: cfe/trunk/www/compatibility.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/www/compatibility.html?rev=107349&r1=107348&r2=107349&view=diff
==============================================================================
--- cfe/trunk/www/compatibility.html (original)
+++ cfe/trunk/www/compatibility.html Wed Jun 30 18:01:39 2010
@@ -590,8 +590,9 @@
 downcast.mm:6:3: error: no matching function for call to 'f'
   f(base);
   ^
-downcast.mm:4:6: note: candidate function not viable: no known conversion from
-      'Base *' to 'Derived *' for 1st argument
+downcast.mm:4:6: note: candidate function not viable: cannot convert from base
+      class pointer 'Base *' to derived class pointer 'Derived *' for 1st
+      argument
 void f(Derived *);
      ^
 </pre>





More information about the cfe-commits mailing list