1 //===--- ASTDumperUtils.h - Printing of AST nodes -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements AST utilities for traversal down the tree.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_ASTDUMPERUTILS_H
14 #define LLVM_CLANG_AST_ASTDUMPERUTILS_H
15 
16 #include "llvm/Support/raw_ostream.h"
17 
18 namespace clang {
19 
20 /// Used to specify the format for printing AST dump information.
21 enum ASTDumpOutputFormat {
22   ADOF_Default,
23   ADOF_JSON
24 };
25 
26 // Colors used for various parts of the AST dump
27 // Do not use bold yellow for any text.  It is hard to read on white screens.
28 
29 struct TerminalColor {
30   llvm::raw_ostream::Colors Color;
31   bool Bold;
32 };
33 
34 // Red           - CastColor
35 // Green         - TypeColor
36 // Bold Green    - DeclKindNameColor, UndeserializedColor
37 // Yellow        - AddressColor, LocationColor
38 // Blue          - CommentColor, NullColor, IndentColor
39 // Bold Blue     - AttrColor
40 // Bold Magenta  - StmtColor
41 // Cyan          - ValueKindColor, ObjectKindColor
42 // Bold Cyan     - ValueColor, DeclNameColor
43 
44 // Decl kind names (VarDecl, FunctionDecl, etc)
45 static const TerminalColor DeclKindNameColor = {llvm::raw_ostream::GREEN, true};
46 // Attr names (CleanupAttr, GuardedByAttr, etc)
47 static const TerminalColor AttrColor = {llvm::raw_ostream::BLUE, true};
48 // Statement names (DeclStmt, ImplicitCastExpr, etc)
49 static const TerminalColor StmtColor = {llvm::raw_ostream::MAGENTA, true};
50 // Comment names (FullComment, ParagraphComment, TextComment, etc)
51 static const TerminalColor CommentColor = {llvm::raw_ostream::BLUE, false};
52 
53 // Type names (int, float, etc, plus user defined types)
54 static const TerminalColor TypeColor = {llvm::raw_ostream::GREEN, false};
55 
56 // Pointer address
57 static const TerminalColor AddressColor = {llvm::raw_ostream::YELLOW, false};
58 // Source locations
59 static const TerminalColor LocationColor = {llvm::raw_ostream::YELLOW, false};
60 
61 // lvalue/xvalue
62 static const TerminalColor ValueKindColor = {llvm::raw_ostream::CYAN, false};
63 // bitfield/objcproperty/objcsubscript/vectorcomponent
64 static const TerminalColor ObjectKindColor = {llvm::raw_ostream::CYAN, false};
65 
66 // Null statements
67 static const TerminalColor NullColor = {llvm::raw_ostream::BLUE, false};
68 
69 // Undeserialized entities
70 static const TerminalColor UndeserializedColor = {llvm::raw_ostream::GREEN,
71                                                   true};
72 
73 // CastKind from CastExpr's
74 static const TerminalColor CastColor = {llvm::raw_ostream::RED, false};
75 
76 // Value of the statement
77 static const TerminalColor ValueColor = {llvm::raw_ostream::CYAN, true};
78 // Decl names
79 static const TerminalColor DeclNameColor = {llvm::raw_ostream::CYAN, true};
80 
81 // Indents ( `, -. | )
82 static const TerminalColor IndentColor = {llvm::raw_ostream::BLUE, false};
83 
84 class ColorScope {
85   llvm::raw_ostream &OS;
86   const bool ShowColors;
87 
88 public:
ColorScope(llvm::raw_ostream & OS,bool ShowColors,TerminalColor Color)89   ColorScope(llvm::raw_ostream &OS, bool ShowColors, TerminalColor Color)
90       : OS(OS), ShowColors(ShowColors) {
91     if (ShowColors)
92       OS.changeColor(Color.Color, Color.Bold);
93   }
~ColorScope()94   ~ColorScope() {
95     if (ShowColors)
96       OS.resetColor();
97   }
98 };
99 
100 } // namespace clang
101 
102 #endif // LLVM_CLANG_AST_ASTDUMPERUTILS_H
103