LLVM  12.0.0git
WithColor.h
Go to the documentation of this file.
1 //===- WithColor.h ----------------------------------------------*- C++ -*-===//
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 #ifndef LLVM_SUPPORT_WITHCOLOR_H
10 #define LLVM_SUPPORT_WITHCOLOR_H
11 
13 
14 namespace llvm {
15 
16 class Error;
17 class StringRef;
18 
19 namespace cl {
20 class OptionCategory;
21 }
22 
23 extern cl::OptionCategory ColorCategory;
24 
25 // Symbolic names for various syntax elements.
26 enum class HighlightColor {
27  Address,
28  String,
29  Tag,
30  Attribute,
31  Enumerator,
32  Macro,
33  Error,
34  Warning,
35  Note,
36  Remark
37 };
38 
39 enum class ColorMode {
40  /// Determine whether to use color based on the command line argument and the
41  /// raw_ostream.
42  Auto,
43  /// Enable colors. Because raw_ostream is the one implementing colors, this
44  /// has no effect if the stream does not support colors or has colors
45  /// disabled.
46  Enable,
47  /// Disable colors.
48  Disable,
49 };
50 
51 /// An RAII object that temporarily switches an output stream to a specific
52 /// color.
53 class WithColor {
54  raw_ostream &OS;
56 
57 public:
58  /// To be used like this: WithColor(OS, HighlightColor::String) << "text";
59  /// @param OS The output stream
60  /// @param S Symbolic name for syntax element to color
61  /// @param Mode Enable, disable or compute whether to use colors.
63  ColorMode Mode = ColorMode::Auto);
64  /// To be used like this: WithColor(OS, raw_ostream::Black) << "text";
65  /// @param OS The output stream
66  /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
67  /// change only the bold attribute, and keep colors untouched
68  /// @param Bold Bold/brighter text, default false
69  /// @param BG If true, change the background, default: change foreground
70  /// @param Mode Enable, disable or compute whether to use colors.
73  bool Bold = false, bool BG = false,
75  : OS(OS), Mode(Mode) {
76  changeColor(Color, Bold, BG);
77  }
78  ~WithColor();
79 
80  raw_ostream &get() { return OS; }
81  operator raw_ostream &() { return OS; }
82  template <typename T> WithColor &operator<<(T &O) {
83  OS << O;
84  return *this;
85  }
86  template <typename T> WithColor &operator<<(const T &O) {
87  OS << O;
88  return *this;
89  }
90 
91  /// Convenience method for printing "error: " to stderr.
92  static raw_ostream &error();
93  /// Convenience method for printing "warning: " to stderr.
94  static raw_ostream &warning();
95  /// Convenience method for printing "note: " to stderr.
96  static raw_ostream &note();
97  /// Convenience method for printing "remark: " to stderr.
98  static raw_ostream &remark();
99 
100  /// Convenience method for printing "error: " to the given stream.
101  static raw_ostream &error(raw_ostream &OS, StringRef Prefix = "",
102  bool DisableColors = false);
103  /// Convenience method for printing "warning: " to the given stream.
104  static raw_ostream &warning(raw_ostream &OS, StringRef Prefix = "",
105  bool DisableColors = false);
106  /// Convenience method for printing "note: " to the given stream.
107  static raw_ostream &note(raw_ostream &OS, StringRef Prefix = "",
108  bool DisableColors = false);
109  /// Convenience method for printing "remark: " to the given stream.
110  static raw_ostream &remark(raw_ostream &OS, StringRef Prefix = "",
111  bool DisableColors = false);
112 
113  /// Determine whether colors are displayed.
114  bool colorsEnabled();
115 
116  /// Change the color of text that will be output from this point forward.
117  /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
118  /// change only the bold attribute, and keep colors untouched
119  /// @param Bold Bold/brighter text, default false
120  /// @param BG If true, change the background, default: change foreground
121  WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false,
122  bool BG = false);
123 
124  /// Reset the colors to terminal defaults. Call this when you are done
125  /// outputting colored text, or before program exit.
126  WithColor &resetColor();
127 
128  /// Implement default handling for Error.
129  /// Print "error: " to stderr.
130  static void defaultErrorHandler(Error Err);
131 
132  /// Implement default handling for Warning.
133  /// Print "warning: " to stderr.
134  static void defaultWarningHandler(Error Warning);
135 };
136 
137 } // end namespace llvm
138 
139 #endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H
SI Whole Quad Mode
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:53
#define error(X)
transform warning
HighlightColor
Definition: WithColor.h:26
Determine whether to use color based on the command line argument and the raw_ostream.
static Error defaultWarningHandler(const Twine &Msg)
Definition: ELF.h:78
WithColor & operator<<(T &O)
Definition: WithColor.h:82
WithColor(raw_ostream &OS, raw_ostream::Colors Color=raw_ostream::SAVEDCOLOR, bool Bold=false, bool BG=false, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, raw_ostream::Black) << "text";.
Definition: WithColor.h:71
WithColor & operator<<(const T &O)
Definition: WithColor.h:86
Color
A "color", which is either even or odd.
static constexpr Colors SAVEDCOLOR
Definition: raw_ostream.h:102
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
ColorMode
Definition: WithColor.h:39
cl::OptionCategory ColorCategory