Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions clang/lib/Sema/HeuristicResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ QualType HeuristicResolverImpl::simplifyType(QualType Type, const Expr *E,
}
}
}

// Similarly, heuristically replace a template template parameter with its
// default argument if it has one.
if (const auto *TST =
dyn_cast_if_present<TemplateSpecializationType>(T.Type)) {
if (const auto *TTPD = dyn_cast_if_present<TemplateTemplateParmDecl>(
TST->getTemplateName().getAsTemplateDecl())) {
if (TTPD->hasDefaultArgument()) {
const auto &DefaultArg = TTPD->getDefaultArgument().getArgument();
if (DefaultArg.getKind() == TemplateArgument::Template) {
if (const auto *CTD = dyn_cast_if_present<ClassTemplateDecl>(
DefaultArg.getAsTemplate().getAsTemplateDecl())) {
return {Ctx.getCanonicalTagType(CTD->getTemplatedDecl())};
}
}
}
}
}

// Check if the expression refers to an explicit object parameter of
// templated type. If so, heuristically treat it as having the type of the
// enclosing class.
Expand Down
18 changes: 18 additions & 0 deletions clang/unittests/Sema/HeuristicResolverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,24 @@ TEST(HeuristicResolver, MemberExpr_DefaultTemplateArgument_Recursive) {
cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_DefaultTemplateTemplateArgument) {
std::string Code = R"cpp(
template <typename T>
struct vector {
void push_back(T);
};
template <typename Element, template <typename> class Container = vector>
void foo(Container<Element> c, Element e) {
c.push_back(e);
}
)cpp";
// Test resolution of "push_back" in "c.push_back(e)".
expectResolution(
Code, &HeuristicResolver::resolveMemberExpr,
cxxDependentScopeMemberExpr(hasMemberName("push_back")).bind("input"),
cxxMethodDecl(hasName("push_back")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_ExplicitObjectParameter) {
std::string Code = R"cpp(
struct Foo {
Expand Down