ASTDumperUtils.h 3.7 KB

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