summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ.Ru <juanruben.segovia@gmail.com>2018-11-07 17:56:49 +0100
committerJ.Ru <juanruben.segovia@gmail.com>2018-12-07 09:12:01 +0100
commitedfc3be19932ee20e5b1d7606997a30f7994f87d (patch)
tree9ecd87573424e64caebe7f4423091ff58417a342
parenta9f3656c0d99557bc7f3cc2e829f5823dbc0c706 (diff)
Use the definition to check if decl is provided by template
TypeToDeclAsWritten would not return the written definition in all cases. For templates, for example, the selected declaration could be the one where it was explicitly instantiatiated. That, in turn, affects the source location that is returned and ultimately the outcome of the function. Make sure to always get to the definition before using it to determine whether the template being instantiated provides the decl. Fixes #558
-rw-r--r--iwyu.cc6
-rw-r--r--tests/cxx/explicit_instantiation2-template_helpers.h8
-rw-r--r--tests/cxx/explicit_instantiation2-template_short.h2
-rw-r--r--tests/cxx/explicit_instantiation2.cc6
4 files changed, 19 insertions, 3 deletions
diff --git a/iwyu.cc b/iwyu.cc
index a111d56..9cb7409 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 5068fe1..71c3679 100644
--- a/tests/cxx/explicit_instantiation2-template_helpers.h
+++ b/tests/cxx/explicit_instantiation2-template_helpers.h
@@ -37,4 +37,12 @@ class TemplateTemplateArgShortFwd {
T<short>* t;
};
+template <class T>
+class ProvidedTemplate {};
+
+template <class U = short, class T = ProvidedTemplate<U>>
+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 0569349..5bae3fe 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<short>;
+extern template class ProvidedTemplate<short>;
+
#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 754ddc7..168d6c8 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<int>
// 10: Specialization of Template<T>
+// 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<char> {};
TemplateAsDefaultFull<char> 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 */