Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
9#include "llvm/Support/WithColor.h"
10
11#include "DebugOptions.h"
12
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/ManagedStatic.h"
16
17using namespacellvm;
18
19cl::OptionCategory &llvm::getColorCategory() {
20staticcl::OptionCategory ColorCategory("Color Options");
21return ColorCategory;
22}
23namespace{
24structCreateUseColor {
25staticvoid *call() {
26returnnewcl::opt<cl::boolOrDefault>(
27"color",cl::cat(getColorCategory()),
28cl::desc("Use colors in output (default=autodetect)"),
29cl::init(cl::BOU_UNSET));
30 }
31};
32}// namespace
33staticManagedStatic<cl::opt<cl::boolOrDefault>, CreateUseColor>UseColor;
34voidllvm::initWithColorOptions() { *UseColor; }
35
36staticboolDefaultAutoDetectFunction(constraw_ostream &OS) {
37return *UseColor ==cl::BOU_UNSET ?OS.has_colors()
38 : *UseColor ==cl::BOU_TRUE;
39}
40
41WithColor::AutoDetectFunctionType WithColor::AutoDetectFunction =
42DefaultAutoDetectFunction;
43
44WithColor::WithColor(raw_ostream &OS,HighlightColor Color,ColorMode Mode)
45 :OS(OS), Mode(Mode) {
46// Detect color from terminal type unless the user passed the --color option.
47if (colorsEnabled()) {
48switch (Color) {
49caseHighlightColor::Address:
50OS.changeColor(raw_ostream::YELLOW);
51break;
52caseHighlightColor::String:
53OS.changeColor(raw_ostream::GREEN);
54break;
55caseHighlightColor::Tag:
56OS.changeColor(raw_ostream::BLUE);
57break;
58caseHighlightColor::Attribute:
59OS.changeColor(raw_ostream::CYAN);
60break;
61caseHighlightColor::Enumerator:
62OS.changeColor(raw_ostream::MAGENTA);
63break;
64caseHighlightColor::Macro:
65OS.changeColor(raw_ostream::RED);
66break;
67caseHighlightColor::Error:
68OS.changeColor(raw_ostream::RED,true);
69break;
70caseHighlightColor::Warning:
71OS.changeColor(raw_ostream::MAGENTA,true);
72break;
73caseHighlightColor::Note:
74OS.changeColor(raw_ostream::BLACK,true);
75break;
76caseHighlightColor::Remark:
77OS.changeColor(raw_ostream::BLUE,true);
78break;
79 }
80 }
81}
82
83raw_ostream &WithColor::error() {returnerror(errs()); }
84
85raw_ostream &WithColor::warning() {returnwarning(errs()); }
86
87raw_ostream &WithColor::note() {returnnote(errs()); }
88
89raw_ostream &WithColor::remark() {returnremark(errs()); }
90
91raw_ostream &WithColor::error(raw_ostream &OS,StringRef Prefix,
92bool DisableColors) {
93if (!Prefix.empty())
94OS << Prefix <<": ";
95returnWithColor(OS,HighlightColor::Error,
96 DisableColors ?ColorMode::Disable :ColorMode::Auto)
97 .get()
98 <<"error: ";
99}
100
101raw_ostream &WithColor::warning(raw_ostream &OS,StringRef Prefix,
102bool DisableColors) {
103if (!Prefix.empty())
104OS << Prefix <<": ";
105returnWithColor(OS,HighlightColor::Warning,
106 DisableColors ?ColorMode::Disable :ColorMode::Auto)
107 .get()
108 <<"warning: ";
109}
110
111raw_ostream &WithColor::note(raw_ostream &OS,StringRef Prefix,
112bool DisableColors) {
113if (!Prefix.empty())
114OS << Prefix <<": ";
115returnWithColor(OS,HighlightColor::Note,
116 DisableColors ?ColorMode::Disable :ColorMode::Auto)
117 .get()
118 <<"note: ";
119}
120
121raw_ostream &WithColor::remark(raw_ostream &OS,StringRef Prefix,
122bool DisableColors) {
123if (!Prefix.empty())
124OS << Prefix <<": ";
125returnWithColor(OS,HighlightColor::Remark,
126 DisableColors ?ColorMode::Disable :ColorMode::Auto)
127 .get()
128 <<"remark: ";
129}
130
131boolWithColor::colorsEnabled() {
132switch (Mode) {
133caseColorMode::Enable:
134returntrue;
135caseColorMode::Disable:
136returnfalse;
137caseColorMode::Auto:
138return AutoDetectFunction(OS);
139 }
140llvm_unreachable("All cases handled above.");
141}
142
143WithColor &WithColor::changeColor(raw_ostream::Colors Color,bool Bold,
144bool BG) {
145if (colorsEnabled())
146 OS.changeColor(Color, Bold, BG);
147return *this;
148}
149
150WithColor &WithColor::resetColor() {
151if (colorsEnabled())
152 OS.resetColor();
153return *this;
154}
155
156WithColor::~WithColor() {resetColor(); }
157
158voidWithColor::defaultErrorHandler(Error Err) {
159handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
160WithColor::error() <<Info.message() <<'\n';
161 });
162}
163
164voidWithColor::defaultWarningHandler(ErrorWarning) {
165handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) {
166WithColor::warning() <<Info.message() <<'\n';
167 });
168}
169
170WithColor::AutoDetectFunctionTypeWithColor::defaultAutoDetectFunction() {
171returnDefaultAutoDetectFunction;
172}
173
174voidWithColor::setAutoDetectFunction(
175AutoDetectFunctionType NewAutoDetectFunction) {
176 AutoDetectFunction = NewAutoDetectFunction;
177}
Info
Analysis containing CSE Info
Definition:CSEInfo.cpp:27
CommandLine.h
DebugOptions.h
ManagedStatic.h
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
DefaultAutoDetectFunction
static bool DefaultAutoDetectFunction(const raw_ostream &OS)
Definition:WithColor.cpp:36
UseColor
static ManagedStatic< cl::opt< cl::boolOrDefault >, CreateUseColor > UseColor
Definition:WithColor.cpp:33
WithColor.h
llvm::ErrorInfoBase
Base class for error info classes.
Definition:Error.h:45
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition:ManagedStatic.h:83
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::WithColor
An RAII object that temporarily switches an output stream to a specific color.
Definition:WithColor.h:54
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:143
llvm::WithColor::defaultWarningHandler
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition:WithColor.cpp:164
llvm::WithColor::WithColor
LLVM_CTOR_NODISCARD WithColor(raw_ostream &OS, HighlightColor S, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition:WithColor.cpp:44
llvm::WithColor::colorsEnabled
bool colorsEnabled()
Determine whether colors are displayed.
Definition:WithColor.cpp:131
llvm::WithColor::warning
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition:WithColor.cpp:85
llvm::WithColor::~WithColor
~WithColor()
Definition:WithColor.cpp:156
llvm::WithColor::error
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition:WithColor.cpp:83
llvm::WithColor::AutoDetectFunctionType
bool(*)(const raw_ostream &OS) AutoDetectFunctionType
Definition:WithColor.h:56
llvm::WithColor::resetColor
WithColor & resetColor()
Reset the colors to terminal defaults.
Definition:WithColor.cpp:150
llvm::WithColor::get
raw_ostream & get()
Definition:WithColor.h:79
llvm::WithColor::defaultErrorHandler
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition:WithColor.cpp:158
llvm::WithColor::note
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition:WithColor.cpp:87
llvm::WithColor::defaultAutoDetectFunction
static AutoDetectFunctionType defaultAutoDetectFunction()
Retrieve the default color auto detection function.
Definition:WithColor.cpp:170
llvm::WithColor::setAutoDetectFunction
static void setAutoDetectFunction(AutoDetectFunctionType NewAutoDetectFunction)
Change the global auto detection function.
Definition:WithColor.cpp:174
llvm::WithColor::remark
static raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition:WithColor.cpp:89
llvm::cl::OptionCategory
Definition:CommandLine.h:179
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::raw_ostream::YELLOW
static constexpr Colors YELLOW
Definition:raw_ostream.h:117
llvm::raw_ostream::CYAN
static constexpr Colors CYAN
Definition:raw_ostream.h:120
llvm::raw_ostream::Colors
Colors
Definition:raw_ostream.h:93
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:520
llvm::raw_ostream::resetColor
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition:raw_ostream.cpp:533
llvm::raw_ostream::BLUE
static constexpr Colors BLUE
Definition:raw_ostream.h:118
llvm::raw_ostream::MAGENTA
static constexpr Colors MAGENTA
Definition:raw_ostream.h:119
llvm::raw_ostream::BLACK
static constexpr Colors BLACK
Definition:raw_ostream.h:114
llvm::raw_ostream::GREEN
static constexpr Colors GREEN
Definition:raw_ostream.h:116
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:351
llvm::raw_ostream::RED
static constexpr Colors RED
Definition:raw_ostream.h:115
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
Error.h
llvm::cl::BOU_UNSET
@ BOU_UNSET
Definition:CommandLine.h:637
llvm::cl::BOU_TRUE
@ BOU_TRUE
Definition:CommandLine.h:637
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::initWithColorOptions
void initWithColorOptions()
Definition:WithColor.cpp:34
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:977
llvm::getColorCategory
cl::OptionCategory & getColorCategory()
Definition:WithColor.cpp:19
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition:raw_ostream.cpp:907
llvm::HighlightColor
HighlightColor
Definition:WithColor.h:27
llvm::HighlightColor::Warning
@ Warning
llvm::HighlightColor::String
@ String
llvm::HighlightColor::Enumerator
@ Enumerator
llvm::HighlightColor::Note
@ Note
llvm::HighlightColor::Macro
@ Macro
llvm::HighlightColor::Error
@ Error
llvm::HighlightColor::Remark
@ Remark
llvm::HighlightColor::Tag
@ Tag
llvm::HighlightColor::Address
@ Address
llvm::HighlightColor::Attribute
@ Attribute
llvm::ColorMode
ColorMode
Definition:WithColor.h:40
llvm::ColorMode::Auto
@ Auto
Determine whether to use color based on the command line argument and the raw_ostream.
llvm::ColorMode::Enable
@ Enable
Enable colors.
llvm::ColorMode::Disable
@ Disable
Disable colors.
llvm::cl::cat
Definition:CommandLine.h:468
llvm::cl::desc
Definition:CommandLine.h:409

Generated on Thu Jul 17 2025 13:51:17 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp