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
17 changes: 17 additions & 0 deletions clang/lib/Sema/HeuristicResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/TemplateBase.h"
#include "clang/AST/Type.h"
#include "llvm/Support/Casting.h"

namespace clang {

Expand Down Expand Up @@ -122,6 +124,7 @@ TemplateName getReferencedTemplateName(const Type *T) {
// resolves it to a CXXRecordDecl in which we can try name lookup.
TagDecl *HeuristicResolverImpl::resolveTypeToTagDecl(QualType QT) {
const Type *T = QT.getTypePtrOrNull();

if (!T)
return nullptr;

Expand Down Expand Up @@ -245,6 +248,20 @@ QualType HeuristicResolverImpl::simplifyType(QualType Type, const Expr *E,
}
}
}
if (const auto *TTPT = dyn_cast_if_present<TemplateTypeParmType>(T.Type)) {
// We can't do much useful with a template parameter (e.g. we cannot look
// up member names inside it). However, if the template parameter has a
// default argument, as a heuristic we can replace T with the default
// argument type.
if (const auto *TTPD = TTPT->getDecl()) {
if (TTPD->hasDefaultArgument()) {
const auto &DefaultArg = TTPD->getDefaultArgument().getArgument();
if (DefaultArg.getKind() == TemplateArgument::Type) {
return {DefaultArg.getAsType()};
}
}
}
}
return T;
};
// As an additional protection against infinite loops, bound the number of
Expand Down
34 changes: 34 additions & 0 deletions clang/unittests/Sema/HeuristicResolverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,40 @@ TEST(HeuristicResolver, MemberExpr_HangIssue126536) {
cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"));
}

TEST(HeuristicResolver, MemberExpr_DefaultTemplateArgument) {
std::string Code = R"cpp(
struct Default {
void foo();
};
template <typename T = Default>
void bar(T t) {
t.foo();
}
)cpp";
// Test resolution of "foo" in "t.foo()".
expectResolution(
Code, &HeuristicResolver::resolveMemberExpr,
cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"),
cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_DefaultTemplateArgument_Recursive) {
std::string Code = R"cpp(
struct Default {
void foo();
};
template <typename D = Default, typename T = D>
void bar(T t) {
t.foo();
}
)cpp";
// Test resolution of "foo" in "t.foo()".
expectResolution(
Code, &HeuristicResolver::resolveMemberExpr,
cxxDependentScopeMemberExpr(hasMemberName("foo")).bind("input"),
cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_StaticMethod) {
std::string Code = R"cpp(
template <typename T>
Expand Down