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
18namespace clang {
19
20/// Used to specify the format for printing AST dump information.
21enum 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
29struct 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)
45static const TerminalColor DeclKindNameColor = {.Color: llvm::raw_ostream::GREEN, .Bold: true};
46// Attr names (CleanupAttr, GuardedByAttr, etc)
47static const TerminalColor AttrColor = {.Color: llvm::raw_ostream::BLUE, .Bold: true};
48// Statement names (DeclStmt, ImplicitCastExpr, etc)
49static const TerminalColor StmtColor = {.Color: llvm::raw_ostream::MAGENTA, .Bold: true};
50// Comment names (FullComment, ParagraphComment, TextComment, etc)
51static const TerminalColor CommentColor = {.Color: llvm::raw_ostream::BLUE, .Bold: false};
52
53// Type names (int, float, etc, plus user defined types)
54static const TerminalColor TypeColor = {.Color: llvm::raw_ostream::GREEN, .Bold: false};
55
56// Pointer address
57static const TerminalColor AddressColor = {.Color: llvm::raw_ostream::YELLOW, .Bold: false};
58// Source locations
59static const TerminalColor LocationColor = {.Color: llvm::raw_ostream::YELLOW, .Bold: false};
60
61// lvalue/xvalue
62static const TerminalColor ValueKindColor = {.Color: llvm::raw_ostream::CYAN, .Bold: false};
63// bitfield/objcproperty/objcsubscript/vectorcomponent
64static const TerminalColor ObjectKindColor = {.Color: llvm::raw_ostream::CYAN, .Bold: false};
65// contains-errors
66static const TerminalColor ErrorsColor = {.Color: llvm::raw_ostream::RED, .Bold: true};
67
68// Null statements
69static const TerminalColor NullColor = {.Color: llvm::raw_ostream::BLUE, .Bold: false};
70
71// Undeserialized entities
72static const TerminalColor UndeserializedColor = {.Color: llvm::raw_ostream::GREEN,
73 .Bold: true};
74
75// CastKind from CastExpr's
76static const TerminalColor CastColor = {.Color: llvm::raw_ostream::RED, .Bold: false};
77
78// Value of the statement
79static const TerminalColor ValueColor = {.Color: llvm::raw_ostream::CYAN, .Bold: true};
80// Decl names
81static const TerminalColor DeclNameColor = {.Color: llvm::raw_ostream::CYAN, .Bold: true};
82
83// Indents ( `, -. | )
84static const TerminalColor IndentColor = {.Color: llvm::raw_ostream::BLUE, .Bold: false};
85
86class ColorScope {
87 llvm::raw_ostream &OS;
88 const bool ShowColors;
89
90public:
91 ColorScope(llvm::raw_ostream &OS, bool ShowColors, TerminalColor Color)
92 : OS(OS), ShowColors(ShowColors) {
93 if (ShowColors)
94 OS.changeColor(Color: Color.Color, Bold: Color.Bold);
95 }
96 ~ColorScope() {
97 if (ShowColors)
98 OS.resetColor();
99 }
100};
101
102} // namespace clang
103
104#endif // LLVM_CLANG_AST_ASTDUMPERUTILS_H
105

source code of clang/include/clang/AST/ASTDumperUtils.h