xref: /llvm-project/llvm/lib/Support/WithColor.cpp (revision 27126f5260760bec250de966f71cc8672092b357)
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/CommandLine.h"
12 #include "llvm/Support/raw_ostream.h"
13 
14 using namespace llvm;
15 
16 cl::OptionCategory llvm::ColorCategory("Color Options");
17 
18 static cl::opt<cl::boolOrDefault>
19     UseColor("color", cl::cat(ColorCategory),
20              cl::desc("Use colors in output (default=autodetect)"),
21              cl::init(cl::BOU_UNSET));
22 
23 bool WithColor::colorsEnabled(raw_ostream &OS) {
24   if (UseColor == cl::BOU_UNSET)
25     return OS.has_colors();
26   return UseColor == cl::BOU_TRUE;
27 }
28 
29 WithColor::WithColor(raw_ostream &OS, HighlightColor Color) : OS(OS) {
30   // Detect color from terminal type unless the user passed the --color option.
31   if (colorsEnabled(OS)) {
32     switch (Color) {
33     case HighlightColor::Address:
34       OS.changeColor(raw_ostream::YELLOW);
35       break;
36     case HighlightColor::String:
37       OS.changeColor(raw_ostream::GREEN);
38       break;
39     case HighlightColor::Tag:
40       OS.changeColor(raw_ostream::BLUE);
41       break;
42     case HighlightColor::Attribute:
43       OS.changeColor(raw_ostream::CYAN);
44       break;
45     case HighlightColor::Enumerator:
46       OS.changeColor(raw_ostream::MAGENTA);
47       break;
48     case HighlightColor::Macro:
49       OS.changeColor(raw_ostream::RED);
50       break;
51     case HighlightColor::Error:
52       OS.changeColor(raw_ostream::RED, true);
53       break;
54     case HighlightColor::Warning:
55       OS.changeColor(raw_ostream::MAGENTA, true);
56       break;
57     case HighlightColor::Note:
58       OS.changeColor(raw_ostream::BLACK, true);
59       break;
60     }
61   }
62 }
63 
64 raw_ostream &WithColor::error() { return error(errs()); }
65 
66 raw_ostream &WithColor::warning() { return warning(errs()); }
67 
68 raw_ostream &WithColor::note() { return note(errs()); }
69 
70 raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix) {
71   if (!Prefix.empty())
72     OS << Prefix << ": ";
73   return WithColor(OS, HighlightColor::Error).get() << "error: ";
74 }
75 
76 raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix) {
77   if (!Prefix.empty())
78     OS << Prefix << ": ";
79   return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
80 }
81 
82 raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix) {
83   if (!Prefix.empty())
84     OS << Prefix << ": ";
85   return WithColor(OS, HighlightColor::Note).get() << "note: ";
86 }
87 
88 WithColor::~WithColor() {
89   if (colorsEnabled(OS))
90     OS.resetColor();
91 }
92