Add color highlighting to various parts of the -ast-dump output.  This will 
make reading and searching the -ast-dump for information easier.

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

Files:
  lib/AST/ASTDumper.cpp
  include/clang/AST/Stmt.h
  include/clang/AST/DeclBase.h
Index: lib/AST/ASTDumper.cpp
===================================================================
--- lib/AST/ASTDumper.cpp
+++ lib/AST/ASTDumper.cpp
@@ -30,6 +30,51 @@
 namespace  {
   class ASTDumper
       : public DeclVisitor<ASTDumper>, public StmtVisitor<ASTDumper> {
+    // Colors used for various parts of the AST dump
+
+    // Decl kind names (VarDecl, FunctionDecl, etc)
+    static const raw_ostream::Colors declKindNameColor = raw_ostream::GREEN;
+    static const bool declKindNameBold = true;
+    // Attr names (CleanupAttr, GuardedByAttr, etc)
+    static const raw_ostream::Colors attrColor = raw_ostream::BLUE;
+    static const bool attrBold = true;
+    // Statement names (DeclStmt, ImplicitCastExpr, etc)
+    static const raw_ostream::Colors stmtColor = raw_ostream::MAGENTA;
+    static const bool stmtBold = true;
+
+    // Type names (int, float, etc, plus user defined types)
+    static const raw_ostream::Colors typeColor = raw_ostream::GREEN;
+    static const bool typeBold = false;
+
+    // Pointer address
+    static const raw_ostream::Colors addressColor = raw_ostream::YELLOW;
+    static const bool addressBold = false;
+    // Source locations
+    static const raw_ostream::Colors locationColor = raw_ostream::YELLOW;
+    static const bool locationBold = true;
+
+    // lvalue/xvalue
+    static const raw_ostream::Colors valueKindColor = raw_ostream::CYAN;
+    static const bool valueKindBold = false;
+    // bitfield/objcproperty/objcsubscript/vectorcomponent
+    static const raw_ostream::Colors objectKindColor = raw_ostream::CYAN;
+    static const bool objectKindBold = false;
+
+    // Null statements
+    static const raw_ostream::Colors nullColor = raw_ostream::BLUE;
+    static const bool nullBold = false;
+
+    // CastKind from CastExpr's
+    static const raw_ostream::Colors castColor = raw_ostream::RED;
+    static const bool castBold = false;
+
+    // Value of the statement
+    static const raw_ostream::Colors valueColor = raw_ostream::CYAN;
+    static const bool valueBold = true;
+    // Decl names
+    static const raw_ostream::Colors declNameColor = raw_ostream::CYAN;
+    static const bool declNameBold = true;
+
     SourceManager *SM;
     raw_ostream &OS;
     unsigned IndentLevel;
@@ -40,6 +85,8 @@
     const char *LastLocFilename;
     unsigned LastLocLine;
 
+    bool ShowColors;
+
     class IndentScope {
       ASTDumper &Dumper;
     public:
@@ -51,11 +98,30 @@
       }
     };
 
+    class ColorScope {
+      ASTDumper &Dumper;
+    public:
+      ColorScope(ASTDumper &Dumper, raw_ostream::Colors Color, bool Bold)
+        : Dumper(Dumper) {
+        if (Dumper.ShowColors)
+          Dumper.OS.changeColor(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 +277,12 @@
 }
 
 void ASTDumper::dumpPointer(const void *Ptr) {
+  ColorScope Color(*this, addressColor, addressBold);
   OS << ' ' << Ptr;
 }
 
 void ASTDumper::dumpLocation(SourceLocation Loc) {
+  ColorScope Color(*this, locationColor, locationBold);
   SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
 
   // The general format we print out is filename:line:col, but we drop pieces
@@ -258,6 +326,8 @@
 }
 
 void ASTDumper::dumpBareType(QualType T) {
+  ColorScope Color(*this, typeColor, typeBold);
+  
   SplitQualType T_split = T.split();
   OS << "'" << QualType::getAsString(T_split) << "'";
 
@@ -275,10 +345,14 @@
 }
 
 void ASTDumper::dumpBareDeclRef(const Decl *D) {
-  OS << D->getDeclKindName();
+  {
+    ColorScope Color(*this, declKindNameColor, declKindNameBold);
+    OS << D->getDeclKindName();
+  }
   dumpPointer(D);
 
   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
+    ColorScope Color(*this, declNameColor, declNameBold);
     OS << " '";
     ND->getDeclName().printName(OS);
     OS << "'";
@@ -299,8 +373,10 @@
 }
 
 void ASTDumper::dumpName(const NamedDecl *ND) {
-  if (ND->getDeclName())
+  if (ND->getDeclName()) {
+    ColorScope Color(*this, declNameColor, declNameBold);
     OS << ' ' << ND->getNameAsString();
+  }
 }
 
 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
@@ -313,12 +389,15 @@
 
 void ASTDumper::dumpAttr(const Attr *A) {
   IndentScope Indent(*this);
-  switch (A->getKind()) {
+  {
+    ColorScope Color(*this, attrColor, attrBold);
+    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 +512,15 @@
   IndentScope Indent(*this);
 
   if (!D) {
+    ColorScope Color(*this, nullColor, nullBold);
     OS << "<<<NULL>>>";
     return;
   }
 
-  OS << D->getDeclKindName() << "Decl";
+  {
+    ColorScope Color(*this, declKindNameColor, declKindNameBold);
+    OS << D->getDeclKindName() << "Decl";
+  }
   dumpPointer(D);
   dumpSourceRange(D->getSourceRange());
   DeclVisitor<ASTDumper>::Visit(D);
@@ -953,6 +1036,7 @@
   IndentScope Indent(*this);
 
   if (!S) {
+    ColorScope Color(*this, nullColor, nullBold);
     OS << "<<<NULL>>>";
     return;
   }
@@ -968,7 +1052,10 @@
 }
 
 void ASTDumper::VisitStmt(Stmt *Node) {
-  OS << Node->getStmtClassName();
+  {   
+    ColorScope Color(*this, stmtColor, stmtBold);
+    OS << Node->getStmtClassName();
+  }
   dumpPointer(Node);
   dumpSourceRange(Node->getSourceRange());
 }
@@ -1006,32 +1093,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, valueKindBold);
+    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, objectKindBold);
+    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 +1154,11 @@
 
 void ASTDumper::VisitCastExpr(CastExpr *Node) {
   VisitExpr(Node);
-  OS << " <" << Node->getCastKindName();
+  OS << " <";
+  {
+    ColorScope Color(*this, castColor, castBold);
+    OS << Node->getCastKindName();
+  }
   dumpBasePath(OS, Node);
   OS << ">";
 }
@@ -1096,8 +1193,11 @@
 void ASTDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
   VisitExpr(Node);
 
-  OS << " " << Node->getDecl()->getDeclKindName()
-     << "Decl='" << *Node->getDecl() << "'";
+  {
+    ColorScope Color(*this, declKindNameColor, declKindNameBold);
+    OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
+  }
+  OS << "='" << *Node->getDecl() << "'";
   dumpPointer(Node->getDecl());
   if (Node->isFreeIvar())
     OS << " isFreeIvar";
@@ -1116,6 +1216,7 @@
 
 void ASTDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
   VisitExpr(Node);
+  ColorScope Color(*this, valueColor, valueBold);
   OS << " " << Node->getValue();
 }
 
@@ -1123,16 +1224,19 @@
   VisitExpr(Node);
 
   bool isSigned = Node->getType()->isSignedIntegerType();
+  ColorScope Color(*this, valueColor, valueBold);
   OS << " " << Node->getValue().toString(10, isSigned);
 }
 
 void ASTDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
   VisitExpr(Node);
+  ColorScope Color(*this, valueColor, valueBold);
   OS << " " << Node->getValueAsApproximateDouble();
 }
 
 void ASTDumper::VisitStringLiteral(StringLiteral *Str) {
   VisitExpr(Str);
+  ColorScope Color(*this, valueColor, valueBold);
   OS << " ";
   Str->outputString(OS);
 }
@@ -1388,6 +1492,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 +1514,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/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;
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;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to