/home/arjun/llvm-project/llvm/lib/Support/WithColor.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- WithColor.cpp ------------------------------------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "llvm/Support/WithColor.h" |
10 | | #include "llvm/Support/raw_ostream.h" |
11 | | |
12 | | using namespace llvm; |
13 | | |
14 | | cl::OptionCategory llvm::ColorCategory("Color Options"); |
15 | | |
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)); |
20 | | |
21 | | WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors) |
22 | 0 | : OS(OS), DisableColors(DisableColors) { |
23 | 0 | // Detect color from terminal type unless the user passed the --color option. |
24 | 0 | if (colorsEnabled()) { |
25 | 0 | switch (Color) { |
26 | 0 | case HighlightColor::Address: |
27 | 0 | OS.changeColor(raw_ostream::YELLOW); |
28 | 0 | break; |
29 | 0 | case HighlightColor::String: |
30 | 0 | OS.changeColor(raw_ostream::GREEN); |
31 | 0 | break; |
32 | 0 | case HighlightColor::Tag: |
33 | 0 | OS.changeColor(raw_ostream::BLUE); |
34 | 0 | break; |
35 | 0 | case HighlightColor::Attribute: |
36 | 0 | OS.changeColor(raw_ostream::CYAN); |
37 | 0 | break; |
38 | 0 | case HighlightColor::Enumerator: |
39 | 0 | OS.changeColor(raw_ostream::MAGENTA); |
40 | 0 | break; |
41 | 0 | case HighlightColor::Macro: |
42 | 0 | OS.changeColor(raw_ostream::RED); |
43 | 0 | break; |
44 | 0 | case HighlightColor::Error: |
45 | 0 | OS.changeColor(raw_ostream::RED, true); |
46 | 0 | break; |
47 | 0 | case HighlightColor::Warning: |
48 | 0 | OS.changeColor(raw_ostream::MAGENTA, true); |
49 | 0 | break; |
50 | 0 | case HighlightColor::Note: |
51 | 0 | OS.changeColor(raw_ostream::BLACK, true); |
52 | 0 | break; |
53 | 0 | case HighlightColor::Remark: |
54 | 0 | OS.changeColor(raw_ostream::BLUE, true); |
55 | 0 | break; |
56 | 0 | } |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | 0 | raw_ostream &WithColor::error() { return error(errs()); } |
61 | | |
62 | 0 | raw_ostream &WithColor::warning() { return warning(errs()); } |
63 | | |
64 | 0 | raw_ostream &WithColor::note() { return note(errs()); } |
65 | | |
66 | 0 | raw_ostream &WithColor::remark() { return remark(errs()); } |
67 | | |
68 | | raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix, |
69 | 0 | bool DisableColors) { |
70 | 0 | if (!Prefix.empty()) |
71 | 0 | OS << Prefix << ": "; |
72 | 0 | return WithColor(OS, HighlightColor::Error, DisableColors).get() |
73 | 0 | << "error: "; |
74 | 0 | } |
75 | | |
76 | | raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix, |
77 | 0 | bool DisableColors) { |
78 | 0 | if (!Prefix.empty()) |
79 | 0 | OS << Prefix << ": "; |
80 | 0 | return WithColor(OS, HighlightColor::Warning, DisableColors).get() |
81 | 0 | << "warning: "; |
82 | 0 | } |
83 | | |
84 | | raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix, |
85 | 0 | bool DisableColors) { |
86 | 0 | if (!Prefix.empty()) |
87 | 0 | OS << Prefix << ": "; |
88 | 0 | return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: "; |
89 | 0 | } |
90 | | |
91 | | raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix, |
92 | 0 | bool DisableColors) { |
93 | 0 | if (!Prefix.empty()) |
94 | 0 | OS << Prefix << ": "; |
95 | 0 | return WithColor(OS, HighlightColor::Remark, DisableColors).get() |
96 | 0 | << "remark: "; |
97 | 0 | } |
98 | | |
99 | 0 | bool WithColor::colorsEnabled() { |
100 | 0 | if (DisableColors) |
101 | 0 | return false; |
102 | 0 | if (UseColor == cl::BOU_UNSET) |
103 | 0 | return OS.has_colors(); |
104 | 0 | return UseColor == cl::BOU_TRUE; |
105 | 0 | } |
106 | | |
107 | | WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold, |
108 | 0 | bool BG) { |
109 | 0 | if (colorsEnabled()) |
110 | 0 | OS.changeColor(Color, Bold, BG); |
111 | 0 | return *this; |
112 | 0 | } |
113 | | |
114 | 0 | WithColor &WithColor::resetColor() { |
115 | 0 | if (colorsEnabled()) |
116 | 0 | OS.resetColor(); |
117 | 0 | return *this; |
118 | 0 | } |
119 | | |
120 | 0 | WithColor::~WithColor() { resetColor(); } |
121 | | |
122 | 0 | void WithColor::defaultErrorHandler(Error Err) { |
123 | 0 | handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) { |
124 | 0 | WithColor::error() << Info.message() << '\n'; |
125 | 0 | }); |
126 | 0 | } |
127 | | |
128 | 0 | void WithColor::defaultWarningHandler(Error Warning) { |
129 | 0 | handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) { |
130 | 0 | WithColor::warning() << Info.message() << '\n'; |
131 | 0 | }); |
132 | 0 | } |