[PATCH] D12326: [Modules] Emit warning if module cannot instantiate static class member.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 4 13:42:37 PDT 2015


rsmith added a subscriber: rsmith.
rsmith added a comment.

I don't really see this as being specific to modules, nor to static data members. The natural generalization of this is:

- at the end of a translation unit (including at the end of a module), warn on any implicit instantiations that are needed but could not be performed because the definition is not available

For valid code, this means we'd warn on templates/temploids which have their definition and all relevant explicit instantiations tucked away in some source file (but for which no explicit instantiation declarations are provided in the relevant header file) -- uncommon, but an idiom used by some. There is at least a syntactic solution we can suggest for people who see the warning: add the missing explicit instantiation declarations to the header file.


================
Comment at: lib/Sema/SemaDecl.cpp:14444-14445
@@ -14403,2 +14443,4 @@
   checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext);
+  InstantiationScanner Scanner(*this);
+  Scanner.TraverseDecl(getASTContext().getTranslationUnitDecl());
 
----------------
This is not the right way to implement the check. `RecursiveASTVisitor`s are generally very expensive, and are poison to modules builds (because they will import all of the contents of all module files).

The right way to handle this is to produce the warning from within `PerformPendingInstantiations` when it's called at the end of the translation unit / module, in the cases where we want to perform an implicit instantiation, the template is not defined, and there is no preceding explicit instantiation declaration.

  warning: instantiation of 'Foo<int>::s_bar' required here, but 'Foo::s_bar' is not defined
  note: add an explicit instantiation declaration to suppress this warning if 'Foo<int>::s_bar' is explicitly instantiated in another translation unit

Extra credit: include a fixit hint in the note to add 'extern template char Foo<int>::s_bar;' somewhere sensible.


http://reviews.llvm.org/D12326





More information about the cfe-commits mailing list