[cfe-commits] [Patch] Visually update -ast-dump

Richard Trieu rtrieu at google.com
Wed Aug 29 19:11:02 PDT 2012


For too long, -ast-dump would produce drab, monochromatic, textual AST's.
 Large walls of text to slough through to find the interesting bits.
 Worse, if a statement filled more than one line, the structural
information of the tree would be easily lost amid the type names, source
locations, pointer address, and statement names.  Fear not, a solution
presents itself!

1) The addition of ASCII characters into the unused indents at the
beginning of lines.  Pipes "|", dashes "-", and backslashes "\" quickly
show relationships between the different statements.  Find siblings,
parents, and children fast and easy, never lose your place again.

2) For each statement, different parts are highlighted with different
colors.  This visually separates the components and allows faster
differentiating.  Looking for a specific type?  All types are the same
color.  Same for the statement names.  And source locations.

3) But wait, there's more!  Since -ast-dump and dump() use the same
ASTVisitor, you can also get colors in your debugger!  Just use dumpColor()
instead of dump() and get colors while you debug.

To use, either use "clang -Xclang -ast-dump" or "clang -cc1 -ast-dump
-fcolor-diagnostics" from a color-enabled terminal.

Implementation detail:
Color changes were handled by changeColor() and resetColor() from the
raw_ostream.  The different colors are stored in constants at the top of
the class before being used in the relevant visit method.

Tree structure printing was handled by a vector of statuses.  The status
indicates if there's future children at the level of the index.  This is
enough information to generate the tree structure.  This replaces the
integer indent counter.

To determine if color printing is required, the dumper queries the
DiagnosticEngine in the SourceManager.  This is enough for -ast-dump.
 However, when debugging and call Stmt.dump(), the Stmt doesn't have a
SourceManager.  It is possible to call dump() with a SourceManager, but to
simplify things, a dumpColor() was created to force color printing.

I've attached a patch and an image showing the difference between color and
no color.  Work in progress, comments welcome.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120829/3963dbea/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ast-dump.png
Type: image/png
Size: 356250 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120829/3963dbea/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ast-dump1.patch
Type: application/octet-stream
Size: 13718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120829/3963dbea/attachment.obj>


More information about the cfe-commits mailing list