avogelsgesang created this revision.
avogelsgesang added reviewers: ChuanqiXu, aprantl, dblaikie, ychen, jryans, 
JDevlieghere.
Herald added a project: All.
avogelsgesang requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

With this commit, the `std::coroutine_handle` pretty printer now
recognizes `std::noop_coroutine()` handles. For noop coroutine handles,
we identify use the summary string `noop_coroutine` and we don't print
children

Instead of

  (std::coroutine_handle<void>) $3 = coro frame = 0x555555559058 {
    resume = 0x00005555555564f0 
(a.out`std::__1::coroutine_handle<std::__1::noop_coroutine_promise>::__noop_coroutine_frame_ty_::__dummy_resume_destroy_func()
 at noop_coroutine_handle.h:79)
    destroy = 0x00005555555564f0 
(a.out`std::__1::coroutine_handle<std::__1::noop_coroutine_promise>::__noop_coroutine_frame_ty_::__dummy_resume_destroy_func()
 at noop_coroutine_handle.h:79)
  }

we now print

  (std::coroutine_handle<void>) $3 = noop_coroutine


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132735

Files:
  lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp

Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp
===================================================================
--- lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp
@@ -36,6 +36,7 @@
   std::coroutine_handle<> type_erased_hdl = gen.hdl;
   std::coroutine_handle<int> incorrectly_typed_hdl =
       std::coroutine_handle<int>::from_address(gen.hdl.address());
+  std::coroutine_handle<> noop_hdl = std::noop_coroutine();
   gen.hdl.resume();                            // Break at initial_suspend
   gen.hdl.resume();                            // Break after co_yield
   empty_function_so_we_can_set_a_breakpoint(); // Break at final_suspend
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
===================================================================
--- lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
@@ -57,6 +57,11 @@
                 ValueCheck(name="destroy", summary = test_generator_func_ptr_re),
                 ValueCheck(name="promise", value="-1")
             ])
+        # We recognize and pretty-print `std::noop_coroutine`. We don't display
+        # any children as those are irrelevant for the noop coroutine.
+        self.expect_expr("noop_hdl",
+            result_summary=re.compile("^noop_coroutine$"),
+            result_children=[])
 
         # Run until after the `co_yield`
         process = self.process()
Index: lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
===================================================================
--- lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
@@ -34,7 +34,7 @@
   return ptr_sp;
 }
 
-static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
+static Function *ExtractFunction(ValueObjectSP &frame_ptr_sp, int offset) {
   lldb::TargetSP target_sp = frame_ptr_sp->GetTargetSP();
   lldb::ProcessSP process_sp = frame_ptr_sp->GetProcessSP();
   auto ptr_size = process_sp->GetAddressByteSize();
@@ -46,24 +46,64 @@
   lldbassert(addr_type == AddressType::eAddressTypeLoad);
 
   Status error;
-  // The destroy pointer is the 2nd pointer inside the compiler-generated
-  // `pair<resumePtr,destroyPtr>`. It
-  auto destroy_func_ptr_addr = frame_ptr_addr + ptr_size;
-  lldb::addr_t destroy_func_addr =
-      process_sp->ReadPointerFromMemory(destroy_func_ptr_addr, error);
+  auto func_ptr_addr = frame_ptr_addr + offset * ptr_size;
+  lldb::addr_t func_addr =
+      process_sp->ReadPointerFromMemory(func_ptr_addr, error);
   if (error.Fail())
     return nullptr;
 
-  Address destroy_func_address;
-  if (!target_sp->ResolveLoadAddress(destroy_func_addr, destroy_func_address))
+  Address func_address;
+  if (!target_sp->ResolveLoadAddress(func_addr, func_address))
     return nullptr;
 
-  Function *destroy_func =
-      destroy_func_address.CalculateSymbolContextFunction();
-  if (!destroy_func)
-    return nullptr;
+  return func_address.CalculateSymbolContextFunction();
+}
 
-  return destroy_func;
+static Function *ExtractResumeFunction(ValueObjectSP &frame_ptr_sp) {
+  return ExtractFunction(frame_ptr_sp, 1);
+}
+
+static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
+  return ExtractFunction(frame_ptr_sp, 1);
+}
+
+static bool IsNoopResumeDestroy(Function *f) {
+  if (!f)
+    return false;
+
+  // clang's `__builtin_coro_noop` gets lowered to
+  // `_NoopCoro_ResumeDestroy`. This is used by libc++
+  // on clang.
+  auto mangledName = f->GetMangled().GetMangledName();
+  if (mangledName == "__NoopCoro_ResumeDestroy")
+    return true;
+
+  // libc++ uses the following name as a fallback on
+  // compilers without `__builtin_coro_noop`.
+  auto name = f->GetNameNoArguments();
+  static RegularExpression libcxxRegex(
+      "^std::coroutine_handle<std::noop_coroutine_promise>::"
+      "__noop_coroutine_frame_ty_::__dummy_resume_destroy_func$");
+  lldbassert(libcxxRegex.IsValid());
+  if (libcxxRegex.Execute(name.GetStringRef()))
+    return true;
+  static RegularExpression libcxxRegexAbiNS(
+      "^std::__[[:alnum:]]+::coroutine_handle<std::__[[:alnum:]]+::"
+      "noop_coroutine_promise>::__noop_coroutine_frame_ty_::"
+      "__dummy_resume_destroy_func$");
+  lldbassert(libcxxRegexAbiNS.IsValid());
+  if (libcxxRegexAbiNS.Execute(name.GetStringRef()))
+    return true;
+
+  // libstdc++ uses the following name on both gcc and clang.
+  static RegularExpression libstdcppRegex(
+      "^std::__[[:alnum:]]+::coroutine_handle<std::__[[:alnum:]]+::"
+      "noop_coroutine_promise>::__frame::__dummy_resume_destroy$");
+  lldbassert(libstdcppRegex.IsValid());
+  if (libstdcppRegex.Execute(name.GetStringRef()))
+    return true;
+
+  return false;
 }
 
 static CompilerType InferPromiseType(Function &destroy_func) {
@@ -113,9 +153,15 @@
 
   if (!ptr_sp->GetValueAsUnsigned(0)) {
     stream << "nullptr";
-  } else {
-    stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
+    return true;
   }
+  if (IsNoopResumeDestroy(ExtractResumeFunction(ptr_sp)) &&
+      IsNoopResumeDestroy(ExtractDestroyFunction(ptr_sp))) {
+    stream << "noop_coroutine";
+    return true;
+  }
+
+  stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
   return true;
 }
 
@@ -157,6 +203,14 @@
   if (!ptr_sp)
     return false;
 
+  Function *resume_func = ExtractResumeFunction(ptr_sp);
+  Function *destroy_func = ExtractDestroyFunction(ptr_sp);
+
+  if (IsNoopResumeDestroy(resume_func) && IsNoopResumeDestroy(destroy_func)) {
+    // For `std::noop_coroutine()`, we don't want to display any child nodes.
+    return false;
+  }
+
   // Get the `promise_type` from the template argument
   CompilerType promise_type(
       valobj_sp->GetCompilerType().GetTypeTemplateArgument(0));
@@ -164,11 +218,9 @@
     return false;
 
   // Try to infer the promise_type if it was type-erased
-  if (promise_type.IsVoidType()) {
-    if (Function *destroy_func = ExtractDestroyFunction(ptr_sp)) {
-      if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
-        promise_type = inferred_type;
-      }
+  if (promise_type.IsVoidType() && destroy_func) {
+    if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
+      promise_type = inferred_type;
     }
   }
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to