/home/arjun/llvm-project/llvm/include/llvm/Support/WithColor.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
12 | | #include "llvm/Support/CommandLine.h" |
13 | | |
14 | | namespace llvm { |
15 | | |
16 | | class Error; |
17 | | class raw_ostream; |
18 | | class StringRef; |
19 | | |
20 | | extern cl::OptionCategory ColorCategory; |
21 | | |
22 | | // Symbolic names for various syntax elements. |
23 | | enum class HighlightColor { |
24 | | Address, |
25 | | String, |
26 | | Tag, |
27 | | Attribute, |
28 | | Enumerator, |
29 | | Macro, |
30 | | Error, |
31 | | Warning, |
32 | | Note, |
33 | | Remark |
34 | | }; |
35 | | |
36 | | /// An RAII object that temporarily switches an output stream to a specific |
37 | | /// color. |
38 | | class WithColor { |
39 | | raw_ostream &OS; |
40 | | bool DisableColors; |
41 | | |
42 | | public: |
43 | | /// To be used like this: WithColor(OS, HighlightColor::String) << "text"; |
44 | | /// @param OS The output stream |
45 | | /// @param S Symbolic name for syntax element to color |
46 | | /// @param DisableColors Whether to ignore color changes regardless of -color |
47 | | /// and support in OS |
48 | | WithColor(raw_ostream &OS, HighlightColor S, bool DisableColors = false); |
49 | | /// To be used like this: WithColor(OS, raw_ostream::Black) << "text"; |
50 | | /// @param OS The output stream |
51 | | /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to |
52 | | /// change only the bold attribute, and keep colors untouched |
53 | | /// @param Bold Bold/brighter text, default false |
54 | | /// @param BG If true, change the background, default: change foreground |
55 | | /// @param DisableColors Whether to ignore color changes regardless of -color |
56 | | /// and support in OS |
57 | | WithColor(raw_ostream &OS, |
58 | | raw_ostream::Colors Color = raw_ostream::SAVEDCOLOR, |
59 | | bool Bold = false, bool BG = false, bool DisableColors = false) |
60 | 0 | : OS(OS), DisableColors(DisableColors) { |
61 | 0 | changeColor(Color, Bold, BG); |
62 | 0 | } |
63 | | ~WithColor(); |
64 | | |
65 | 0 | raw_ostream &get() { return OS; } |
66 | 0 | operator raw_ostream &() { return OS; } |
67 | 0 | template <typename T> WithColor &operator<<(T &O) { |
68 | 0 | OS << O; |
69 | 0 | return *this; |
70 | 0 | } Unexecuted instantiation: _ZN4llvm9WithColorlsIPKcEERS0_RT_ Unexecuted instantiation: _ZN4llvm9WithColorlsIcEERS0_RT_ |
71 | 0 | template <typename T> WithColor &operator<<(const T &O) { |
72 | 0 | OS << O; |
73 | 0 | return *this; |
74 | 0 | } Unexecuted instantiation: _ZN4llvm9WithColorlsIA3_cEERS0_RKT_ Unexecuted instantiation: _ZN4llvm9WithColorlsIA8_cEERS0_RKT_ Unexecuted instantiation: _ZN4llvm9WithColorlsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS0_RKT_ Unexecuted instantiation: _ZN4llvm9WithColorlsIcEERS0_RKT_ Unexecuted instantiation: _ZN4llvm9WithColorlsIiEERS0_RKT_ |
75 | | |
76 | | /// Convenience method for printing "error: " to stderr. |
77 | | static raw_ostream &error(); |
78 | | /// Convenience method for printing "warning: " to stderr. |
79 | | static raw_ostream &warning(); |
80 | | /// Convenience method for printing "note: " to stderr. |
81 | | static raw_ostream ¬e(); |
82 | | /// Convenience method for printing "remark: " to stderr. |
83 | | static raw_ostream &remark(); |
84 | | |
85 | | /// Convenience method for printing "error: " to the given stream. |
86 | | static raw_ostream &error(raw_ostream &OS, StringRef Prefix = "", |
87 | | bool DisableColors = false); |
88 | | /// Convenience method for printing "warning: " to the given stream. |
89 | | static raw_ostream &warning(raw_ostream &OS, StringRef Prefix = "", |
90 | | bool DisableColors = false); |
91 | | /// Convenience method for printing "note: " to the given stream. |
92 | | static raw_ostream ¬e(raw_ostream &OS, StringRef Prefix = "", |
93 | | bool DisableColors = false); |
94 | | /// Convenience method for printing "remark: " to the given stream. |
95 | | static raw_ostream &remark(raw_ostream &OS, StringRef Prefix = "", |
96 | | bool DisableColors = false); |
97 | | |
98 | | /// Determine whether colors are displayed. |
99 | | bool colorsEnabled(); |
100 | | |
101 | | /// Change the color of text that will be output from this point forward. |
102 | | /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to |
103 | | /// change only the bold attribute, and keep colors untouched |
104 | | /// @param Bold Bold/brighter text, default false |
105 | | /// @param BG If true, change the background, default: change foreground |
106 | | WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false, |
107 | | bool BG = false); |
108 | | |
109 | | /// Reset the colors to terminal defaults. Call this when you are done |
110 | | /// outputting colored text, or before program exit. |
111 | | WithColor &resetColor(); |
112 | | |
113 | | /// Implement default handling for Error. |
114 | | /// Print "error: " to stderr. |
115 | | static void defaultErrorHandler(Error Err); |
116 | | |
117 | | /// Implement default handling for Warning. |
118 | | /// Print "warning: " to stderr. |
119 | | static void defaultWarningHandler(Error Warning); |
120 | | }; |
121 | | |
122 | | } // end namespace llvm |
123 | | |
124 | | #endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H |