LLVM 23.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
41
42WithColor::AutoDetectFunctionType WithColor::AutoDetectFunction =
44
46 : OS(OS), Mode(Mode) {
47 // Detect color from terminal type unless the user passed the --color option.
48 if (colorsEnabled()) {
49 switch (Color) {
51 OS.changeColor(raw_ostream::YELLOW);
52 break;
54 OS.changeColor(raw_ostream::GREEN);
55 break;
57 OS.changeColor(raw_ostream::BLUE);
58 break;
60 OS.changeColor(raw_ostream::CYAN);
61 break;
63 OS.changeColor(raw_ostream::MAGENTA);
64 break;
66 OS.changeColor(raw_ostream::RED);
67 break;
69 OS.changeColor(raw_ostream::RED, true);
70 break;
72 OS.changeColor(raw_ostream::MAGENTA, true);
73 break;
75 OS.changeColor(raw_ostream::BLACK, true);
76 break;
78 OS.changeColor(raw_ostream::BLUE, true);
79 break;
80 }
81 }
82}
83
85
87
89
91
93 bool DisableColors) {
94 if (!Prefix.empty())
95 OS << Prefix << ": ";
97 DisableColors ? ColorMode::Disable : ColorMode::Auto)
98 .get()
99 << "error: ";
100}
101
103 bool DisableColors) {
104 if (!Prefix.empty())
105 OS << Prefix << ": ";
107 DisableColors ? ColorMode::Disable : ColorMode::Auto)
108 .get()
109 << "warning: ";
110}
111
113 bool DisableColors) {
114 if (!Prefix.empty())
115 OS << Prefix << ": ";
117 DisableColors ? ColorMode::Disable : ColorMode::Auto)
118 .get()
119 << "note: ";
120}
121
123 bool DisableColors) {
124 if (!Prefix.empty())
125 OS << Prefix << ": ";
127 DisableColors ? ColorMode::Disable : ColorMode::Auto)
128 .get()
129 << "remark: ";
130}
131
133 switch (Mode) {
135 return true;
137 return false;
138 case ColorMode::Auto:
139 return AutoDetectFunction(OS);
140 }
141 llvm_unreachable("All cases handled above.");
142}
143
145 bool BG) {
146 if (colorsEnabled())
147 OS.changeColor(Color, Bold, BG);
148 return *this;
149}
150
152 if (colorsEnabled())
153 OS.resetColor();
154 return *this;
155}
156
158
160 handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
161 WithColor::error() << Info.message() << '\n';
162 });
163}
164
166 handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) {
167 WithColor::warning() << Info.message() << '\n';
168 });
169}
170
174
176 AutoDetectFunctionType NewAutoDetectFunction) {
177 AutoDetectFunction = NewAutoDetectFunction;
178}
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:44
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI 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.
static LLVM_ABI void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
LLVM_CTOR_NODISCARD LLVM_ABI WithColor(raw_ostream &OS, HighlightColor S, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition WithColor.cpp:45
LLVM_ABI bool colorsEnabled()
Determine whether colors are displayed.
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition WithColor.cpp:86
LLVM_ABI ~WithColor()
static LLVM_ABI raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition WithColor.cpp:84
bool(*)(const raw_ostream &OS) AutoDetectFunctionType
Definition WithColor.h:56
LLVM_ABI WithColor & resetColor()
Reset the colors to terminal defaults.
static LLVM_ABI void defaultErrorHandler(Error Err)
Implement default handling for Error.
static LLVM_ABI raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition WithColor.cpp:88
static LLVM_ABI AutoDetectFunctionType defaultAutoDetectFunction()
Retrieve the default color auto detection function.
static LLVM_ABI void setAutoDetectFunction(AutoDetectFunctionType NewAutoDetectFunction)
Change the global auto detection function.
static LLVM_ABI raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition WithColor.cpp:90
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
static constexpr Colors BLACK
static constexpr Colors GREEN
static constexpr Colors BLUE
static constexpr Colors RED
static constexpr Colors MAGENTA
static constexpr Colors YELLOW
static constexpr Colors CYAN
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
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:1013
LLVM_ABI cl::OptionCategory & getColorCategory()
Definition WithColor.cpp:19
LLVM_ABI 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.
Definition WithColor.h:43
@ Enable
Enable colors.
Definition WithColor.h:47
@ Disable
Disable colors.
Definition WithColor.h:49