[cfe-commits] r110173 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp test/CXX/special/class.dtor/p9.cpp

John McCall rjmccall at apple.com
Tue Aug 3 17:31:26 PDT 2010


Author: rjmccall
Date: Tue Aug  3 19:31:26 2010
New Revision: 110173

URL: http://llvm.org/viewvc/llvm-project?rev=110173&view=rev
Log:
Look through using declarations when deciding whether to use an operator
delete for a virtual destructor.  Diagnose ambiguities.

Fixes PR7803.


Added:
    cfe/trunk/test/CXX/special/class.dtor/p9.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprCXX.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=110173&r1=110172&r2=110173&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Aug  3 19:31:26 2010
@@ -2400,6 +2400,8 @@
   "deleting incomplete class type %0; no conversions to pointer type">;
 def err_no_suitable_delete_member_function_found : Error<
   "no suitable member %0 in %1">;
+def err_ambiguous_suitable_delete_member_function_found : Error<
+  "multiple suitable %0 functions in %1">;
 def note_member_declared_here : Note<
   "member %0 declared here">;
 def err_decrement_bool : Error<"cannot decrement expression of type bool">;

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=110173&r1=110172&r2=110173&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Aug  3 19:31:26 2010
@@ -1298,15 +1298,31 @@
 
   Found.suppressDiagnostics();
 
+  llvm::SmallVector<DeclAccessPair,4> Matches;
   for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
        F != FEnd; ++F) {
-    if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F))
-      if (Delete->isUsualDeallocationFunction()) {
-        Operator = Delete;
-        CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(),
-                              F.getPair());
-        return false;
-      }
+    CXXMethodDecl *Delete = cast<CXXMethodDecl>((*F)->getUnderlyingDecl());
+    if (Delete->isUsualDeallocationFunction())
+      Matches.push_back(F.getPair());
+  }
+
+  // There's exactly one suitable operator;  pick it.
+  if (Matches.size() == 1) {
+    Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl());
+    CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(),
+                          Matches[0]);
+    return false;
+
+  // We found multiple suitable operators;  complain about the ambiguity.
+  } else if (!Matches.empty()) {
+    Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found)
+      << Name << RD;
+
+    for (llvm::SmallVectorImpl<DeclAccessPair>::iterator
+           F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F)
+      Diag((*F)->getUnderlyingDecl()->getLocation(),
+           diag::note_member_declared_here) << Name;
+    return true;
   }
 
   // We did find operator delete/operator delete[] declarations, but
@@ -1316,10 +1332,9 @@
       << Name << RD;
         
     for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
-         F != FEnd; ++F) {
-      Diag((*F)->getLocation(), diag::note_member_declared_here)
-        << Name;
-    }
+         F != FEnd; ++F)
+      Diag((*F)->getUnderlyingDecl()->getLocation(),
+           diag::note_member_declared_here) << Name;
 
     return true;
   }

Added: cfe/trunk/test/CXX/special/class.dtor/p9.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/special/class.dtor/p9.cpp?rev=110173&view=auto
==============================================================================
--- cfe/trunk/test/CXX/special/class.dtor/p9.cpp (added)
+++ cfe/trunk/test/CXX/special/class.dtor/p9.cpp Tue Aug  3 19:31:26 2010
@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+typedef typeof(sizeof(int)) size_t;
+
+// PR7803
+namespace test0 {
+  class A {
+  public:
+    static void operator delete(void *p) {};
+    virtual ~A();
+  };
+
+  class B : protected A {
+  public:
+    ~B();
+  };
+
+  class C : protected B {
+  public:
+    using B::operator delete;
+    ~C();
+  };
+
+  // Shouldn't have an error.
+  C::~C() {}
+}
+
+namespace test1 {
+  class A {
+  public:
+    static void operator delete(void *p) {}; // expected-note {{member 'operator delete' declared here}}
+    virtual ~A();
+  };
+
+  class B : protected A {
+  public:
+    static void operator delete(void *, size_t) {}; // expected-note {{member 'operator delete' declared here}}
+    ~B();
+  };
+
+  class C : protected B {
+  public:
+    using A::operator delete;
+    using B::operator delete;
+
+    ~C(); // expected-error {{multiple suitable 'operator delete' functions in 'C'}}
+  };
+}





More information about the cfe-commits mailing list