Skip to content

Commit

Permalink
[ASTDump] Add utility for dumping a label with child nodes
Browse files Browse the repository at this point in the history
Summary:
Use it to add optional label nodes to Stmt dumps.  This preserves
behavior of InitExprList dump:

// CHECK-NEXT: `-InitListExpr {{.+}} <col:13, col:15> 'U [3]'
// CHECK-NEXT:   |-array_filler: InitListExpr {{.+}} <col:15> 'U' field Field {{.+}} 'i' 'int'
// CHECK-NEXT:   `-InitListExpr {{.+}} <col:14> 'U' field Field {{.+}} 'i' 'int'
// CHECK-NEXT:     `-IntegerLiteral {{.+}} <col:14> 'int' 1

Reviewers: aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 350957
  • Loading branch information
steveire committed Jan 11, 2019
1 parent 27ba559 commit 0df805d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
14 changes: 13 additions & 1 deletion clang/include/clang/AST/TextNodeDumper.h
Expand Up @@ -41,6 +41,12 @@ class TextTreeStructure {
public:
/// Add a child of the current node. Calls DoAddChild without arguments
template <typename Fn> void AddChild(Fn DoAddChild) {
return AddChild("", DoAddChild);
}

/// Add a child of the current node with an optional label.
/// Calls DoAddChild without arguments.
template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild) {
// If we're at the top level, there's nothing interesting to do; just
// run the dumper.
if (TopLevel) {
Expand All @@ -56,7 +62,10 @@ class TextTreeStructure {
return;
}

auto DumpWithIndent = [this, DoAddChild](bool IsLastChild) {
// We need to capture an owning-string in the lambda because the lambda
// is invoked in a deferred manner.
std::string LabelStr = Label;
auto DumpWithIndent = [this, DoAddChild, LabelStr](bool IsLastChild) {
// Print out the appropriate tree structure and work out the prefix for
// children of this node. For instance:
//
Expand All @@ -73,6 +82,9 @@ class TextTreeStructure {
OS << '\n';
ColorScope Color(OS, ShowColors, IndentColor);
OS << Prefix << (IsLastChild ? '`' : '|') << '-';
if (!LabelStr.empty())
OS << LabelStr << ": ";

this->Prefix.push_back(IsLastChild ? ' ' : '|');
this->Prefix.push_back(' ');
}
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/AST/ASTDumper.cpp
Expand Up @@ -61,6 +61,9 @@ namespace {
template<typename Fn> void dumpChild(Fn DoDumpChild) {
NodeDumper.AddChild(DoDumpChild);
}
template <typename Fn> void dumpChild(StringRef Label, Fn DoDumpChild) {
NodeDumper.AddChild(Label, DoDumpChild);
}

public:
ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
Expand All @@ -80,7 +83,7 @@ namespace {
void setDeserialize(bool D) { Deserialize = D; }

void dumpDecl(const Decl *D);
void dumpStmt(const Stmt *S);
void dumpStmt(const Stmt *S, StringRef Label = {});

// Utilities
void dumpType(QualType T) { NodeDumper.dumpType(T); }
Expand Down Expand Up @@ -1685,8 +1688,8 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
// Stmt dumping methods.
//===----------------------------------------------------------------------===//

void ASTDumper::dumpStmt(const Stmt *S) {
dumpChild([=] {
void ASTDumper::dumpStmt(const Stmt *S, StringRef Label) {
dumpChild(Label, [=] {
if (!S) {
ColorScope Color(OS, ShowColors, NullColor);
OS << "<<<NULL>>>";
Expand Down Expand Up @@ -1957,10 +1960,7 @@ void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
NodeDumper.dumpBareDeclRef(Field);
}
if (auto *Filler = ILE->getArrayFiller()) {
dumpChild([=] {
OS << "array filler";
dumpStmt(Filler);
});
dumpStmt(Filler, "array_filler");
}
}

Expand Down
3 changes: 1 addition & 2 deletions clang/test/AST/ast-dump-stmt.cpp
Expand Up @@ -91,8 +91,7 @@ void TestUnionInitList()
U us[3] = {1};
// CHECK: VarDecl {{.+}} <col:3, col:15> col:5 us 'U [3]' cinit
// CHECK-NEXT: `-InitListExpr {{.+}} <col:13, col:15> 'U [3]'
// CHECK-NEXT: |-array filler
// CHECK-NEXT: | `-InitListExpr {{.+}} <col:15> 'U' field Field {{.+}} 'i' 'int'
// CHECK-NEXT: |-array_filler: InitListExpr {{.+}} <col:15> 'U' field Field {{.+}} 'i' 'int'
// CHECK-NEXT: `-InitListExpr {{.+}} <col:14> 'U' field Field {{.+}} 'i' 'int'
// CHECK-NEXT: `-IntegerLiteral {{.+}} <col:14> 'int' 1
}
Expand Down

0 comments on commit 0df805d

Please sign in to comment.