From: David Blaikie Date: Fri, 9 Dec 2011 18:32:50 +0000 (+0000) Subject: Provide a separate warning for weak vtables in explicit template instantiations.... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=44d95b55711bc5c2e4055ebf1b5156e7fd3f0196;p=clang Provide a separate warning for weak vtables in explicit template instantiations. There's no (current) way to fix such templates to emit strong symbols/vtables, but perhaps users want to know about the cost being incurred anyway. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146265 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2031d068a3..15824849e7 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -701,6 +701,10 @@ def warn_weak_vtable : Warning< "%0 has no out-of-line virtual method definitions; its vtable will be " "emitted in every translation unit">, InGroup>, DefaultIgnore; +def warn_weak_template_vtable : Warning< + "explicit template instantiation %0 will emit a vtable in every " + "translation unit">, + InGroup>, DefaultIgnore; def ext_using_undefined_std : ExtWarn< "using directive refers to implicitly-defined namespace 'std'">; diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 59c5e7894f..50e07553a1 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -10617,7 +10617,10 @@ bool Sema::DefineUsedVTables() { if (!KeyFunction || (KeyFunction->hasBody(KeyFunctionDef) && KeyFunctionDef->isInlined())) - Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; + Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == + TSK_ExplicitInstantiationDefinition + ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) + << Class; } } VTableUses.clear(); diff --git a/test/SemaCXX/warn-weak-vtables.cpp b/test/SemaCXX/warn-weak-vtables.cpp index 912622f5a7..135e034259 100644 --- a/test/SemaCXX/warn-weak-vtables.cpp +++ b/test/SemaCXX/warn-weak-vtables.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -fsyntax-only -verify -Wweak-vtables +// RUN: %clang_cc1 %s -fsyntax-only -verify -Wweak-vtables -Wweak-template-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() { } @@ -56,3 +56,23 @@ void uses(Parent &p, Derived &d, VeryDerived &vd) { d.getFoo(); vd.getFoo(); } + +template struct TemplVirt { + virtual void f(); +}; + +template class TemplVirt; // expected-warning{{explicit template instantiation 'TemplVirt' will emit a vtable in every translation unit}} + +template<> struct TemplVirt { + virtual void f(); +}; + +template<> struct TemplVirt { // expected-warning{{'TemplVirt' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit}} + virtual void f() {} +}; + +void uses(TemplVirt& f, TemplVirt& b, TemplVirt& l) { + f.f(); + b.f(); + l.f(); +}