LLVM  8.0.1
WithColor.h
Go to the documentation of this file.
1 //===- WithColor.h ----------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_WITHCOLOR_H
11 #define LLVM_SUPPORT_WITHCOLOR_H
12 
13 #include "llvm/ADT/StringRef.h"
15 
16 namespace llvm {
17 
18 extern cl::OptionCategory ColorCategory;
19 
20 class raw_ostream;
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
59  bool Bold = false, bool BG = false, bool DisableColors = false)
60  : OS(OS), DisableColors(DisableColors) {
61  changeColor(Color, Bold, BG);
62  }
63  ~WithColor();
64 
65  raw_ostream &get() { return OS; }
66  operator raw_ostream &() { return OS; }
67  template <typename T> WithColor &operator<<(T &O) {
68  OS << O;
69  return *this;
70  }
71  template <typename T> WithColor &operator<<(const T &O) {
72  OS << O;
73  return *this;
74  }
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 &note();
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.
90  bool DisableColors = false);
91  /// Convenience method for printing "note: " to the given stream.
92  static raw_ostream &note(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 
114 } // end namespace llvm
115 
116 #endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:38
#define error(X)
transform warning
HighlightColor
Definition: WithColor.h:23
WithColor & operator<<(T &O)
Definition: WithColor.h:67
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:57
WithColor & operator<<(const T &O)
Definition: WithColor.h:71
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:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
cl::OptionCategory ColorCategory