[clang] 5ff992b - [DEBUG-INFO] Change how we handle auto return types for lambda operator() to be consistent with gcc

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 21 14:58:56 PDT 2022


Author: Shafik Yaghmour
Date: 2022-04-21T14:58:50-07:00
New Revision: 5ff992bca208a0e37ca6338fc735aec6aa848b72

URL: https://github.com/llvm/llvm-project/commit/5ff992bca208a0e37ca6338fc735aec6aa848b72
DIFF: https://github.com/llvm/llvm-project/commit/5ff992bca208a0e37ca6338fc735aec6aa848b72.diff

LOG: [DEBUG-INFO] Change how we handle auto return types for lambda operator() to be consistent with gcc

D70524 added support for auto return types for C++ member functions. I was
implementing support on the LLDB side for looking up the deduced type.

I ran into trouble with some cases with respect to lambdas. I looked into
how gcc was handling these cases and it appears gcc emits the deduced return type for lambdas.

So I am changing out behavior to match that.

Differential Revision: https://reviews.llvm.org/D123319

Added: 
    clang/test/CodeGenCXX/no_auto_return_lambda.cpp

Modified: 
    clang/lib/CodeGen/CGDebugInfo.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 4bfa8461eefea..846b9db76dd4a 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -1679,9 +1679,17 @@ CGDebugInfo::getOrCreateInstanceMethodType(QualType ThisPtr,
   SmallVector<llvm::Metadata *, 16> Elts;
   // First element is always return type. For 'void' functions it is NULL.
   QualType temp = Func->getReturnType();
-  if (temp->getTypeClass() == Type::Auto && decl)
-    Elts.push_back(CreateType(cast<AutoType>(temp)));
-  else
+  if (temp->getTypeClass() == Type::Auto && decl) {
+    const AutoType *AT = cast<AutoType>(temp);
+
+    // It may be tricky in some cases to link the specification back the lambda
+    // call operator and so we skip emitting "auto" for lambdas. This is
+    // consistent with gcc as well.
+    if (AT->isDeduced() && ThisPtr->getPointeeCXXRecordDecl()->isLambda())
+      Elts.push_back(getOrCreateType(AT->getDeducedType(), Unit));
+    else
+      Elts.push_back(CreateType(AT));
+  } else
     Elts.push_back(Args[0]);
 
   // "this" pointer is always first argument.

diff  --git a/clang/test/CodeGenCXX/no_auto_return_lambda.cpp b/clang/test/CodeGenCXX/no_auto_return_lambda.cpp
new file mode 100644
index 0000000000000..76a98263f7668
--- /dev/null
+++ b/clang/test/CodeGenCXX/no_auto_return_lambda.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// We emit "auto" for deduced return types for member functions but we should
+// not emitting "auto" for deduced return types for lambdas call function which
+// will be implmented as operator() in a class type. This test will verify that
+// behavior.
+
+__attribute__((used)) int g() {
+  auto f = []() { return 10; };
+  return f();
+}
+
+// g() is not a member function so we should not emit "auto" for the deduced
+// return type.
+//
+// CHECK: !DISubprogram(name: "g",{{.*}}, type: ![[FUN_TYPE:[0-9]+]],{{.*}}
+// CHECK: ![[FUN_TYPE]] = !DISubroutineType(types: ![[TYPE_NODE:[0-9]+]])
+// CHECK: ![[TYPE_NODE]] = !{![[INT_TYPE:[0-9]+]]}
+// CHECK: ![[INT_TYPE]] = !DIBasicType(name: "int", {{.*}})
+
+// operator() of the local lambda should have the same return type as g()
+//
+// CHECK: distinct !DISubprogram(name: "operator()",{{.*}}, type: ![[FUN_TYPE_LAMBDA:[0-9]+]],{{.*}}
+// CHECK: ![[FUN_TYPE_LAMBDA]] = !DISubroutineType(types: ![[TYPE_NODE_LAMBDA:[0-9]+]])
+// CHECK: ![[TYPE_NODE_LAMBDA]] = !{![[INT_TYPE:[0-9]+]], {{.*}}


        


More information about the cfe-commits mailing list