|
|
|||
File indexing completed on 2026-05-10 08:44:35
0001 //===- WithColor.h ----------------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLVM_SUPPORT_WITHCOLOR_H 0010 #define LLVM_SUPPORT_WITHCOLOR_H 0011 0012 #include "llvm/Support/Compiler.h" 0013 #include "llvm/Support/raw_ostream.h" 0014 0015 namespace llvm { 0016 0017 class Error; 0018 class StringRef; 0019 0020 namespace cl { 0021 class OptionCategory; 0022 } 0023 0024 extern cl::OptionCategory &getColorCategory(); 0025 0026 // Symbolic names for various syntax elements. 0027 enum class HighlightColor { 0028 Address, 0029 String, 0030 Tag, 0031 Attribute, 0032 Enumerator, 0033 Macro, 0034 Error, 0035 Warning, 0036 Note, 0037 Remark 0038 }; 0039 0040 enum class ColorMode { 0041 /// Determine whether to use color based on the command line argument and the 0042 /// raw_ostream. 0043 Auto, 0044 /// Enable colors. Because raw_ostream is the one implementing colors, this 0045 /// has no effect if the stream does not support colors or has colors 0046 /// disabled. 0047 Enable, 0048 /// Disable colors. 0049 Disable, 0050 }; 0051 0052 /// An RAII object that temporarily switches an output stream to a specific 0053 /// color. 0054 class WithColor { 0055 public: 0056 using AutoDetectFunctionType = bool (*)(const raw_ostream &OS); 0057 0058 /// To be used like this: WithColor(OS, HighlightColor::String) << "text"; 0059 /// @param OS The output stream 0060 /// @param S Symbolic name for syntax element to color 0061 /// @param Mode Enable, disable or compute whether to use colors. 0062 LLVM_CTOR_NODISCARD WithColor(raw_ostream &OS, HighlightColor S, 0063 ColorMode Mode = ColorMode::Auto); 0064 /// To be used like this: WithColor(OS, raw_ostream::BLACK) << "text"; 0065 /// @param OS The output stream 0066 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to 0067 /// change only the bold attribute, and keep colors untouched 0068 /// @param Bold Bold/brighter text, default false 0069 /// @param BG If true, change the background, default: change foreground 0070 /// @param Mode Enable, disable or compute whether to use colors. 0071 LLVM_CTOR_NODISCARD WithColor( 0072 raw_ostream &OS, raw_ostream::Colors Color = raw_ostream::SAVEDCOLOR, 0073 bool Bold = false, bool BG = false, ColorMode Mode = ColorMode::Auto) 0074 : OS(OS), Mode(Mode) { 0075 changeColor(Color, Bold, BG); 0076 } 0077 ~WithColor(); 0078 0079 raw_ostream &get() { return OS; } 0080 operator raw_ostream &() { return OS; } 0081 template <typename T> WithColor &operator<<(T &O) { 0082 OS << O; 0083 return *this; 0084 } 0085 template <typename T> WithColor &operator<<(const T &O) { 0086 OS << O; 0087 return *this; 0088 } 0089 0090 /// Convenience method for printing "error: " to stderr. 0091 static raw_ostream &error(); 0092 /// Convenience method for printing "warning: " to stderr. 0093 static raw_ostream &warning(); 0094 /// Convenience method for printing "note: " to stderr. 0095 static raw_ostream ¬e(); 0096 /// Convenience method for printing "remark: " to stderr. 0097 static raw_ostream &remark(); 0098 0099 /// Convenience method for printing "error: " to the given stream. 0100 static raw_ostream &error(raw_ostream &OS, StringRef Prefix = "", 0101 bool DisableColors = false); 0102 /// Convenience method for printing "warning: " to the given stream. 0103 static raw_ostream &warning(raw_ostream &OS, StringRef Prefix = "", 0104 bool DisableColors = false); 0105 /// Convenience method for printing "note: " to the given stream. 0106 static raw_ostream ¬e(raw_ostream &OS, StringRef Prefix = "", 0107 bool DisableColors = false); 0108 /// Convenience method for printing "remark: " to the given stream. 0109 static raw_ostream &remark(raw_ostream &OS, StringRef Prefix = "", 0110 bool DisableColors = false); 0111 0112 /// Determine whether colors are displayed. 0113 bool colorsEnabled(); 0114 0115 /// Change the color of text that will be output from this point forward. 0116 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to 0117 /// change only the bold attribute, and keep colors untouched 0118 /// @param Bold Bold/brighter text, default false 0119 /// @param BG If true, change the background, default: change foreground 0120 WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false, 0121 bool BG = false); 0122 0123 /// Reset the colors to terminal defaults. Call this when you are done 0124 /// outputting colored text, or before program exit. 0125 WithColor &resetColor(); 0126 0127 /// Implement default handling for Error. 0128 /// Print "error: " to stderr. 0129 static void defaultErrorHandler(Error Err); 0130 0131 /// Implement default handling for Warning. 0132 /// Print "warning: " to stderr. 0133 static void defaultWarningHandler(Error Warning); 0134 0135 /// Retrieve the default color auto detection function. 0136 static AutoDetectFunctionType defaultAutoDetectFunction(); 0137 0138 /// Change the global auto detection function. 0139 static void 0140 setAutoDetectFunction(AutoDetectFunctionType NewAutoDetectFunction); 0141 0142 private: 0143 raw_ostream &OS; 0144 ColorMode Mode; 0145 0146 static AutoDetectFunctionType AutoDetectFunction; 0147 }; 0148 0149 } // end namespace llvm 0150 0151 #endif // LLVM_SUPPORT_WITHCOLOR_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|