LLVM  14.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 
10 
11 #include "DebugOptions.h"
12 
14 #include "llvm/Support/Error.h"
16 
17 using namespace llvm;
18 
20  static cl::OptionCategory ColorCategory("Color Options");
21  return ColorCategory;
22 }
23 namespace {
24 struct CreateUseColor {
25  static void *call() {
26  return new cl::opt<cl::boolOrDefault>(
27  "color", cl::cat(getColorCategory()),
28  cl::desc("Use colors in output (default=autodetect)"),
30  }
31 };
32 } // namespace
35 
37  : OS(OS), Mode(Mode) {
38  // Detect color from terminal type unless the user passed the --color option.
39  if (colorsEnabled()) {
40  switch (Color) {
43  break;
46  break;
49  break;
52  break;
55  break;
58  break;
60  OS.changeColor(raw_ostream::RED, true);
61  break;
64  break;
67  break;
70  break;
71  }
72  }
73 }
74 
76 
78 
80 
82 
84  bool DisableColors) {
85  if (!Prefix.empty())
86  OS << Prefix << ": ";
88  DisableColors ? ColorMode::Disable : ColorMode::Auto)
89  .get()
90  << "error: ";
91 }
92 
94  bool DisableColors) {
95  if (!Prefix.empty())
96  OS << Prefix << ": ";
98  DisableColors ? ColorMode::Disable : ColorMode::Auto)
99  .get()
100  << "warning: ";
101 }
102 
104  bool DisableColors) {
105  if (!Prefix.empty())
106  OS << Prefix << ": ";
107  return WithColor(OS, HighlightColor::Note,
108  DisableColors ? ColorMode::Disable : ColorMode::Auto)
109  .get()
110  << "note: ";
111 }
112 
114  bool DisableColors) {
115  if (!Prefix.empty())
116  OS << Prefix << ": ";
118  DisableColors ? ColorMode::Disable : ColorMode::Auto)
119  .get()
120  << "remark: ";
121 }
122 
124  switch (Mode) {
125  case ColorMode::Enable:
126  return true;
127  case ColorMode::Disable:
128  return false;
129  case ColorMode::Auto:
130  return *UseColor == cl::BOU_UNSET ? OS.has_colors()
131  : *UseColor == cl::BOU_TRUE;
132  }
133  llvm_unreachable("All cases handled above.");
134 }
135 
137  bool BG) {
138  if (colorsEnabled())
139  OS.changeColor(Color, Bold, BG);
140  return *this;
141 }
142 
144  if (colorsEnabled())
145  OS.resetColor();
146  return *this;
147 }
148 
150 
153  WithColor::error() << Info.message() << '\n';
154  });
155 }
156 
159  WithColor::warning() << Info.message() << '\n';
160  });
161 }
UseColor
static ManagedStatic< cl::opt< cl::boolOrDefault >, CreateUseColor > UseColor
Definition: WithColor.cpp:33
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::getColorCategory
cl::OptionCategory & getColorCategory()
Definition: WithColor.cpp:19
llvm::HighlightColor
HighlightColor
Definition: WithColor.h:26
ManagedStatic.h
llvm::WithColor::WithColor
WithColor(raw_ostream &OS, HighlightColor S, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition: WithColor.cpp:36
llvm::WithColor::colorsEnabled
bool colorsEnabled()
Determine whether colors are displayed.
Definition: WithColor.cpp:123
Error.h
llvm::raw_ostream::GREEN
static constexpr Colors GREEN
Definition: raw_ostream.h:112
DebugOptions.h
llvm::WithColor::defaultErrorHandler
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:151
llvm::WithColor
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:53
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
llvm::raw_ostream::RED
static constexpr Colors RED
Definition: raw_ostream.h:111
llvm::WithColor::defaultWarningHandler
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition: WithColor.cpp:157
llvm::HighlightColor::Warning
@ Warning
llvm::HighlightColor::Note
@ Note
llvm::WithColor::remark
static raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition: WithColor.cpp:81
llvm::WithColor::error
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:75
llvm::raw_ostream::MAGENTA
static constexpr Colors MAGENTA
Definition: raw_ostream.h:115
CommandLine.h
llvm::WithColor::~WithColor
~WithColor()
Definition: WithColor.cpp:149
llvm::raw_ostream::changeColor
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.cpp:522
llvm::HighlightColor::Tag
@ Tag
llvm::HighlightColor::Enumerator
@ Enumerator
llvm::initWithColorOptions
void initWithColorOptions()
Definition: WithColor.cpp:34
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
llvm::ColorMode::Auto
@ Auto
Determine whether to use color based on the command line argument and the raw_ostream.
llvm::ColorMode::Disable
@ Disable
Disable colors.
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::ErrorInfoBase
Base class for error info classes.
Definition: Error.h:48
llvm::HighlightColor::Remark
@ Remark
llvm::cl::opt
Definition: CommandLine.h:1432
llvm::WithColor::warning
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:77
llvm::cl::BOU_UNSET
@ BOU_UNSET
Definition: CommandLine.h:623
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
call
S is passed via registers r2 But gcc stores them to the and then reload them to and r3 before issuing the call(r0 contains the address of the format string)
Definition: README.txt:190
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
llvm::HighlightColor::Macro
@ Macro
llvm::HighlightColor::Address
@ Address
llvm::WithColor::changeColor
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:136
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
llvm::HighlightColor::Error
@ Error
llvm::WithColor::resetColor
WithColor & resetColor()
Reset the colors to terminal defaults.
Definition: WithColor.cpp:143
llvm::raw_ostream::CYAN
static constexpr Colors CYAN
Definition: raw_ostream.h:116
llvm::HighlightColor::String
@ String
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::ColorMode
ColorMode
Definition: WithColor.h:39
llvm::raw_ostream::has_colors
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:327
llvm::HighlightColor::Attribute
@ Attribute
WithColor.h
llvm::ColorMode::Enable
@ Enable
Enable colors.
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::cl::BOU_TRUE
@ BOU_TRUE
Definition: CommandLine.h:623
llvm::raw_ostream::BLACK
static constexpr Colors BLACK
Definition: raw_ostream.h:110
llvm::cl::cat
Definition: CommandLine.h:463
llvm::raw_ostream::Colors
Colors
Definition: raw_ostream.h:97
llvm::raw_ostream::resetColor
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.cpp:535
llvm::raw_ostream::BLUE
static constexpr Colors BLUE
Definition: raw_ostream.h:114
llvm::WithColor::note
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:79
llvm::raw_ostream::YELLOW
static constexpr Colors YELLOW
Definition: raw_ostream.h:113
llvm::cl::desc
Definition: CommandLine.h:412
llvm::handleAllErrors
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:968
llvm::cl::OptionCategory
Definition: CommandLine.h:186