steveire created this revision.
steveire added a reviewer: aaron.ballman.
Herald added a subscriber: cfe-commits.

Repository:
  rC Clang

https://reviews.llvm.org/D55190

Files:
  include/clang/AST/ASTTextNodeDumper.h
  lib/AST/ASTDumper.cpp

Index: lib/AST/ASTDumper.cpp
===================================================================
--- lib/AST/ASTDumper.cpp
+++ lib/AST/ASTDumper.cpp
@@ -49,7 +49,6 @@
     TextNodeDumper NodeDumper;
 
     raw_ostream &OS;
-    const CommandTraits *Traits;
     const SourceManager *SM;
 
     /// The policy to use for printing; can be defaulted.
@@ -79,8 +78,8 @@
               const SourceManager *SM, bool ShowColors,
               const PrintingPolicy &PrintPolicy)
         : ChildDumper(OS, ShowColors),
-          NodeDumper(OS, ShowColors, SM, PrintPolicy), OS(OS), Traits(Traits),
-          SM(SM), PrintPolicy(PrintPolicy), ShowColors(ShowColors) {}
+          NodeDumper(OS, ShowColors, SM, PrintPolicy, Traits), OS(OS), SM(SM),
+          PrintPolicy(PrintPolicy), ShowColors(ShowColors) {}
 
     void setDeserialize(bool D) { Deserialize = D; }
 
@@ -436,29 +435,6 @@
     // Comments.
     const char *getCommandName(unsigned CommandID);
     void dumpComment(const Comment *C, const FullComment *FC);
-
-    // Inline comments.
-    void visitTextComment(const TextComment *C, const FullComment *FC);
-    void visitInlineCommandComment(const InlineCommandComment *C,
-                                   const FullComment *FC);
-    void visitHTMLStartTagComment(const HTMLStartTagComment *C,
-                                  const FullComment *FC);
-    void visitHTMLEndTagComment(const HTMLEndTagComment *C,
-                                const FullComment *FC);
-
-    // Block comments.
-    void visitBlockCommandComment(const BlockCommandComment *C,
-                                  const FullComment *FC);
-    void visitParamCommandComment(const ParamCommandComment *C,
-                                  const FullComment *FC);
-    void visitTParamCommandComment(const TParamCommandComment *C,
-                                   const FullComment *FC);
-    void visitVerbatimBlockComment(const VerbatimBlockComment *C,
-                                   const FullComment *FC);
-    void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C,
-                                       const FullComment *FC);
-    void visitVerbatimLineComment(const VerbatimLineComment *C,
-                                  const FullComment *FC);
   };
 }
 
@@ -2359,15 +2335,6 @@
 // Comments
 //===----------------------------------------------------------------------===//
 
-const char *ASTDumper::getCommandName(unsigned CommandID) {
-  if (Traits)
-    return Traits->getCommandInfo(CommandID)->Name;
-  const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
-  if (Info)
-    return Info->Name;
-  return "<not a builtin command>";
-}
-
 void ASTDumper::dumpFullComment(const FullComment *C) {
   if (!C)
     return;
@@ -2376,18 +2343,10 @@
 
 void ASTDumper::dumpComment(const Comment *C, const FullComment *FC) {
   dumpChild([=] {
+    NodeDumper.visit(C, FC);
     if (!C) {
-      ColorScope Color(OS, ShowColors, NullColor);
-      OS << "<<<NULL>>>";
       return;
     }
-
-    {
-      ColorScope Color(OS, ShowColors, CommentColor);
-      OS << C->getCommentKindName();
-    }
-    NodeDumper.dumpPointer(C);
-    NodeDumper.dumpSourceRange(C->getSourceRange());
     ConstCommentVisitor<ASTDumper, void, const FullComment *>::visit(C, FC);
     for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
          I != E; ++I)
@@ -2395,114 +2354,6 @@
   });
 }
 
-void ASTDumper::visitTextComment(const TextComment *C, const FullComment *) {
-  OS << " Text=\"" << C->getText() << "\"";
-}
-
-void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C,
-                                          const FullComment *) {
-  OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
-  switch (C->getRenderKind()) {
-  case InlineCommandComment::RenderNormal:
-    OS << " RenderNormal";
-    break;
-  case InlineCommandComment::RenderBold:
-    OS << " RenderBold";
-    break;
-  case InlineCommandComment::RenderMonospaced:
-    OS << " RenderMonospaced";
-    break;
-  case InlineCommandComment::RenderEmphasized:
-    OS << " RenderEmphasized";
-    break;
-  }
-
-  for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
-    OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
-}
-
-void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C,
-                                         const FullComment *) {
-  OS << " Name=\"" << C->getTagName() << "\"";
-  if (C->getNumAttrs() != 0) {
-    OS << " Attrs: ";
-    for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
-      const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
-      OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
-    }
-  }
-  if (C->isSelfClosing())
-    OS << " SelfClosing";
-}
-
-void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C,
-                                       const FullComment *) {
-  OS << " Name=\"" << C->getTagName() << "\"";
-}
-
-void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C,
-                                         const FullComment *) {
-  OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
-  for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
-    OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
-}
-
-void ASTDumper::visitParamCommandComment(const ParamCommandComment *C,
-                                         const FullComment *FC) {
-  OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
-
-  if (C->isDirectionExplicit())
-    OS << " explicitly";
-  else
-    OS << " implicitly";
-
-  if (C->hasParamName()) {
-    if (C->isParamIndexValid())
-      OS << " Param=\"" << C->getParamName(FC) << "\"";
-    else
-      OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
-  }
-
-  if (C->isParamIndexValid() && !C->isVarArgParam())
-    OS << " ParamIndex=" << C->getParamIndex();
-}
-
-void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C,
-                                          const FullComment *FC) {
-  if (C->hasParamName()) {
-    if (C->isPositionValid())
-      OS << " Param=\"" << C->getParamName(FC) << "\"";
-    else
-      OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
-  }
-
-  if (C->isPositionValid()) {
-    OS << " Position=<";
-    for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
-      OS << C->getIndex(i);
-      if (i != e - 1)
-        OS << ", ";
-    }
-    OS << ">";
-  }
-}
-
-void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C,
-                                          const FullComment *) {
-  OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
-        " CloseName=\"" << C->getCloseName() << "\"";
-}
-
-void ASTDumper::visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C,
-                                              const FullComment *) {
-  OS << " Text=\"" << C->getText() << "\"";
-}
-
-void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C,
-                                         const FullComment *) {
-  OS << " Text=\"" << C->getText() << "\"";
-}
-
 //===----------------------------------------------------------------------===//
 // Type method implementations
 //===----------------------------------------------------------------------===//
Index: include/clang/AST/ASTTextNodeDumper.h
===================================================================
--- include/clang/AST/ASTTextNodeDumper.h
+++ include/clang/AST/ASTTextNodeDumper.h
@@ -16,13 +16,17 @@
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTDumperUtils.h"
+#include "clang/AST/CommentCommandTraits.h"
+#include "clang/AST/CommentVisitor.h"
 #include "clang/AST/ExprCXX.h"
 
 namespace clang {
 
 namespace ast_dumper {
 
-class TextNodeDumper {
+class TextNodeDumper
+    : public comments::ConstCommentVisitor<TextNodeDumper, void,
+                                           const comments::FullComment *> {
   raw_ostream &OS;
   const bool ShowColors;
 
@@ -36,10 +40,42 @@
   /// The policy to use for printing; can be defaulted.
   PrintingPolicy PrintPolicy;
 
+  const comments::CommandTraits *Traits;
+
+  const char *getCommandName(unsigned CommandID) {
+    if (Traits)
+      return Traits->getCommandInfo(CommandID)->Name;
+    const comments::CommandInfo *Info =
+        comments::CommandTraits::getBuiltinCommandInfo(CommandID);
+    if (Info)
+      return Info->Name;
+    return "<not a builtin command>";
+  }
+
 public:
   TextNodeDumper(raw_ostream &OS, bool ShowColors, const SourceManager *SM,
-                 const PrintingPolicy &PrintPolicy)
-      : OS(OS), ShowColors(ShowColors), SM(SM), PrintPolicy(PrintPolicy) {}
+                 const PrintingPolicy &PrintPolicy,
+                 const comments::CommandTraits *Traits)
+      : OS(OS), ShowColors(ShowColors), SM(SM), PrintPolicy(PrintPolicy),
+        Traits(Traits) {}
+
+  void visit(const comments::Comment *C, const comments::FullComment *FC) {
+    if (!C) {
+      ColorScope Color(OS, ShowColors, NullColor);
+      OS << "<<<NULL>>>";
+      return;
+    }
+
+    {
+      ColorScope Color(OS, ShowColors, CommentColor);
+      OS << C->getCommentKindName();
+    }
+    dumpPointer(C);
+    dumpSourceRange(C->getSourceRange());
+
+    ConstCommentVisitor<TextNodeDumper, void,
+                        const comments::FullComment *>::visit(C, FC);
+  }
 
   void dumpPointer(const void *Ptr) {
     ColorScope Color(OS, ShowColors, AddressColor);
@@ -160,6 +196,120 @@
     dumpPointer(Temporary);
     OS << ")";
   }
+
+  void visitTextComment(const comments::TextComment *C,
+                        const comments::FullComment *) {
+    OS << " Text=\"" << C->getText() << "\"";
+  }
+
+  void visitInlineCommandComment(const comments::InlineCommandComment *C,
+                                 const comments::FullComment *) {
+    OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
+    switch (C->getRenderKind()) {
+    case comments::InlineCommandComment::RenderNormal:
+      OS << " RenderNormal";
+      break;
+    case comments::InlineCommandComment::RenderBold:
+      OS << " RenderBold";
+      break;
+    case comments::InlineCommandComment::RenderMonospaced:
+      OS << " RenderMonospaced";
+      break;
+    case comments::InlineCommandComment::RenderEmphasized:
+      OS << " RenderEmphasized";
+      break;
+    }
+
+    for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
+      OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
+  }
+
+  void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C,
+                                const comments::FullComment *) {
+    OS << " Name=\"" << C->getTagName() << "\"";
+    if (C->getNumAttrs() != 0) {
+      OS << " Attrs: ";
+      for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
+        const comments::HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
+        OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
+      }
+    }
+    if (C->isSelfClosing())
+      OS << " SelfClosing";
+  }
+
+  void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C,
+                              const comments::FullComment *) {
+    OS << " Name=\"" << C->getTagName() << "\"";
+  }
+
+  void visitBlockCommandComment(const comments::BlockCommandComment *C,
+                                const comments::FullComment *) {
+    OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
+    for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
+      OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
+  }
+
+  void visitParamCommandComment(const comments::ParamCommandComment *C,
+                                const comments::FullComment *FC) {
+    OS << " "
+       << comments::ParamCommandComment::getDirectionAsString(
+              C->getDirection());
+
+    if (C->isDirectionExplicit())
+      OS << " explicitly";
+    else
+      OS << " implicitly";
+
+    if (C->hasParamName()) {
+      if (C->isParamIndexValid())
+        OS << " Param=\"" << C->getParamName(FC) << "\"";
+      else
+        OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
+    }
+
+    if (C->isParamIndexValid() && !C->isVarArgParam())
+      OS << " ParamIndex=" << C->getParamIndex();
+  }
+
+  void visitTParamCommandComment(const comments::TParamCommandComment *C,
+                                 const comments::FullComment *FC) {
+    if (C->hasParamName()) {
+      if (C->isPositionValid())
+        OS << " Param=\"" << C->getParamName(FC) << "\"";
+      else
+        OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
+    }
+
+    if (C->isPositionValid()) {
+      OS << " Position=<";
+      for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
+        OS << C->getIndex(i);
+        if (i != e - 1)
+          OS << ", ";
+      }
+      OS << ">";
+    }
+  }
+
+  void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C,
+                                 const comments::FullComment *) {
+    OS << " Name=\"" << getCommandName(C->getCommandID())
+       << "\""
+          " CloseName=\""
+       << C->getCloseName() << "\"";
+  }
+
+  void
+  visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C,
+                                const comments::FullComment *) {
+    OS << " Text=\"" << C->getText() << "\"";
+  }
+
+  void visitVerbatimLineComment(const comments::VerbatimLineComment *C,
+                                const comments::FullComment *) {
+    OS << " Text=\"" << C->getText() << "\"";
+  }
 };
 
 } // namespace ast_dumper
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to