[PATCH] D95244: [clang][AST] Handle overload callee type in CallExpr::getCallReturnType.

Balázs Kéri via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 26 03:28:11 PST 2021


balazske updated this revision to Diff 319261.
balazske added a comment.

Adding a test, fixing other problems revealed by the test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95244/new/

https://reviews.llvm.org/D95244

Files:
  clang/lib/AST/Expr.cpp
  clang/unittests/Tooling/SourceCodeTest.cpp


Index: clang/unittests/Tooling/SourceCodeTest.cpp
===================================================================
--- clang/unittests/Tooling/SourceCodeTest.cpp
+++ clang/unittests/Tooling/SourceCodeTest.cpp
@@ -621,4 +621,37 @@
   };
   Visitor.runOver(Code);
 }
+
+TEST(SourceCodeTest, GetCallReturnType) {
+  llvm::StringRef Code = R"cpp(
+template<class T, class F>
+void templ(const T& t, F f) {
+  f(t);
+}
+int f_overload(int) { return 1; }
+int f_overload(double) { return 2; }
+
+struct A {
+  void f(int);
+  void f(double);
+};
+
+void f() {
+  int i = 0;
+  templ(i, [](const auto &p) {
+    f_overload(p);
+    A a;
+    a.f(p);
+  });
+}
+)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
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -1382,6 +1382,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<PointerType>()) {
     CalleeType = FnTypePtr->getPointeeType();
   } else if (const auto *BPT = CalleeType->getAs<BlockPointerType>()) {
@@ -1390,8 +1398,13 @@
     if (isa<CXXPseudoDestructorExpr>(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<FunctionType>();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95244.319261.patch
Type: text/x-patch
Size: 2204 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210126/48e94956/attachment.bin>


More information about the cfe-commits mailing list