Skip to content

Commit

Permalink
[AST] Teach TextNodeDumper to print the "implicit" bit for coroutine …
Browse files Browse the repository at this point in the history
…AST nodes (#77311)
  • Loading branch information
hokein committed Jan 9, 2024
1 parent 243a582 commit 414ea3a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
69 changes: 69 additions & 0 deletions clang/test/AST/ast-dump-coroutine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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;
};

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>{{$}}

0 comments on commit 414ea3a

Please sign in to comment.