1 //===- WithColor.cpp ------------------------------------------------------===//
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 #include "llvm/Support/WithColor.h"
11 #include "DebugOptions.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/ManagedStatic.h"
19 cl::OptionCategory
&llvm::getColorCategory() {
20 static cl::OptionCategory
ColorCategory("Color Options");
24 struct CreateUseColor
{
26 return new cl::opt
<cl::boolOrDefault
>(
27 "color", cl::cat(getColorCategory()),
28 cl::desc("Use colors in output (default=autodetect)"),
29 cl::init(cl::BOU_UNSET
));
33 static ManagedStatic
<cl::opt
<cl::boolOrDefault
>, CreateUseColor
> UseColor
;
34 void llvm::initWithColorOptions() { *UseColor
; }
36 WithColor::WithColor(raw_ostream
&OS
, HighlightColor Color
, ColorMode Mode
)
37 : OS(OS
), Mode(Mode
) {
38 // Detect color from terminal type unless the user passed the --color option.
39 if (colorsEnabled()) {
41 case HighlightColor::Address
:
42 OS
.changeColor(raw_ostream::YELLOW
);
44 case HighlightColor::String
:
45 OS
.changeColor(raw_ostream::GREEN
);
47 case HighlightColor::Tag
:
48 OS
.changeColor(raw_ostream::BLUE
);
50 case HighlightColor::Attribute
:
51 OS
.changeColor(raw_ostream::CYAN
);
53 case HighlightColor::Enumerator
:
54 OS
.changeColor(raw_ostream::MAGENTA
);
56 case HighlightColor::Macro
:
57 OS
.changeColor(raw_ostream::RED
);
59 case HighlightColor::Error
:
60 OS
.changeColor(raw_ostream::RED
, true);
62 case HighlightColor::Warning
:
63 OS
.changeColor(raw_ostream::MAGENTA
, true);
65 case HighlightColor::Note
:
66 OS
.changeColor(raw_ostream::BLACK
, true);
68 case HighlightColor::Remark
:
69 OS
.changeColor(raw_ostream::BLUE
, true);
75 raw_ostream
&WithColor::error() { return error(errs()); }
77 raw_ostream
&WithColor::warning() { return warning(errs()); }
79 raw_ostream
&WithColor::note() { return note(errs()); }
81 raw_ostream
&WithColor::remark() { return remark(errs()); }
83 raw_ostream
&WithColor::error(raw_ostream
&OS
, StringRef Prefix
,
87 return WithColor(OS
, HighlightColor::Error
,
88 DisableColors
? ColorMode::Disable
: ColorMode::Auto
)
93 raw_ostream
&WithColor::warning(raw_ostream
&OS
, StringRef Prefix
,
97 return WithColor(OS
, HighlightColor::Warning
,
98 DisableColors
? ColorMode::Disable
: ColorMode::Auto
)
103 raw_ostream
&WithColor::note(raw_ostream
&OS
, StringRef Prefix
,
104 bool DisableColors
) {
106 OS
<< Prefix
<< ": ";
107 return WithColor(OS
, HighlightColor::Note
,
108 DisableColors
? ColorMode::Disable
: ColorMode::Auto
)
113 raw_ostream
&WithColor::remark(raw_ostream
&OS
, StringRef Prefix
,
114 bool DisableColors
) {
116 OS
<< Prefix
<< ": ";
117 return WithColor(OS
, HighlightColor::Remark
,
118 DisableColors
? ColorMode::Disable
: ColorMode::Auto
)
123 bool WithColor::colorsEnabled() {
125 case ColorMode::Enable
:
127 case ColorMode::Disable
:
129 case ColorMode::Auto
:
130 return *UseColor
== cl::BOU_UNSET
? OS
.has_colors()
131 : *UseColor
== cl::BOU_TRUE
;
133 llvm_unreachable("All cases handled above.");
136 WithColor
&WithColor::changeColor(raw_ostream::Colors Color
, bool Bold
,
139 OS
.changeColor(Color
, Bold
, BG
);
143 WithColor
&WithColor::resetColor() {
149 WithColor::~WithColor() { resetColor(); }
151 void WithColor::defaultErrorHandler(Error Err
) {
152 handleAllErrors(std::move(Err
), [](ErrorInfoBase
&Info
) {
153 WithColor::error() << Info
.message() << '\n';
157 void WithColor::defaultWarningHandler(Error Warning
) {
158 handleAllErrors(std::move(Warning
), [](ErrorInfoBase
&Info
) {
159 WithColor::warning() << Info
.message() << '\n';