r350665 - Implement the TreeStructure interface through the TextNodeDumper

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 8 14:32:48 PST 2019


Author: steveire
Date: Tue Jan  8 14:32:48 2019
New Revision: 350665

URL: http://llvm.org/viewvc/llvm-project?rev=350665&view=rev
Log:
Implement the TreeStructure interface through the TextNodeDumper

Summary:
This way, when the generic ASTTraverser is extracted from ASTDumper,
there can't be any problem related to ordering of class members, a
concern that was raised in https://reviews.llvm.org/D55337.

This will also preserve the property that the generic traverser does not
enforce any coupling between the NodeDumper and the TreeStructure.

 https://godbolt.org/z/PEtT1_

Reviewers: aaron.ballman, erichkeane

Subscribers: cfe-commits

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

Modified:
    cfe/trunk/include/clang/AST/ASTDumperUtils.h
    cfe/trunk/include/clang/AST/TextNodeDumper.h
    cfe/trunk/lib/AST/ASTDumper.cpp
    cfe/trunk/lib/AST/TextNodeDumper.cpp

Modified: cfe/trunk/include/clang/AST/ASTDumperUtils.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTDumperUtils.h?rev=350665&r1=350664&r2=350665&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTDumperUtils.h (original)
+++ cfe/trunk/include/clang/AST/ASTDumperUtils.h Tue Jan  8 14:32:48 2019
@@ -92,90 +92,6 @@ public:
   }
 };
 
-class TextTreeStructure {
-  raw_ostream &OS;
-  const bool ShowColors;
-
-  /// Pending[i] is an action to dump an entity at level i.
-  llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
-
-  /// Indicates whether we're at the top level.
-  bool TopLevel = true;
-
-  /// Indicates if we're handling the first child after entering a new depth.
-  bool FirstChild = true;
-
-  /// Prefix for currently-being-dumped entity.
-  std::string Prefix;
-
-public:
-  /// Add a child of the current node.  Calls doAddChild without arguments
-  template <typename Fn> void addChild(Fn doAddChild) {
-    // If we're at the top level, there's nothing interesting to do; just
-    // run the dumper.
-    if (TopLevel) {
-      TopLevel = false;
-      doAddChild();
-      while (!Pending.empty()) {
-        Pending.back()(true);
-        Pending.pop_back();
-      }
-      Prefix.clear();
-      OS << "\n";
-      TopLevel = true;
-      return;
-    }
-
-    auto dumpWithIndent = [this, doAddChild](bool isLastChild) {
-      // Print out the appropriate tree structure and work out the prefix for
-      // children of this node. For instance:
-      //
-      //   A        Prefix = ""
-      //   |-B      Prefix = "| "
-      //   | `-C    Prefix = "|   "
-      //   `-D      Prefix = "  "
-      //     |-E    Prefix = "  | "
-      //     `-F    Prefix = "    "
-      //   G        Prefix = ""
-      //
-      // Note that the first level gets no prefix.
-      {
-        OS << '\n';
-        ColorScope Color(OS, ShowColors, IndentColor);
-        OS << Prefix << (isLastChild ? '`' : '|') << '-';
-        this->Prefix.push_back(isLastChild ? ' ' : '|');
-        this->Prefix.push_back(' ');
-      }
-
-      FirstChild = true;
-      unsigned Depth = Pending.size();
-
-      doAddChild();
-
-      // If any children are left, they're the last at their nesting level.
-      // Dump those ones out now.
-      while (Depth < Pending.size()) {
-        Pending.back()(true);
-        this->Pending.pop_back();
-      }
-
-      // Restore the old prefix.
-      this->Prefix.resize(Prefix.size() - 2);
-    };
-
-    if (FirstChild) {
-      Pending.push_back(std::move(dumpWithIndent));
-    } else {
-      Pending.back()(false);
-      Pending.back() = std::move(dumpWithIndent);
-    }
-    FirstChild = false;
-  }
-
-  TextTreeStructure(raw_ostream &OS, bool ShowColors)
-      : OS(OS), ShowColors(ShowColors) {}
-};
-
 } // namespace clang
 
 #endif // LLVM_CLANG_AST_ASTDUMPERUTILS_H

Modified: cfe/trunk/include/clang/AST/TextNodeDumper.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/TextNodeDumper.h?rev=350665&r1=350664&r2=350665&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/TextNodeDumper.h (original)
+++ cfe/trunk/include/clang/AST/TextNodeDumper.h Tue Jan  8 14:32:48 2019
@@ -22,9 +22,94 @@
 
 namespace clang {
 
+class TextTreeStructure {
+  raw_ostream &OS;
+  const bool ShowColors;
+
+  /// Pending[i] is an action to dump an entity at level i.
+  llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
+
+  /// Indicates whether we're at the top level.
+  bool TopLevel = true;
+
+  /// Indicates if we're handling the first child after entering a new depth.
+  bool FirstChild = true;
+
+  /// Prefix for currently-being-dumped entity.
+  std::string Prefix;
+
+public:
+  /// Add a child of the current node.  Calls doAddChild without arguments
+  template <typename Fn> void addChild(Fn doAddChild) {
+    // If we're at the top level, there's nothing interesting to do; just
+    // run the dumper.
+    if (TopLevel) {
+      TopLevel = false;
+      doAddChild();
+      while (!Pending.empty()) {
+        Pending.back()(true);
+        Pending.pop_back();
+      }
+      Prefix.clear();
+      OS << "\n";
+      TopLevel = true;
+      return;
+    }
+
+    auto dumpWithIndent = [this, doAddChild](bool isLastChild) {
+      // Print out the appropriate tree structure and work out the prefix for
+      // children of this node. For instance:
+      //
+      //   A        Prefix = ""
+      //   |-B      Prefix = "| "
+      //   | `-C    Prefix = "|   "
+      //   `-D      Prefix = "  "
+      //     |-E    Prefix = "  | "
+      //     `-F    Prefix = "    "
+      //   G        Prefix = ""
+      //
+      // Note that the first level gets no prefix.
+      {
+        OS << '\n';
+        ColorScope Color(OS, ShowColors, IndentColor);
+        OS << Prefix << (isLastChild ? '`' : '|') << '-';
+        this->Prefix.push_back(isLastChild ? ' ' : '|');
+        this->Prefix.push_back(' ');
+      }
+
+      FirstChild = true;
+      unsigned Depth = Pending.size();
+
+      doAddChild();
+
+      // If any children are left, they're the last at their nesting level.
+      // Dump those ones out now.
+      while (Depth < Pending.size()) {
+        Pending.back()(true);
+        this->Pending.pop_back();
+      }
+
+      // Restore the old prefix.
+      this->Prefix.resize(Prefix.size() - 2);
+    };
+
+    if (FirstChild) {
+      Pending.push_back(std::move(dumpWithIndent));
+    } else {
+      Pending.back()(false);
+      Pending.back() = std::move(dumpWithIndent);
+    }
+    FirstChild = false;
+  }
+
+  TextTreeStructure(raw_ostream &OS, bool ShowColors)
+      : OS(OS), ShowColors(ShowColors) {}
+};
+
 class TextNodeDumper
     : public comments::ConstCommentVisitor<TextNodeDumper, void,
-                                           const comments::FullComment *> {
+                                           const comments::FullComment *>,
+      public TextTreeStructure {
   raw_ostream &OS;
   const bool ShowColors;
 

Modified: cfe/trunk/lib/AST/ASTDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTDumper.cpp?rev=350665&r1=350664&r2=350665&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTDumper.cpp (original)
+++ cfe/trunk/lib/AST/ASTDumper.cpp Tue Jan  8 14:32:48 2019
@@ -44,7 +44,6 @@ namespace  {
         public ConstCommentVisitor<ASTDumper, void, const FullComment *>,
         public TypeVisitor<ASTDumper> {
 
-    TextTreeStructure TreeStructure;
     TextNodeDumper NodeDumper;
 
     raw_ostream &OS;
@@ -60,7 +59,7 @@ namespace  {
 
     /// Dump a child of the current node.
     template<typename Fn> void dumpChild(Fn doDumpChild) {
-      TreeStructure.addChild(doDumpChild);
+      NodeDumper.addChild(doDumpChild);
     }
 
   public:
@@ -75,8 +74,7 @@ namespace  {
     ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
               const SourceManager *SM, bool ShowColors,
               const PrintingPolicy &PrintPolicy)
-        : TreeStructure(OS, ShowColors),
-          NodeDumper(OS, ShowColors, SM, PrintPolicy, Traits), OS(OS),
+        : NodeDumper(OS, ShowColors, SM, PrintPolicy, Traits), OS(OS),
           PrintPolicy(PrintPolicy), ShowColors(ShowColors) {}
 
     void setDeserialize(bool D) { Deserialize = D; }

Modified: cfe/trunk/lib/AST/TextNodeDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TextNodeDumper.cpp?rev=350665&r1=350664&r2=350665&view=diff
==============================================================================
--- cfe/trunk/lib/AST/TextNodeDumper.cpp (original)
+++ cfe/trunk/lib/AST/TextNodeDumper.cpp Tue Jan  8 14:32:48 2019
@@ -19,8 +19,8 @@ TextNodeDumper::TextNodeDumper(raw_ostre
                                const SourceManager *SM,
                                const PrintingPolicy &PrintPolicy,
                                const comments::CommandTraits *Traits)
-    : OS(OS), ShowColors(ShowColors), SM(SM), PrintPolicy(PrintPolicy),
-      Traits(Traits) {}
+    : TextTreeStructure(OS, ShowColors), OS(OS), ShowColors(ShowColors), SM(SM),
+      PrintPolicy(PrintPolicy), Traits(Traits) {}
 
 void TextNodeDumper::Visit(const comments::Comment *C,
                            const comments::FullComment *FC) {




More information about the cfe-commits mailing list