LLVM  7.0.0
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 
22 bool WithColor::colorsEnabled(raw_ostream &OS) {
23  if (UseColor == cl::BOU_UNSET)
24  return OS.has_colors();
25  return UseColor == cl::BOU_TRUE;
26 }
27 
29  // Detect color from terminal type unless the user passed the --color option.
30  if (colorsEnabled(OS)) {
31  switch (Color) {
34  break;
37  break;
40  break;
43  break;
46  break;
49  break;
51  OS.changeColor(raw_ostream::RED, true);
52  break;
55  break;
58  break;
59  }
60  }
61 }
62 
64 
66 
68 
70  if (!Prefix.empty())
71  OS << Prefix << ": ";
72  return WithColor(OS, HighlightColor::Error).get() << "error: ";
73 }
74 
76  if (!Prefix.empty())
77  OS << Prefix << ": ";
78  return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
79 }
80 
82  if (!Prefix.empty())
83  OS << Prefix << ": ";
84  return WithColor(OS, HighlightColor::Note).get() << "note: ";
85 }
86 
88  if (colorsEnabled(OS))
89  OS.resetColor();
90 }
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:63
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
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:65
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:67
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:410
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 cl::opt< cl::boolOrDefault > UseColor("color", cl::cat(ColorCategory), cl::desc("Use colors in output (default=autodetect)"), cl::init(cl::BOU_UNSET))
WithColor(raw_ostream &OS, HighlightColor S)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition: WithColor.cpp:28
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