diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1383,6 +1383,14 @@ QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const { const Expr *Callee = getCallee(); QualType CalleeType = Callee->getType(); + + auto CreateDependentType = [&Ctx]() { + ASTContext::GetBuiltinTypeError Error; + QualType RetTy = Ctx.GetBuiltinType(BuiltinType::Dependent, Error); + assert(Error == ASTContext::GE_None); + return RetTy; + }; + if (const auto *FnTypePtr = CalleeType->getAs()) { CalleeType = FnTypePtr->getPointeeType(); } else if (const auto *BPT = CalleeType->getAs()) { @@ -1391,8 +1399,13 @@ if (isa(Callee->IgnoreParens())) return Ctx.VoidTy; - // This should never be overloaded and so should never return null. CalleeType = Expr::findBoundMemberType(Callee); + if (CalleeType.isNull()) + return CreateDependentType(); + + } else if (CalleeType->isDependentType() || + CalleeType->isSpecificPlaceholderType(BuiltinType::Overload)) { + return CreateDependentType(); } const FunctionType *FnType = CalleeType->castAs(); diff --git a/clang/unittests/Tooling/SourceCodeTest.cpp b/clang/unittests/Tooling/SourceCodeTest.cpp --- a/clang/unittests/Tooling/SourceCodeTest.cpp +++ b/clang/unittests/Tooling/SourceCodeTest.cpp @@ -621,4 +621,63 @@ }; Visitor.runOver(Code); } + +TEST(SourceCodeTest, GetCallReturnType_Callee_UnresolvedLookupExpr) { + llvm::StringRef Code = R"cpp( +template +void templ(const T& t, F f) { + f(t); + // CalleeType in getCallReturntype is Overload and dependent +} +int f_overload(int) { return 1; } +int f_overload(double) { return 2; } + +void f() { + int i = 0; + templ(i, [](const auto &p) { + f_overload(p); // UnresolvedLookupExpr + // CalleeType in getCallReturntype is Overload and not dependent + }); +} +)cpp"; + + CallsVisitor Visitor; + Visitor.OnCall = [](CallExpr *Expr, ASTContext *Context) { + // Should not crash. + (void)Expr->getCallReturnType(*Context); + }; + Visitor.runOver(Code, CallsVisitor::Lang_CXX14); +} + +TEST(SourceCodeTest, GetCallReturnType_Callee_UnresolvedMemberExpr) { + llvm::StringRef Code = R"cpp( +template +void templ(const T& t, F f) { + f(t); + // CalleeType in getCallReturntype is Overload and dependent +} + +struct A { + void f_overload(int); + void f_overload(double); +}; + +void f() { + int i = 0; + templ(i, [](const auto &p) { + A a; + a.f_overload(p); // UnresolvedMemberExpr + // CalleeType in getCallReturntype is BoundMember and has overloads + }); +} +)cpp"; + + CallsVisitor Visitor; + Visitor.OnCall = [](CallExpr *Expr, ASTContext *Context) { + // Should not crash. + (void)Expr->getCallReturnType(*Context); + }; + Visitor.runOver(Code, CallsVisitor::Lang_CXX14); +} + } // end anonymous namespace