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"
10 #include "llvm/Support/raw_ostream.h"
14 cl::OptionCategory
llvm::ColorCategory("Color Options");
16 static cl::opt
<cl::boolOrDefault
>
17 UseColor("color", cl::cat(ColorCategory
),
18 cl::desc("Use colors in output (default=autodetect)"),
19 cl::init(cl::BOU_UNSET
));
21 WithColor::WithColor(raw_ostream
&OS
, HighlightColor Color
, bool DisableColors
)
22 : OS(OS
), DisableColors(DisableColors
) {
23 // Detect color from terminal type unless the user passed the --color option.
24 if (colorsEnabled()) {
26 case HighlightColor::Address
:
27 OS
.changeColor(raw_ostream::YELLOW
);
29 case HighlightColor::String
:
30 OS
.changeColor(raw_ostream::GREEN
);
32 case HighlightColor::Tag
:
33 OS
.changeColor(raw_ostream::BLUE
);
35 case HighlightColor::Attribute
:
36 OS
.changeColor(raw_ostream::CYAN
);
38 case HighlightColor::Enumerator
:
39 OS
.changeColor(raw_ostream::MAGENTA
);
41 case HighlightColor::Macro
:
42 OS
.changeColor(raw_ostream::RED
);
44 case HighlightColor::Error
:
45 OS
.changeColor(raw_ostream::RED
, true);
47 case HighlightColor::Warning
:
48 OS
.changeColor(raw_ostream::MAGENTA
, true);
50 case HighlightColor::Note
:
51 OS
.changeColor(raw_ostream::BLACK
, true);
53 case HighlightColor::Remark
:
54 OS
.changeColor(raw_ostream::BLUE
, true);
60 raw_ostream
&WithColor::error() { return error(errs()); }
62 raw_ostream
&WithColor::warning() { return warning(errs()); }
64 raw_ostream
&WithColor::note() { return note(errs()); }
66 raw_ostream
&WithColor::remark() { return remark(errs()); }
68 raw_ostream
&WithColor::error(raw_ostream
&OS
, StringRef Prefix
,
72 return WithColor(OS
, HighlightColor::Error
, DisableColors
).get()
76 raw_ostream
&WithColor::warning(raw_ostream
&OS
, StringRef Prefix
,
80 return WithColor(OS
, HighlightColor::Warning
, DisableColors
).get()
84 raw_ostream
&WithColor::note(raw_ostream
&OS
, StringRef Prefix
,
88 return WithColor(OS
, HighlightColor::Note
, DisableColors
).get() << "note: ";
91 raw_ostream
&WithColor::remark(raw_ostream
&OS
, StringRef Prefix
,
95 return WithColor(OS
, HighlightColor::Remark
, DisableColors
).get()
99 bool WithColor::colorsEnabled() {
102 if (UseColor
== cl::BOU_UNSET
)
103 return OS
.has_colors();
104 return UseColor
== cl::BOU_TRUE
;
107 WithColor
&WithColor::changeColor(raw_ostream::Colors Color
, bool Bold
,
110 OS
.changeColor(Color
, Bold
, BG
);
114 WithColor
&WithColor::resetColor() {
120 WithColor::~WithColor() { resetColor(); }