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/CommandLine.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, ColorMode Mode)
22 : OS(OS), Mode(Mode) {
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,
73 DisableColors ? ColorMode::Disable : ColorMode::Auto)
78 raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
82 return WithColor(OS, HighlightColor::Warning,
83 DisableColors ? ColorMode::Disable : ColorMode::Auto)
88 raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
92 return WithColor(OS, HighlightColor::Note,
93 DisableColors ? ColorMode::Disable : ColorMode::Auto)
98 raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
101 OS << Prefix << ": ";
102 return WithColor(OS, HighlightColor::Remark,
103 DisableColors ? ColorMode::Disable : ColorMode::Auto)
108 bool WithColor::colorsEnabled() {
110 case ColorMode::Enable:
112 case ColorMode::Disable:
114 case ColorMode::Auto:
115 return UseColor == cl::BOU_UNSET ? OS.has_colors()
116 : UseColor == cl::BOU_TRUE;
118 llvm_unreachable("All cases handled above.");
121 WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
124 OS.changeColor(Color, Bold, BG);
128 WithColor &WithColor::resetColor() {
134 WithColor::~WithColor() { resetColor(); }
136 void WithColor::defaultErrorHandler(Error Err) {
137 handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
138 WithColor::error() << Info.message() << '\n';
142 void WithColor::defaultWarningHandler(Error Warning) {
143 handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) {
144 WithColor::warning() << Info.message() << '\n';