1 //===- WithColor.cpp ------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/WithColor.h"
11 #include "llvm/Support/raw_ostream.h"
12
13 using namespace llvm;
14
15 cl::OptionCategory llvm::ColorCategory("Color Options");
16
17 static cl::opt<cl::boolOrDefault>
18 UseColor("color", cl::cat(ColorCategory),
19 cl::desc("Use colors in output (default=autodetect)"),
20 cl::init(cl::BOU_UNSET));
21
WithColor(raw_ostream & OS,HighlightColor Color,bool DisableColors)22 WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
23 : OS(OS), DisableColors(DisableColors) {
24 // Detect color from terminal type unless the user passed the --color option.
25 if (colorsEnabled()) {
26 switch (Color) {
27 case HighlightColor::Address:
28 OS.changeColor(raw_ostream::YELLOW);
29 break;
30 case HighlightColor::String:
31 OS.changeColor(raw_ostream::GREEN);
32 break;
33 case HighlightColor::Tag:
34 OS.changeColor(raw_ostream::BLUE);
35 break;
36 case HighlightColor::Attribute:
37 OS.changeColor(raw_ostream::CYAN);
38 break;
39 case HighlightColor::Enumerator:
40 OS.changeColor(raw_ostream::MAGENTA);
41 break;
42 case HighlightColor::Macro:
43 OS.changeColor(raw_ostream::RED);
44 break;
45 case HighlightColor::Error:
46 OS.changeColor(raw_ostream::RED, true);
47 break;
48 case HighlightColor::Warning:
49 OS.changeColor(raw_ostream::MAGENTA, true);
50 break;
51 case HighlightColor::Note:
52 OS.changeColor(raw_ostream::BLACK, true);
53 break;
54 case HighlightColor::Remark:
55 OS.changeColor(raw_ostream::BLUE, true);
56 break;
57 }
58 }
59 }
60
error()61 raw_ostream &WithColor::error() { return error(errs()); }
62
warning()63 raw_ostream &WithColor::warning() { return warning(errs()); }
64
note()65 raw_ostream &WithColor::note() { return note(errs()); }
66
remark()67 raw_ostream &WithColor::remark() { return remark(errs()); }
68
error(raw_ostream & OS,StringRef Prefix,bool DisableColors)69 raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix,
70 bool DisableColors) {
71 if (!Prefix.empty())
72 OS << Prefix << ": ";
73 return WithColor(OS, HighlightColor::Error, DisableColors).get()
74 << "error: ";
75 }
76
warning(raw_ostream & OS,StringRef Prefix,bool DisableColors)77 raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
78 bool DisableColors) {
79 if (!Prefix.empty())
80 OS << Prefix << ": ";
81 return WithColor(OS, HighlightColor::Warning, DisableColors).get()
82 << "warning: ";
83 }
84
note(raw_ostream & OS,StringRef Prefix,bool DisableColors)85 raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
86 bool DisableColors) {
87 if (!Prefix.empty())
88 OS << Prefix << ": ";
89 return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
90 }
91
remark(raw_ostream & OS,StringRef Prefix,bool DisableColors)92 raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
93 bool DisableColors) {
94 if (!Prefix.empty())
95 OS << Prefix << ": ";
96 return WithColor(OS, HighlightColor::Remark, DisableColors).get()
97 << "remark: ";
98 }
99
colorsEnabled()100 bool WithColor::colorsEnabled() {
101 if (DisableColors)
102 return false;
103 if (UseColor == cl::BOU_UNSET)
104 return OS.has_colors();
105 return UseColor == cl::BOU_TRUE;
106 }
107
changeColor(raw_ostream::Colors Color,bool Bold,bool BG)108 WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
109 bool BG) {
110 if (colorsEnabled())
111 OS.changeColor(Color, Bold, BG);
112 return *this;
113 }
114
resetColor()115 WithColor &WithColor::resetColor() {
116 if (colorsEnabled())
117 OS.resetColor();
118 return *this;
119 }
120
~WithColor()121 WithColor::~WithColor() { resetColor(); }
122