LLVM  9.0.0svn
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), DisableColors(DisableColors) {
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 << ": ";
72  return WithColor(OS, HighlightColor::Error, DisableColors).get()
73  << "error: ";
74 }
75 
77  bool DisableColors) {
78  if (!Prefix.empty())
79  OS << Prefix << ": ";
80  return WithColor(OS, HighlightColor::Warning, DisableColors).get()
81  << "warning: ";
82 }
83 
85  bool DisableColors) {
86  if (!Prefix.empty())
87  OS << Prefix << ": ";
88  return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
89 }
90 
92  bool DisableColors) {
93  if (!Prefix.empty())
94  OS << Prefix << ": ";
95  return WithColor(OS, HighlightColor::Remark, DisableColors).get()
96  << "remark: ";
97 }
98 
100  if (DisableColors)
101  return false;
102  if (UseColor == cl::BOU_UNSET)
103  return OS.has_colors();
104  return UseColor == cl::BOU_TRUE;
105 }
106 
108  bool BG) {
109  if (colorsEnabled())
110  OS.changeColor(Color, Bold, BG);
111  return *this;
112 }
113 
115  if (colorsEnabled())
116  OS.resetColor();
117  return *this;
118 }
119 
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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:37
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:60
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. ...
Definition: raw_ostream.h:256
WithColor(raw_ostream &OS, HighlightColor S, bool DisableColors=false)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition: WithColor.cpp:21
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:62
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:107
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:114
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
HighlightColor
Definition: WithColor.h:22
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
bool colorsEnabled()
Determine whether colors are displayed.
Definition: WithColor.cpp:99
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:278
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))
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:267
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
cl::OptionCategory ColorCategory