[clang] [AST] Teach TextNodeDumper to print the "implicit" bit for coroutine AST nodes (PR #77311)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 9 00:25:41 PST 2024


https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/77311

>From b0efa2e20f1ca7bc9e18b81231574895fe2281c3 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Mon, 8 Jan 2024 14:43:40 +0100
Subject: [PATCH 1/2] [AST] TextNodeDumper print the "implicit" bit for
 coroutine AST nodes

---
 clang/include/clang/AST/TextNodeDumper.h |  2 ++
 clang/lib/AST/TextNodeDumper.cpp         | 10 ++++++
 clang/test/AST/ast-dump-coroutine.cpp    | 46 ++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 clang/test/AST/ast-dump-coroutine.cpp

diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h
index 2f4ed082a0c7ad..dd9ed56fc584a7 100644
--- a/clang/include/clang/AST/TextNodeDumper.h
+++ b/clang/include/clang/AST/TextNodeDumper.h
@@ -252,6 +252,8 @@ class TextNodeDumper
   void VisitGotoStmt(const GotoStmt *Node);
   void VisitCaseStmt(const CaseStmt *Node);
   void VisitReturnStmt(const ReturnStmt *Node);
+  void VisitCoawaitExpr(const CoawaitExpr* Node);
+  void VisitCoreturnStmt(const CoreturnStmt *Node);
   void VisitCompoundStmt(const CompoundStmt *Node);
   void VisitConstantExpr(const ConstantExpr *Node);
   void VisitCallExpr(const CallExpr *Node);
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index e8274fcd5cfe9c..d53688ea862e34 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -1094,6 +1094,16 @@ void clang::TextNodeDumper::VisitReturnStmt(const ReturnStmt *Node) {
   }
 }
 
+void clang::TextNodeDumper::VisitCoawaitExpr(const CoawaitExpr* Node) {
+  if (Node->isImplicit())
+    OS << " implicit";
+}
+
+void clang::TextNodeDumper::VisitCoreturnStmt(const CoreturnStmt *Node) {
+  if (Node->isImplicit())
+    OS << " implicit";
+}
+
 void TextNodeDumper::VisitConstantExpr(const ConstantExpr *Node) {
   if (Node->hasAPValueResult())
     AddChild("value",
diff --git a/clang/test/AST/ast-dump-coroutine.cpp b/clang/test/AST/ast-dump-coroutine.cpp
new file mode 100644
index 00000000000000..f760809f53689a
--- /dev/null
+++ b/clang/test/AST/ast-dump-coroutine.cpp
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -std=c++20 \
+// RUN:    -fsyntax-only -ast-dump -ast-dump-filter test | FileCheck %s
+
+#include "Inputs/std-coroutine.h"
+
+using namespace std;
+
+struct Task {
+  struct promise_type {
+    std::suspend_always initial_suspend() { return {}; }
+    Task get_return_object() {
+      return std::coroutine_handle<promise_type>::from_promise(*this);
+    }
+    std::suspend_always final_suspend() noexcept { return {}; }
+    std::suspend_always return_void() { return {}; }
+    void unhandled_exception() {}
+
+    auto await_transform(int s) {
+      struct awaiter {
+        promise_type *promise;
+        bool await_ready() { return true; }
+        int await_resume() { return 1; }
+        void await_suspend(std::coroutine_handle<>) {}
+      };
+
+      return awaiter{this};
+    }
+  };
+
+  Task(std::coroutine_handle<promise_type> promise);
+
+  std::coroutine_handle<promise_type> handle;
+};
+
+// Verify the implicit AST nodes for coroutines.
+Task test()  {
+  co_await 1;
+}
+// CHECK:        |-DeclStmt {{.*}}
+// CHECK-NEXT:   | `-VarDecl {{.*}} implicit used __promise
+// CHECK-NEXT:   |   `-CXXConstructExpr {{.*}}
+// CHECK-NEXT:   |-ExprWithCleanups {{.*}} 'void'
+// CHECK-NEXT:   | `-CoawaitExpr {{.*}} 'void' implicit
+//                 ...
+// FIXME: the CoreturnStmt should be marked as implicit
+// CHECK: CoreturnStmt {{.*}} <col:6>

>From fb51d8c08de466c87af530edee1b9d2673e030aa Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 9 Jan 2024 09:23:30 +0100
Subject: [PATCH 2/2] Add more tests per review comment.

---
 clang/test/AST/ast-dump-coroutine.cpp | 29 ++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/clang/test/AST/ast-dump-coroutine.cpp b/clang/test/AST/ast-dump-coroutine.cpp
index f760809f53689a..5e7736300f9fee 100644
--- a/clang/test/AST/ast-dump-coroutine.cpp
+++ b/clang/test/AST/ast-dump-coroutine.cpp
@@ -32,15 +32,38 @@ struct Task {
   std::coroutine_handle<promise_type> handle;
 };
 
-// Verify the implicit AST nodes for coroutines.
 Task test()  {
   co_await 1;
+// Writen souce code, verify no implicit bit for the co_await expr.
+// CHECK:        CompoundStmt {{.*}}
+// CHECK-NEXT:   | `-ExprWithCleanups {{.*}} 'int'
+// CHECK-NEXT:   |   `-CoawaitExpr {{.*}} 'int'{{$}}
+// CHECK-NEXT:   |     |-IntegerLiteral {{.*}} <col:12> 'int' 1
+// CHECK-NEXT:   |     |-MaterializeTemporaryExpr {{.*}} 'awaiter'
+// CHECK-NEXT:   |     | `-CXXMemberCallExpr {{.*}} 'awaiter'
+// CHECK-NEXT:   |     |   |-MemberExpr {{.*}} .await_transform
 }
+// Verify the implicit AST nodes for coroutines.
 // CHECK:        |-DeclStmt {{.*}}
 // CHECK-NEXT:   | `-VarDecl {{.*}} implicit used __promise
 // CHECK-NEXT:   |   `-CXXConstructExpr {{.*}}
 // CHECK-NEXT:   |-ExprWithCleanups {{.*}} 'void'
 // CHECK-NEXT:   | `-CoawaitExpr {{.*}} 'void' implicit
-//                 ...
+// CHECK-NEXT:       |-CXXMemberCallExpr  {{.*}} 'std::suspend_always'
+// CHECK-NEXT:       |   | `-MemberExpr {{.*}} .initial_suspend
+//                   ...
+// FIXME: the CoreturnStmt should be marked as implicit
+// CHECK: CoreturnStmt {{.*}} <col:6>{{$}}
+
+Task test2()  {
+// Writen souce code, verify no implicit bit for the co_return expr.
+// CHECK:        CompoundStmt {{.*}}
+// CHECK-NEXT:   | `-CoreturnStmt {{.*}} <line:{{.*}}:{{.*}}>{{$}}
+  co_return;
+}
+// Verify the implicit AST nodes for coroutines.
+// CHECK:        |-DeclStmt {{.*}}
+// CHECK-NEXT:   | `-VarDecl {{.*}} implicit used __promise
+//               ...
 // FIXME: the CoreturnStmt should be marked as implicit
-// CHECK: CoreturnStmt {{.*}} <col:6>
+// CHECK: CoreturnStmt {{.*}} <col:6>{{$}}



More information about the cfe-commits mailing list