1 //===- WithColor.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_WITHCOLOR_H
10 #define LLVM_SUPPORT_WITHCOLOR_H
12 #include "llvm/Support/raw_ostream.h"
23 extern cl::OptionCategory ColorCategory;
25 // Symbolic names for various syntax elements.
26 enum class HighlightColor {
39 enum class ColorMode {
40 /// Determine whether to use color based on the command line argument and the
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
51 /// An RAII object that temporarily switches an output stream to a specific
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.
62 WithColor(raw_ostream &OS, HighlightColor S,
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.
71 WithColor(raw_ostream &OS,
72 raw_ostream::Colors Color = raw_ostream::SAVEDCOLOR,
73 bool Bold = false, bool BG = false,
74 ColorMode Mode = ColorMode::Auto)
75 : OS(OS), Mode(Mode) {
76 changeColor(Color, Bold, BG);
80 raw_ostream &get() { return OS; }
81 operator raw_ostream &() { return OS; }
82 template <typename T> WithColor &operator<<(T &O) {
86 template <typename T> WithColor &operator<<(const T &O) {
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 ¬e();
97 /// Convenience method for printing "remark: " to stderr.
98 static raw_ostream &remark();
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 ¬e(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);
113 /// Determine whether colors are displayed.
114 bool colorsEnabled();
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,
124 /// Reset the colors to terminal defaults. Call this when you are done
125 /// outputting colored text, or before program exit.
126 WithColor &resetColor();
128 /// Implement default handling for Error.
129 /// Print "error: " to stderr.
130 static void defaultErrorHandler(Error Err);
132 /// Implement default handling for Warning.
133 /// Print "warning: " to stderr.
134 static void defaultWarningHandler(Error Warning);
137 } // end namespace llvm
139 #endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H