LLVM  9.0.0svn
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 
12 #include "llvm/ADT/StringRef.h"
14 
15 namespace llvm {
16 
17 extern cl::OptionCategory ColorCategory;
18 
19 class raw_ostream;
20 
21 // Symbolic names for various syntax elements.
22 enum class HighlightColor {
23  Address,
24  String,
25  Tag,
26  Attribute,
27  Enumerator,
28  Macro,
29  Error,
30  Warning,
31  Note,
32  Remark
33 };
34 
35 /// An RAII object that temporarily switches an output stream to a specific
36 /// color.
37 class WithColor {
38  raw_ostream &OS;
39  bool DisableColors;
40 
41 public:
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
46  /// and support in OS
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
55  /// and support in OS
58  bool Bold = false, bool BG = false, bool DisableColors = false)
59  : OS(OS), DisableColors(DisableColors) {
60  changeColor(Color, Bold, BG);
61  }
62  ~WithColor();
63 
64  raw_ostream &get() { return OS; }
65  operator raw_ostream &() { return OS; }
66  template <typename T> WithColor &operator<<(T &O) {
67  OS << O;
68  return *this;
69  }
70  template <typename T> WithColor &operator<<(const T &O) {
71  OS << O;
72  return *this;
73  }
74 
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 &note();
81  /// Convenience method for printing "remark: " to stderr.
82  static raw_ostream &remark();
83 
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.
89  bool DisableColors = false);
90  /// Convenience method for printing "note: " to the given stream.
91  static raw_ostream &note(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);
96 
97  /// Determine whether colors are displayed.
98  bool colorsEnabled();
99 
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,
106  bool BG = false);
107 
108  /// Reset the colors to terminal defaults. Call this when you are done
109  /// outputting colored text, or before program exit.
110  WithColor &resetColor();
111 };
112 
113 } // end namespace llvm
114 
115 #endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:37
#define error(X)
transform warning
HighlightColor
Definition: WithColor.h:22
WithColor & operator<<(T &O)
Definition: WithColor.h:66
WithColor(raw_ostream &OS, raw_ostream::Colors Color=raw_ostream::SAVEDCOLOR, bool Bold=false, bool BG=false, bool DisableColors=false)
To be used like this: WithColor(OS, raw_ostream::Black) << "text";.
Definition: WithColor.h:56
WithColor & operator<<(const T &O)
Definition: WithColor.h:70
Color
A "color", which is either even or odd.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
cl::OptionCategory ColorCategory