LLVM 20.0.0git
WithColor.cpp
Go to the documentation of this file.
1//===- WithColor.cpp ------------------------------------------------------===//
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
10
11#include "DebugOptions.h"
12
14#include "llvm/Support/Error.h"
16
17using namespace llvm;
18
20 static cl::OptionCategory ColorCategory("Color Options");
21 return ColorCategory;
22}
23namespace {
24struct CreateUseColor {
25 static void *call() {
27 "color", cl::cat(getColorCategory()),
28 cl::desc("Use colors in output (default=autodetect)"),
30 }
31};
32} // namespace
35
37 return *UseColor == cl::BOU_UNSET ? OS.has_colors()
39}
40
41WithColor::AutoDetectFunctionType WithColor::AutoDetectFunction =
43
45 : OS(OS), Mode(Mode) {
46 // Detect color from terminal type unless the user passed the --color option.
47 if (colorsEnabled()) {
48 switch (Color) {
51 break;
54 break;
57 break;
60 break;
63 break;
66 break;
69 break;
72 break;
75 break;
78 break;
79 }
80 }
81}
82
84
86
88
90
92 bool DisableColors) {
93 if (!Prefix.empty())
94 OS << Prefix << ": ";
96 DisableColors ? ColorMode::Disable : ColorMode::Auto)
97 .get()
98 << "error: ";
99}
100
102 bool DisableColors) {
103 if (!Prefix.empty())
104 OS << Prefix << ": ";
106 DisableColors ? ColorMode::Disable : ColorMode::Auto)
107 .get()
108 << "warning: ";
109}
110
112 bool DisableColors) {
113 if (!Prefix.empty())
114 OS << Prefix << ": ";
116 DisableColors ? ColorMode::Disable : ColorMode::Auto)
117 .get()
118 << "note: ";
119}
120
122 bool DisableColors) {
123 if (!Prefix.empty())
124 OS << Prefix << ": ";
126 DisableColors ? ColorMode::Disable : ColorMode::Auto)
127 .get()
128 << "remark: ";
129}
130
132 switch (Mode) {
134 return true;
136 return false;
137 case ColorMode::Auto:
138 return AutoDetectFunction(OS);
139 }
140 llvm_unreachable("All cases handled above.");
141}
142
144 bool BG) {
145 if (colorsEnabled())
146 OS.changeColor(Color, Bold, BG);
147 return *this;
148}
149
151 if (colorsEnabled())
152 OS.resetColor();
153 return *this;
154}
155
157
159 handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
160 WithColor::error() << Info.message() << '\n';
161 });
162}
163
165 handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) {
166 WithColor::warning() << Info.message() << '\n';
167 });
168}
169
172}
173
175 AutoDetectFunctionType NewAutoDetectFunction) {
176 AutoDetectFunction = NewAutoDetectFunction;
177}
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
raw_pwrite_stream & OS
static bool DefaultAutoDetectFunction(const raw_ostream &OS)
Definition: WithColor.cpp:36
static ManagedStatic< cl::opt< cl::boolOrDefault >, CreateUseColor > UseColor
Definition: WithColor.cpp:33
Base class for error info classes.
Definition: Error.h:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:54
WithColor & changeColor(raw_ostream::Colors Color, bool Bold=false, bool BG=false)
Change the color of text that will be output from this point forward.
Definition: WithColor.cpp:143
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition: WithColor.cpp:164
LLVM_CTOR_NODISCARD WithColor(raw_ostream &OS, HighlightColor S, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition: WithColor.cpp:44
bool colorsEnabled()
Determine whether colors are displayed.
Definition: WithColor.cpp:131
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:85
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:83
bool(*)(const raw_ostream &OS) AutoDetectFunctionType
Definition: WithColor.h:56
WithColor & resetColor()
Reset the colors to terminal defaults.
Definition: WithColor.cpp:150
raw_ostream & get()
Definition: WithColor.h:79
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:158
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:87
static AutoDetectFunctionType defaultAutoDetectFunction()
Retrieve the default color auto detection function.
Definition: WithColor.cpp:170
static void setAutoDetectFunction(AutoDetectFunctionType NewAutoDetectFunction)
Change the global auto detection function.
Definition: WithColor.cpp:174
static raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition: WithColor.cpp:89
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
static constexpr Colors YELLOW
Definition: raw_ostream.h:117
static constexpr Colors CYAN
Definition: raw_ostream.h:120
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward.
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
static constexpr Colors BLUE
Definition: raw_ostream.h:118
static constexpr Colors MAGENTA
Definition: raw_ostream.h:119
static constexpr Colors BLACK
Definition: raw_ostream.h:114
static constexpr Colors GREEN
Definition: raw_ostream.h:116
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:351
static constexpr Colors RED
Definition: raw_ostream.h:115
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initWithColorOptions()
Definition: WithColor.cpp:34
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:977
cl::OptionCategory & getColorCategory()
Definition: WithColor.cpp:19
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
HighlightColor
Definition: WithColor.h:27
ColorMode
Definition: WithColor.h:40
@ Auto
Determine whether to use color based on the command line argument and the raw_ostream.
@ Enable
Enable colors.
@ Disable
Disable colors.