diff --git a/iwyu.cc b/iwyu.cc index a111d5659..9cb7409f1 100644 --- a/iwyu.cc +++ b/iwyu.cc @@ -3207,7 +3207,7 @@ class InstantiatedTemplateVisitor ast_node != caller_ast_node_; ast_node = ast_node->parent()) { if (preprocessor_info().PublicHeaderIntendsToProvide( GetFileEntry(ast_node->GetLocation()), - GetFileEntry(decl))) + GetFileEntry(decl->getLocation()))) return ast_node->GetLocation(); } return SourceLocation(); // an invalid source-loc @@ -3219,8 +3219,10 @@ class InstantiatedTemplateVisitor bool IsProvidedByTemplate(const Type* type) const { type = RemoveSubstTemplateTypeParm(type); type = RemovePointersAndReferences(type); // get down to the decl - if (const NamedDecl* decl = TypeToDeclAsWritten(type)) + if (const NamedDecl* decl = TypeToDeclAsWritten(type)) { + decl = GetDefinitionAsWritten(decl); return GetLocOfTemplateThatProvides(decl).isValid(); + } return true; // we always provide non-decl types like int, etc. } diff --git a/tests/cxx/explicit_instantiation2-template_helpers.h b/tests/cxx/explicit_instantiation2-template_helpers.h index 5068fe1c8..71c3679e8 100644 --- a/tests/cxx/explicit_instantiation2-template_helpers.h +++ b/tests/cxx/explicit_instantiation2-template_helpers.h @@ -37,4 +37,12 @@ class TemplateTemplateArgShortFwd { T* t; }; +template +class ProvidedTemplate {}; + +template > +class TemplateAsDefaultFullProvided { + T t; +}; + #endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPLICIT_INSTANTIATION2_TEMPLATE_HELPERS_H_ diff --git a/tests/cxx/explicit_instantiation2-template_short.h b/tests/cxx/explicit_instantiation2-template_short.h index 0569349ba..5bae3fe45 100644 --- a/tests/cxx/explicit_instantiation2-template_short.h +++ b/tests/cxx/explicit_instantiation2-template_short.h @@ -12,4 +12,6 @@ extern template class Template; +extern template class ProvidedTemplate; + #endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_EXPLICIT_INSTANTIATION2_TEMPLATE_SHORT_H_ diff --git a/tests/cxx/explicit_instantiation2.cc b/tests/cxx/explicit_instantiation2.cc index 754ddc719..168d6c841 100644 --- a/tests/cxx/explicit_instantiation2.cc +++ b/tests/cxx/explicit_instantiation2.cc @@ -29,6 +29,8 @@ // 6: Fwd-decl use in a template, provided as a default parameter. // 9: Implicit instantiation of Template // 10: Specialization of Template +// 11: Explicit instantiation with a dependent instantiation declaration, but +// provided by the template helper itself. // IWYU: Template is...*explicit_instantiation-template.h @@ -81,6 +83,8 @@ template <> class Template {}; TemplateAsDefaultFull t10; // 10 +TemplateAsDefaultFullProvided<> t11; // 11 + /**** IWYU_SUMMARY tests/cxx/explicit_instantiation2.cc should add these lines: @@ -93,7 +97,7 @@ tests/cxx/explicit_instantiation2.cc should remove these lines: The full include-list for tests/cxx/explicit_instantiation2.cc: #include "explicit_instantiation-template.h" // for Template -#include "explicit_instantiation2-template_helpers.h" // for FullUseArg, FwdDeclUseArg, TemplateAsDefaultFull, TemplateAsDefaultFwd, TemplateTemplateArgShortFull, TemplateTemplateArgShortFwd +#include "explicit_instantiation2-template_helpers.h" // for FullUseArg, FwdDeclUseArg, TemplateAsDefaultFull, TemplateAsDefaultFullProvided, TemplateAsDefaultFwd, TemplateTemplateArgShortFull, TemplateTemplateArgShortFwd #include "explicit_instantiation2-template_short.h" // for Template ***** IWYU_SUMMARY */