[PATCH] D101566: Let -Wweak-template-vtables warn on implicit instantiations

Aaron Puchert via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 30 04:37:44 PDT 2021


aaronpuchert updated this revision to Diff 341845.
aaronpuchert added a comment.

- Fix punctuation in expected warning message.
- Fix test failure: the instantiated class might not be a template, it could also be the member of another template.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101566/new/

https://reviews.llvm.org/D101566

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/warn-weak-vtables.cpp


Index: clang/test/SemaCXX/warn-weak-vtables.cpp
===================================================================
--- clang/test/SemaCXX/warn-weak-vtables.cpp
+++ clang/test/SemaCXX/warn-weak-vtables.cpp
@@ -8,7 +8,7 @@
   virtual void f() { } 
 };
 
-template<typename T> struct B {
+template<typename T> struct B { // expected-warning {{there is no explicit instantiation declaration for 'B<int>'; its vtable will be emitted in every translation unit}}
   virtual void f() { } 
 };
 
@@ -29,7 +29,8 @@
 // Use the vtables
 void uses_abc() {
   A a;
-  B<int> b;
+  B<int> b; // expected-note{{implicit instantiation first required here}}
+  B<C> b_internal;
   C c;
 }
 
@@ -63,7 +64,7 @@
   virtual void f();
 };
 
-template class TemplVirt<float>; // expected-warning{{explicit template instantiation 'TemplVirt<float>' will emit a vtable in every translation unit}}
+template class TemplVirt<float>;
 
 template<> struct TemplVirt<bool> {
   virtual void f();
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -17359,15 +17359,24 @@
     // no key function or the key function is inlined. Don't warn in C++ ABIs
     // that lack key functions, since the user won't be able to make one.
     if (Context.getTargetInfo().getCXXABI().hasKeyFunctions() &&
-        Class->isExternallyVisible() && ClassTSK != TSK_ImplicitInstantiation) {
+        Class->isExternallyVisible() &&
+        ClassTSK != TSK_ExplicitInstantiationDefinition) {
       const FunctionDecl *KeyFunctionDef = nullptr;
       if (!KeyFunction || (KeyFunction->hasBody(KeyFunctionDef) &&
                            KeyFunctionDef->isInlined())) {
-        Diag(Class->getLocation(),
-             ClassTSK == TSK_ExplicitInstantiationDefinition
-                 ? diag::warn_weak_template_vtable
-                 : diag::warn_weak_vtable)
-            << Class;
+        if (ClassTSK == TSK_ImplicitInstantiation) {
+          Diag(Class->getLocation(), diag::warn_weak_template_vtable) << Class;
+
+          SourceLocation InstLoc;
+          if (const auto *CTSD =
+                  dyn_cast<ClassTemplateSpecializationDecl>(Class))
+            InstLoc = CTSD->getPointOfInstantiation();
+          else
+            InstLoc =
+                Class->getMemberSpecializationInfo()->getPointOfInstantiation();
+          Diag(InstLoc, diag::note_instantiation_required_here) << false;
+        } else
+          Diag(Class->getLocation(), diag::warn_weak_vtable) << Class;
       }
     }
   }
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1583,8 +1583,8 @@
   "emitted in every translation unit">,
   InGroup<DiagGroup<"weak-vtables">>, DefaultIgnore;
 def warn_weak_template_vtable : Warning<
-  "explicit template instantiation %0 will emit a vtable in every "
-  "translation unit">,
+  "there is no explicit instantiation declaration for %0; its vtable will be "
+  "emitted in every translation unit">,
   InGroup<DiagGroup<"weak-template-vtables">>, DefaultIgnore;
 
 def ext_using_undefined_std : ExtWarn<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101566.341845.patch
Type: text/x-patch
Size: 3314 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210430/43b4dd1e/attachment.bin>


More information about the cfe-commits mailing list