LLVM 23.0.0git
WithColor.h
Go to the documentation of this file.
1//===- WithColor.h ----------------------------------------------*- C++ -*-===//
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#ifndef LLVM_SUPPORT_WITHCOLOR_H
10#define LLVM_SUPPORT_WITHCOLOR_H
11
14
15namespace llvm {
16
17class Error;
18class StringRef;
19
20namespace cl {
21class OptionCategory;
22}
23
25
26// Symbolic names for various syntax elements.
39
40enum class ColorMode {
41 /// Determine whether to use color based on the command line argument and the
42 /// raw_ostream.
44 /// Enable colors. Because raw_ostream is the one implementing colors, this
45 /// has no effect if the stream does not support colors or has colors
46 /// disabled.
48 /// Disable colors.
50};
51
52/// An RAII object that temporarily switches an output stream to a specific
53/// color.
54class WithColor {
55public:
56 using AutoDetectFunctionType = bool (*)(const raw_ostream &OS);
57
58 /// To be used like this: WithColor(OS, HighlightColor::String) << "text";
59 /// @param OS The output stream
60 /// @param S Symbolic name for syntax element to color
61 /// @param Mode Enable, disable or compute whether to use colors.
64 /// To be used like this: WithColor(OS, raw_ostream::BLACK) << "text";
65 /// @param OS The output stream
66 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
67 /// change only the bold attribute, and keep colors untouched
68 /// @param Bold Bold/brighter text, default false
69 /// @param BG If true, change the background, default: change foreground
70 /// @param Mode Enable, disable or compute whether to use colors.
71 ///
72 /// FIXME: If Color == SAVEDCOLOR, Bold == false is currently ignored.
75 bool Bold = false, bool BG = false, ColorMode Mode = ColorMode::Auto)
76 : OS(OS), Mode(Mode) {
77 changeColor(Color, Bold, BG);
78 }
80
81 raw_ostream &get() { return OS; }
82 operator raw_ostream &() { return OS; }
83 template <typename T> WithColor &operator<<(T &O) {
84 OS << O;
85 return *this;
86 }
87 template <typename T> WithColor &operator<<(const T &O) {
88 OS << O;
89 return *this;
90 }
91
92 /// Convenience method for printing "error: " to stderr.
93 LLVM_ABI static raw_ostream &error();
94 /// Convenience method for printing "warning: " to stderr.
96 /// Convenience method for printing "note: " to stderr.
97 LLVM_ABI static raw_ostream &note();
98 /// Convenience method for printing "remark: " to stderr.
99 LLVM_ABI static raw_ostream &remark();
100
101 /// Convenience method for printing "error: " to the given stream.
102 LLVM_ABI static raw_ostream &error(raw_ostream &OS, StringRef Prefix = "",
103 bool DisableColors = false);
104 /// Convenience method for printing "warning: " to the given stream.
105 LLVM_ABI static raw_ostream &warning(raw_ostream &OS, StringRef Prefix = "",
106 bool DisableColors = false);
107 /// Convenience method for printing "note: " to the given stream.
108 LLVM_ABI static raw_ostream &note(raw_ostream &OS, StringRef Prefix = "",
109 bool DisableColors = false);
110 /// Convenience method for printing "remark: " to the given stream.
111 LLVM_ABI static raw_ostream &remark(raw_ostream &OS, StringRef Prefix = "",
112 bool DisableColors = false);
113
114 /// Determine whether colors are displayed.
115 LLVM_ABI bool colorsEnabled();
116
117 /// Change the color of text that will be output from this point forward.
118 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
119 /// change only the bold attribute, and keep colors untouched
120 /// @param Bold Bold/brighter text, default false
121 /// @param BG If true, change the background, default: change foreground
122 ///
123 /// FIXME: If Color == SAVEDCOLOR, Bold == false is currently ignored.
124 LLVM_ABI WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false,
125 bool BG = false);
126
127 /// Reset the colors to terminal defaults. Call this when you are done
128 /// outputting colored text, or before program exit.
130
131 /// Implement default handling for Error.
132 /// Print "error: " to stderr.
133 LLVM_ABI static void defaultErrorHandler(Error Err);
134
135 /// Implement default handling for Warning.
136 /// Print "warning: " to stderr.
138
139 /// Retrieve the default color auto detection function.
141
142 /// Change the global auto detection function.
143 LLVM_ABI static void
144 setAutoDetectFunction(AutoDetectFunctionType NewAutoDetectFunction);
145
146private:
147 raw_ostream &OS;
149
150 static AutoDetectFunctionType AutoDetectFunction;
151};
152
153} // end namespace llvm
154
155#endif // LLVM_SUPPORT_WITHCOLOR_H
#define LLVM_CTOR_NODISCARD
Definition Compiler.h:443
#define LLVM_ABI
Definition Compiler.h:215
#define T
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An RAII object that temporarily switches an output stream to a specific color.
Definition WithColor.h:54
LLVM_ABI 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.
static LLVM_ABI void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
LLVM_CTOR_NODISCARD LLVM_ABI WithColor(raw_ostream &OS, HighlightColor S, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, HighlightColor::String) << "text";.
Definition WithColor.cpp:45
WithColor & operator<<(const T &O)
Definition WithColor.h:87
LLVM_ABI bool colorsEnabled()
Determine whether colors are displayed.
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition WithColor.cpp:86
LLVM_ABI ~WithColor()
LLVM_CTOR_NODISCARD WithColor(raw_ostream &OS, raw_ostream::Colors Color=raw_ostream::SAVEDCOLOR, bool Bold=false, bool BG=false, ColorMode Mode=ColorMode::Auto)
To be used like this: WithColor(OS, raw_ostream::BLACK) << "text";.
Definition WithColor.h:73
static LLVM_ABI raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition WithColor.cpp:84
bool(*)(const raw_ostream &OS) AutoDetectFunctionType
Definition WithColor.h:56
LLVM_ABI WithColor & resetColor()
Reset the colors to terminal defaults.
raw_ostream & get()
Definition WithColor.h:81
static LLVM_ABI void defaultErrorHandler(Error Err)
Implement default handling for Error.
static LLVM_ABI raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition WithColor.cpp:88
static LLVM_ABI AutoDetectFunctionType defaultAutoDetectFunction()
Retrieve the default color auto detection function.
static LLVM_ABI void setAutoDetectFunction(AutoDetectFunctionType NewAutoDetectFunction)
Change the global auto detection function.
WithColor & operator<<(T &O)
Definition WithColor.h:83
static LLVM_ABI raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
Definition WithColor.cpp:90
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
static constexpr Colors SAVEDCOLOR
This namespace contains all of the command line option processing machinery.
Definition MCSchedule.h:35
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI cl::OptionCategory & getColorCategory()
Definition WithColor.cpp:19
HighlightColor
Definition WithColor.h:27
ColorMode
Definition WithColor.h:40
@ Auto
Determine whether to use color based on the command line argument and the raw_ostream.
Definition WithColor.h:43
@ Enable
Enable colors.
Definition WithColor.h:47
@ Disable
Disable colors.
Definition WithColor.h:49