Skip to content

Commit 414ea3a

Browse files
authored
[AST] Teach TextNodeDumper to print the "implicit" bit for coroutine AST nodes (llvm#77311)
1 parent 243a582 commit 414ea3a

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

clang/include/clang/AST/TextNodeDumper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ class TextNodeDumper
252252
void VisitGotoStmt(const GotoStmt *Node);
253253
void VisitCaseStmt(const CaseStmt *Node);
254254
void VisitReturnStmt(const ReturnStmt *Node);
255+
void VisitCoawaitExpr(const CoawaitExpr *Node);
256+
void VisitCoreturnStmt(const CoreturnStmt *Node);
255257
void VisitCompoundStmt(const CompoundStmt *Node);
256258
void VisitConstantExpr(const ConstantExpr *Node);
257259
void VisitCallExpr(const CallExpr *Node);

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,16 @@ void clang::TextNodeDumper::VisitReturnStmt(const ReturnStmt *Node) {
10941094
}
10951095
}
10961096

1097+
void clang::TextNodeDumper::VisitCoawaitExpr(const CoawaitExpr *Node) {
1098+
if (Node->isImplicit())
1099+
OS << " implicit";
1100+
}
1101+
1102+
void clang::TextNodeDumper::VisitCoreturnStmt(const CoreturnStmt *Node) {
1103+
if (Node->isImplicit())
1104+
OS << " implicit";
1105+
}
1106+
10971107
void TextNodeDumper::VisitConstantExpr(const ConstantExpr *Node) {
10981108
if (Node->hasAPValueResult())
10991109
AddChild("value",

clang/test/AST/ast-dump-coroutine.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -std=c++20 \
2+
// RUN: -fsyntax-only -ast-dump -ast-dump-filter test | FileCheck %s
3+
4+
#include "Inputs/std-coroutine.h"
5+
6+
using namespace std;
7+
8+
struct Task {
9+
struct promise_type {
10+
std::suspend_always initial_suspend() { return {}; }
11+
Task get_return_object() {
12+
return std::coroutine_handle<promise_type>::from_promise(*this);
13+
}
14+
std::suspend_always final_suspend() noexcept { return {}; }
15+
std::suspend_always return_void() { return {}; }
16+
void unhandled_exception() {}
17+
18+
auto await_transform(int s) {
19+
struct awaiter {
20+
promise_type *promise;
21+
bool await_ready() { return true; }
22+
int await_resume() { return 1; }
23+
void await_suspend(std::coroutine_handle<>) {}
24+
};
25+
26+
return awaiter{this};
27+
}
28+
};
29+
30+
Task(std::coroutine_handle<promise_type> promise);
31+
32+
std::coroutine_handle<promise_type> handle;
33+
};
34+
35+
Task test() {
36+
co_await 1;
37+
// Writen souce code, verify no implicit bit for the co_await expr.
38+
// CHECK: CompoundStmt {{.*}}
39+
// CHECK-NEXT: | `-ExprWithCleanups {{.*}} 'int'
40+
// CHECK-NEXT: | `-CoawaitExpr {{.*}} 'int'{{$}}
41+
// CHECK-NEXT: | |-IntegerLiteral {{.*}} <col:12> 'int' 1
42+
// CHECK-NEXT: | |-MaterializeTemporaryExpr {{.*}} 'awaiter'
43+
// CHECK-NEXT: | | `-CXXMemberCallExpr {{.*}} 'awaiter'
44+
// CHECK-NEXT: | | |-MemberExpr {{.*}} .await_transform
45+
}
46+
// Verify the implicit AST nodes for coroutines.
47+
// CHECK: |-DeclStmt {{.*}}
48+
// CHECK-NEXT: | `-VarDecl {{.*}} implicit used __promise
49+
// CHECK-NEXT: | `-CXXConstructExpr {{.*}}
50+
// CHECK-NEXT: |-ExprWithCleanups {{.*}} 'void'
51+
// CHECK-NEXT: | `-CoawaitExpr {{.*}} 'void' implicit
52+
// CHECK-NEXT: |-CXXMemberCallExpr {{.*}} 'std::suspend_always'
53+
// CHECK-NEXT: | | `-MemberExpr {{.*}} .initial_suspend
54+
// ...
55+
// FIXME: the CoreturnStmt should be marked as implicit
56+
// CHECK: CoreturnStmt {{.*}} <col:6>{{$}}
57+
58+
Task test2() {
59+
// Writen souce code, verify no implicit bit for the co_return expr.
60+
// CHECK: CompoundStmt {{.*}}
61+
// CHECK-NEXT: | `-CoreturnStmt {{.*}} <line:{{.*}}:{{.*}}>{{$}}
62+
co_return;
63+
}
64+
// Verify the implicit AST nodes for coroutines.
65+
// CHECK: |-DeclStmt {{.*}}
66+
// CHECK-NEXT: | `-VarDecl {{.*}} implicit used __promise
67+
// ...
68+
// FIXME: the CoreturnStmt should be marked as implicit
69+
// CHECK: CoreturnStmt {{.*}} <col:6>{{$}}

0 commit comments

Comments
 (0)