summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ.Ru <juanruben.segovia@gmail.com>2018-11-07 17:56:49 +0100
committerKim Grasman <kim.grasman@gmail.com>2018-12-08 15:03:42 +0100
commit5658531781696e251804a13a3c033b74b0e39915 (patch)
treeb0fc9b31e05a9a1833377e077349f8238cbe06f8
parent6ed513b2eaa34059613814e45d9c87a4adaf6cc9 (diff)
Use the definition to check if decl is provided by templateclang_7.00.11clang_7.0
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 18e7ed1..5dbb34b 100644
--- a/iwyu.cc
+++ b/iwyu.cc
@@ -3206,7 +3206,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
@@ -3218,8 +3218,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 */