1 //===- WithColor.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_WITHCOLOR_H
10 #define LLVM_SUPPORT_WITHCOLOR_H
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/CommandLine.h"
17 extern cl::OptionCategory ColorCategory
;
21 // Symbolic names for various syntax elements.
22 enum class HighlightColor
{
35 /// An RAII object that temporarily switches an output stream to a specific
42 /// To be used like this: WithColor(OS, HighlightColor::String) << "text";
43 /// @param OS The output stream
44 /// @param S Symbolic name for syntax element to color
45 /// @param DisableColors Whether to ignore color changes regardless of -color
47 WithColor(raw_ostream
&OS
, HighlightColor S
, bool DisableColors
= false);
48 /// To be used like this: WithColor(OS, raw_ostream::Black) << "text";
49 /// @param OS The output stream
50 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
51 /// change only the bold attribute, and keep colors untouched
52 /// @param Bold Bold/brighter text, default false
53 /// @param BG If true, change the background, default: change foreground
54 /// @param DisableColors Whether to ignore color changes regardless of -color
56 WithColor(raw_ostream
&OS
,
57 raw_ostream::Colors Color
= raw_ostream::SAVEDCOLOR
,
58 bool Bold
= false, bool BG
= false, bool DisableColors
= false)
59 : OS(OS
), DisableColors(DisableColors
) {
60 changeColor(Color
, Bold
, BG
);
64 raw_ostream
&get() { return OS
; }
65 operator raw_ostream
&() { return OS
; }
66 template <typename T
> WithColor
&operator<<(T
&O
) {
70 template <typename T
> WithColor
&operator<<(const T
&O
) {
75 /// Convenience method for printing "error: " to stderr.
76 static raw_ostream
&error();
77 /// Convenience method for printing "warning: " to stderr.
78 static raw_ostream
&warning();
79 /// Convenience method for printing "note: " to stderr.
80 static raw_ostream
¬e();
81 /// Convenience method for printing "remark: " to stderr.
82 static raw_ostream
&remark();
84 /// Convenience method for printing "error: " to the given stream.
85 static raw_ostream
&error(raw_ostream
&OS
, StringRef Prefix
= "",
86 bool DisableColors
= false);
87 /// Convenience method for printing "warning: " to the given stream.
88 static raw_ostream
&warning(raw_ostream
&OS
, StringRef Prefix
= "",
89 bool DisableColors
= false);
90 /// Convenience method for printing "note: " to the given stream.
91 static raw_ostream
¬e(raw_ostream
&OS
, StringRef Prefix
= "",
92 bool DisableColors
= false);
93 /// Convenience method for printing "remark: " to the given stream.
94 static raw_ostream
&remark(raw_ostream
&OS
, StringRef Prefix
= "",
95 bool DisableColors
= false);
97 /// Determine whether colors are displayed.
100 /// Change the color of text that will be output from this point forward.
101 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
102 /// change only the bold attribute, and keep colors untouched
103 /// @param Bold Bold/brighter text, default false
104 /// @param BG If true, change the background, default: change foreground
105 WithColor
&changeColor(raw_ostream::Colors Color
, bool Bold
= false,
108 /// Reset the colors to terminal defaults. Call this when you are done
109 /// outputting colored text, or before program exit.
110 WithColor
&resetColor();
113 } // end namespace llvm
115 #endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H