Moved color and bold into a struct.
  Capitalized variable names.
  Turned off bold on source locations.

http://llvm-reviews.chandlerc.com/D291

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D291?vs=682&id=751#toc

Files:
  lib/AST/ASTDumper.cpp
  include/clang/AST/DeclBase.h
  include/clang/AST/Stmt.h
Index: lib/AST/ASTDumper.cpp
===================================================================
--- lib/AST/ASTDumper.cpp
+++ lib/AST/ASTDumper.cpp
@@ -28,8 +28,47 @@
 //===----------------------------------------------------------------------===//
 
 namespace  {
+  // Colors used for various parts of the AST dump
+
+  struct TerminalColor {
+    raw_ostream::Colors Color;
+    bool Bold;
+  };
+
+  // Decl kind names (VarDecl, FunctionDecl, etc)
+  static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
+  // Attr names (CleanupAttr, GuardedByAttr, etc)
+  static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
+  // Statement names (DeclStmt, ImplicitCastExpr, etc)
+  static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
+
+  // Type names (int, float, etc, plus user defined types)
+  static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
+
+  // Pointer address
+  static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
+  // Source locations
+  static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
+
+  // lvalue/xvalue
+  static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
+  // bitfield/objcproperty/objcsubscript/vectorcomponent
+  static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
+
+  // Null statements
+  static const TerminalColor NullColor = { raw_ostream::BLUE, false };
+
+  // CastKind from CastExpr's
+  static const TerminalColor CastColor = { raw_ostream::RED, false };
+
+  // Value of the statement
+  static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
+  // Decl names
+  static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
+
   class ASTDumper
       : public DeclVisitor<ASTDumper>, public StmtVisitor<ASTDumper> {
+
     SourceManager *SM;
     raw_ostream &OS;
     unsigned IndentLevel;
@@ -40,6 +79,8 @@
     const char *LastLocFilename;
     unsigned LastLocLine;
 
+    bool ShowColors;
+
     class IndentScope {
       ASTDumper &Dumper;
     public:
@@ -51,11 +92,30 @@
       }
     };
 
+    class ColorScope {
+      ASTDumper &Dumper;
+    public:
+      ColorScope(ASTDumper &Dumper, TerminalColor Color)
+        : Dumper(Dumper) {
+        if (Dumper.ShowColors)
+          Dumper.OS.changeColor(Color.Color, Color.Bold);
+      }
+      ~ColorScope() {
+        if (Dumper.ShowColors)
+          Dumper.OS.resetColor();
+      }
+    };
+
   public:
     ASTDumper(SourceManager *SM, raw_ostream &OS)
       : SM(SM), OS(OS), IndentLevel(0), IsFirstLine(true),
-        LastLocFilename(""), LastLocLine(~0U) { }
+        LastLocFilename(""), LastLocLine(~0U),
+        ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
 
+    ASTDumper(SourceManager *SM, raw_ostream &OS, bool ShowColors)
+      : SM(SM), OS(OS), IndentLevel(0), IsFirstLine(true),
+        LastLocFilename(""), LastLocLine(~0U), ShowColors(ShowColors) { }
+
     ~ASTDumper() {
       OS << "\n";
     }
@@ -211,10 +271,12 @@
 }
 
 void ASTDumper::dumpPointer(const void *Ptr) {
+  ColorScope Color(*this, AddressColor);
   OS << ' ' << Ptr;
 }
 
 void ASTDumper::dumpLocation(SourceLocation Loc) {
+  ColorScope Color(*this, LocationColor);
   SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
 
   // The general format we print out is filename:line:col, but we drop pieces
@@ -258,6 +320,8 @@
 }
 
 void ASTDumper::dumpBareType(QualType T) {
+  ColorScope Color(*this, TypeColor);
+  
   SplitQualType T_split = T.split();
   OS << "'" << QualType::getAsString(T_split) << "'";
 
@@ -275,10 +339,14 @@
 }
 
 void ASTDumper::dumpBareDeclRef(const Decl *D) {
-  OS << D->getDeclKindName();
+  {
+    ColorScope Color(*this, DeclKindNameColor);
+    OS << D->getDeclKindName();
+  }
   dumpPointer(D);
 
   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
+    ColorScope Color(*this, DeclNameColor);
     OS << " '";
     ND->getDeclName().printName(OS);
     OS << "'";
@@ -299,8 +367,10 @@
 }
 
 void ASTDumper::dumpName(const NamedDecl *ND) {
-  if (ND->getDeclName())
+  if (ND->getDeclName()) {
+    ColorScope Color(*this, DeclNameColor);
     OS << ' ' << ND->getNameAsString();
+  }
 }
 
 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
@@ -313,12 +383,15 @@
 
 void ASTDumper::dumpAttr(const Attr *A) {
   IndentScope Indent(*this);
-  switch (A->getKind()) {
+  {
+    ColorScope Color(*this, AttrColor);
+    switch (A->getKind()) {
 #define ATTR(X) case attr::X: OS << #X; break;
 #include "clang/Basic/AttrList.inc"
-  default: llvm_unreachable("unexpected attribute kind");
+    default: llvm_unreachable("unexpected attribute kind");
+    }
+    OS << "Attr";
   }
-  OS << "Attr";
   dumpPointer(A);
   dumpSourceRange(A->getRange());
 #include "clang/AST/AttrDump.inc"
@@ -433,11 +506,15 @@
   IndentScope Indent(*this);
 
   if (!D) {
+    ColorScope Color(*this, NullColor);
     OS << "<<<NULL>>>";
     return;
   }
 
-  OS << D->getDeclKindName() << "Decl";
+  {
+    ColorScope Color(*this, DeclKindNameColor);
+    OS << D->getDeclKindName() << "Decl";
+  }
   dumpPointer(D);
   dumpSourceRange(D->getSourceRange());
   DeclVisitor<ASTDumper>::Visit(D);
@@ -953,6 +1030,7 @@
   IndentScope Indent(*this);
 
   if (!S) {
+    ColorScope Color(*this, NullColor);
     OS << "<<<NULL>>>";
     return;
   }
@@ -968,7 +1046,10 @@
 }
 
 void ASTDumper::VisitStmt(Stmt *Node) {
-  OS << Node->getStmtClassName();
+  {   
+    ColorScope Color(*this, StmtColor);
+    OS << Node->getStmtClassName();
+  }
   dumpPointer(Node);
   dumpSourceRange(Node->getSourceRange());
 }
@@ -1006,32 +1087,38 @@
   VisitStmt(Node);
   dumpType(Node->getType());
 
-  switch (Node->getValueKind()) {
-  case VK_RValue:
-    break;
-  case VK_LValue:
-    OS << " lvalue";
-    break;
-  case VK_XValue:
-    OS << " xvalue";
-    break;
+  {
+    ColorScope Color(*this, ValueKindColor);
+    switch (Node->getValueKind()) {
+    case VK_RValue:
+      break;
+    case VK_LValue:
+      OS << " lvalue";
+      break;
+    case VK_XValue:
+      OS << " xvalue";
+      break;
+    }
   }
 
-  switch (Node->getObjectKind()) {
-  case OK_Ordinary:
-    break;
-  case OK_BitField:
-    OS << " bitfield";
-    break;
-  case OK_ObjCProperty:
-    OS << " objcproperty";
-    break;
-  case OK_ObjCSubscript:
-    OS << " objcsubscript";
-    break;
-  case OK_VectorComponent:
-    OS << " vectorcomponent";
-    break;
+  {
+    ColorScope Color(*this, ObjectKindColor);
+    switch (Node->getObjectKind()) {
+    case OK_Ordinary:
+      break;
+    case OK_BitField:
+      OS << " bitfield";
+      break;
+    case OK_ObjCProperty:
+      OS << " objcproperty";
+      break;
+    case OK_ObjCSubscript:
+      OS << " objcsubscript";
+      break;
+    case OK_VectorComponent:
+      OS << " vectorcomponent";
+      break;
+    }
   }
 }
 
@@ -1061,7 +1148,11 @@
 
 void ASTDumper::VisitCastExpr(CastExpr *Node) {
   VisitExpr(Node);
-  OS << " <" << Node->getCastKindName();
+  OS << " <";
+  {
+    ColorScope Color(*this, CastColor);
+    OS << Node->getCastKindName();
+  }
   dumpBasePath(OS, Node);
   OS << ">";
 }
@@ -1096,8 +1187,11 @@
 void ASTDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
   VisitExpr(Node);
 
-  OS << " " << Node->getDecl()->getDeclKindName()
-     << "Decl='" << *Node->getDecl() << "'";
+  {
+    ColorScope Color(*this, DeclKindNameColor);
+    OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
+  }
+  OS << "='" << *Node->getDecl() << "'";
   dumpPointer(Node->getDecl());
   if (Node->isFreeIvar())
     OS << " isFreeIvar";
@@ -1116,6 +1210,7 @@
 
 void ASTDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
   VisitExpr(Node);
+  ColorScope Color(*this, ValueColor);
   OS << " " << Node->getValue();
 }
 
@@ -1123,16 +1218,19 @@
   VisitExpr(Node);
 
   bool isSigned = Node->getType()->isSignedIntegerType();
+  ColorScope Color(*this, ValueColor);
   OS << " " << Node->getValue().toString(10, isSigned);
 }
 
 void ASTDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
   VisitExpr(Node);
+  ColorScope Color(*this, ValueColor);
   OS << " " << Node->getValueAsApproximateDouble();
 }
 
 void ASTDumper::VisitStringLiteral(StringLiteral *Str) {
   VisitExpr(Str);
+  ColorScope Color(*this, ValueColor);
   OS << " ";
   Str->outputString(OS);
 }
@@ -1388,6 +1486,11 @@
   P.dumpDecl(const_cast<Decl*>(this));
 }
 
+void Decl::dumpColor() const {
+  ASTDumper P(&getASTContext().getSourceManager(), llvm::errs(),
+              /*ShowColors*/true);
+  P.dumpDecl(const_cast<Decl*>(this));
+}
 //===----------------------------------------------------------------------===//
 // Stmt method implementations
 //===----------------------------------------------------------------------===//
@@ -1405,3 +1508,8 @@
   ASTDumper P(0, llvm::errs());
   P.dumpStmt(const_cast<Stmt*>(this));
 }
+
+void Stmt::dumpColor() const {
+  ASTDumper P(0, llvm::errs(), /*ShowColors*/true);
+  P.dumpStmt(const_cast<Stmt*>(this));
+}
Index: include/clang/AST/DeclBase.h
===================================================================
--- include/clang/AST/DeclBase.h
+++ include/clang/AST/DeclBase.h
@@ -851,6 +851,8 @@
                          unsigned Indentation = 0);
   // Debuggers don't usually respect default arguments.
   LLVM_ATTRIBUTE_USED void dump() const;
+  // Same as dump(), but forces color printing.
+  LLVM_ATTRIBUTE_USED void dumpColor() const;
   void dump(raw_ostream &Out) const;
   // Debuggers don't usually respect default arguments.
   LLVM_ATTRIBUTE_USED void dumpXML() const;
Index: include/clang/AST/Stmt.h
===================================================================
--- include/clang/AST/Stmt.h
+++ include/clang/AST/Stmt.h
@@ -364,6 +364,9 @@
   LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
   void dump(raw_ostream &OS, SourceManager &SM) const;
 
+  /// dumpColor - same as dump(), but forces color highlighting.
+  LLVM_ATTRIBUTE_USED void dumpColor() const;
+
   /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
   /// back to its original source language syntax.
   void dumpPretty(ASTContext &Context) const;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to