LLVM  12.0.0git
WithColor.cpp
Go to the documentation of this file.
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 
11 
12 using namespace llvm;
13 
15 
17  UseColor("color", cl::cat(ColorCategory),
18  cl::desc("Use colors in output (default=autodetect)"),
20 
22  : OS(OS), Mode(Mode) {
23  // Detect color from terminal type unless the user passed the --color option.
24  if (colorsEnabled()) {
25  switch (Color) {
28  break;
31  break;
34  break;
37  break;
40  break;
43  break;
45  OS.changeColor(raw_ostream::RED, true);
46  break;
49  break;
52  break;
55  break;
56  }
57  }
58 }
59 
61 
63 
65 
67 
69  bool DisableColors) {
70  if (!Prefix.empty())
71  OS << Prefix << ": ";
73  DisableColors ? ColorMode::Disable : ColorMode::Auto)
74  .get()
75  << "error: ";
76 }
77 
79  bool DisableColors) {
80  if (!Prefix.empty())
81  OS << Prefix << ": ";
83  DisableColors ? ColorMode::Disable : ColorMode::Auto)
84  .get()
85  << "warning: ";
86 }
87 
89  bool DisableColors) {
90  if (!Prefix.empty())
91  OS << Prefix << ": ";
93  DisableColors ? ColorMode::Disable : ColorMode::Auto)
94  .get()
95  << "note: ";
96 }
97 
99  bool DisableColors) {
100  if (!Prefix.empty())
101  OS << Prefix << ": ";
103  DisableColors ? ColorMode::Disable : ColorMode::Auto)
104  .get()
105  << "remark: ";
106 }
107 
109  switch (Mode) {
110  case ColorMode::Enable:
111  return true;
112  case ColorMode::Disable:
113  return false;
114  case ColorMode::Auto:
115  return UseColor == cl::BOU_UNSET ? OS.has_colors()
116  : UseColor == cl::BOU_TRUE;
117  }
118  llvm_unreachable("All cases handled above.");
119 }
120 
122  bool BG) {
123  if (colorsEnabled())
124  OS.changeColor(Color, Bold, BG);
125  return *this;
126 }
127 
129  if (colorsEnabled())
130  OS.resetColor();
131  return *this;
132 }
133 
135 
137  handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
138  WithColor::error() << Info.message() << '\n';
139  });
140 }
141 
143  handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) {
144  WithColor::warning() << Info.message() << '\n';
145  });
146 }
SI Whole Quad Mode
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:53
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:60
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:56
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:62
WithColor(raw_ostream &OS, HighlightColor S, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition: WithColor.cpp:21
Base class for error info classes.
Definition: Error.h:48
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:136
WithColor & changeColor(raw_ostream::Colors Color, bool Bold=false, bool BG=false)
Change the color of text that will be output from this point forward.
Definition: WithColor.cpp:121
static constexpr Colors CYAN
Definition: raw_ostream.h:100
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:64
WithColor & resetColor()
Reset the colors to terminal defaults.
Definition: WithColor.cpp:128
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
HighlightColor
Definition: WithColor.h:26
Determine whether to use color based on the command line argument and the raw_ostream.
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
Disable colors.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:434
static constexpr Colors YELLOW
Definition: raw_ostream.h:97
bool colorsEnabled()
Determine whether colors are displayed.
Definition: WithColor.cpp:108
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static constexpr Colors MAGENTA
Definition: raw_ostream.h:99
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:295
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:929
Color
A "color", which is either even or odd.
static raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition: WithColor.cpp:66
static cl::opt< cl::boolOrDefault > UseColor("color", cl::cat(ColorCategory), cl::desc("Use colors in output (default=autodetect)"), cl::init(cl::BOU_UNSET))
static constexpr Colors BLUE
Definition: raw_ostream.h:98
static constexpr Colors GREEN
Definition: raw_ostream.h:96
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition: WithColor.cpp:142
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward. ...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
static constexpr Colors RED
Definition: raw_ostream.h:95
ColorMode
Definition: WithColor.h:39
cl::OptionCategory ColorCategory
static constexpr Colors BLACK
Definition: raw_ostream.h:94