LLVM  8.0.1
WithColor.cpp
Go to the documentation of this file.
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"
12 
13 using namespace llvm;
14 
16 
18  UseColor("color", cl::cat(ColorCategory),
19  cl::desc("Use colors in output (default=autodetect)"),
21 
23  : OS(OS), DisableColors(DisableColors) {
24  // Detect color from terminal type unless the user passed the --color option.
25  if (colorsEnabled()) {
26  switch (Color) {
29  break;
32  break;
35  break;
38  break;
41  break;
44  break;
46  OS.changeColor(raw_ostream::RED, true);
47  break;
50  break;
53  break;
56  break;
57  }
58  }
59 }
60 
62 
64 
66 
68 
70  bool DisableColors) {
71  if (!Prefix.empty())
72  OS << Prefix << ": ";
73  return WithColor(OS, HighlightColor::Error, DisableColors).get()
74  << "error: ";
75 }
76 
78  bool DisableColors) {
79  if (!Prefix.empty())
80  OS << Prefix << ": ";
81  return WithColor(OS, HighlightColor::Warning, DisableColors).get()
82  << "warning: ";
83 }
84 
86  bool DisableColors) {
87  if (!Prefix.empty())
88  OS << Prefix << ": ";
89  return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
90 }
91 
93  bool DisableColors) {
94  if (!Prefix.empty())
95  OS << Prefix << ": ";
96  return WithColor(OS, HighlightColor::Remark, DisableColors).get()
97  << "remark: ";
98 }
99 
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 
109  bool BG) {
110  if (colorsEnabled())
111  OS.changeColor(Color, Bold, BG);
112  return *this;
113 }
114 
116  if (colorsEnabled())
117  OS.resetColor();
118  return *this;
119 }
120 
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:38
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:61
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:257
WithColor(raw_ostream &OS, HighlightColor S, bool DisableColors=false)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition: WithColor.cpp:22
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:63
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:108
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:65
WithColor & resetColor()
Reset the colors to terminal defaults.
Definition: WithColor.cpp:115
HighlightColor
Definition: WithColor.h:23
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
bool colorsEnabled()
Determine whether colors are displayed.
Definition: WithColor.cpp:100
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:279
Color
A "color", which is either even or odd.
static raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition: WithColor.cpp:67
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:268
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:49
cl::OptionCategory ColorCategory