[cfe-commits] r95472 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/SemaCXX/warn-weak-vtables.cpp

Anders Carlsson andersca at mac.com
Fri Feb 5 18:27:10 PST 2010


Author: andersca
Date: Fri Feb  5 20:27:10 2010
New Revision: 95472

URL: http://llvm.org/viewvc/llvm-project?rev=95472&view=rev
Log:
Implement a warning diagnostic for weak vtables. Fixes PR6116.

Added:
    cfe/trunk/test/SemaCXX/warn-weak-vtables.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Feb  5 20:27:10 2010
@@ -390,6 +390,11 @@
 def err_deleted_decl_not_first : Error<
   "deleted definition must be first declaration">;
 
+def warn_weak_vtable : Warning<
+  "%0 has no out-of-line virtual method definitions; its vtable will be "
+  "emitted in every translation unit">,
+  InGroup<DiagGroup<"weak-vtables">>, DefaultIgnore;
+
 // C++ exception specifications
 def err_exception_spec_in_typedef : Error<
   "exception specifications are not allowed in typedefs">;

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Feb  5 20:27:10 2010
@@ -5002,9 +5002,16 @@
   if (Record->isDependentContext() || !Record->isDefinition())
     return;
   
-  if (Record->isDynamicClass() && !S.Context.getKeyFunction(Record))
-    S.ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Record, Loc));
+  if (Record->isDynamicClass()) {
+    const CXXMethodDecl *KeyFunction = S.Context.getKeyFunction(Record);
   
+    if (!KeyFunction)
+      S.ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Record, Loc));
+  
+    if ((!KeyFunction || KeyFunction->getBody() && KeyFunction->isInlined()) && 
+        Record->getLinkage() == ExternalLinkage)
+      S.Diag(Record->getLocation(), diag::warn_weak_vtable) << Record;
+  }
   for (DeclContext::decl_iterator D = Record->decls_begin(), 
                                DEnd = Record->decls_end();
        D != DEnd; ++D) {

Added: cfe/trunk/test/SemaCXX/warn-weak-vtables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-weak-vtables.cpp?rev=95472&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/warn-weak-vtables.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-weak-vtables.cpp Fri Feb  5 20:27:10 2010
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -Wweak-vtables
+
+struct A { // expected-warning {{'A' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit}}
+  virtual void f() { } 
+};
+
+template<typename T> struct B {
+  virtual void f() { } 
+};
+
+namespace {
+  struct C { 
+    virtual void f() { }
+  };
+}
+
+void f() {
+  struct A {
+    virtual void f() { }
+  };
+}
\ No newline at end of file





More information about the cfe-commits mailing list