1 //===- WithColor.cpp ------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/WithColor.h"
11 #include "llvm/Support/raw_ostream.h"
15 cl::OptionCategory llvm::ColorCategory("Color Options");
17 static cl::opt<cl::boolOrDefault>
18 UseColor("color", cl::cat(ColorCategory),
19 cl::desc("Use colors in output (default=autodetect)"),
20 cl::init(cl::BOU_UNSET));
22 WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
23 : OS(OS), DisableColors(DisableColors) {
24 // Detect color from terminal type unless the user passed the --color option.
25 if (colorsEnabled()) {
27 case HighlightColor::Address:
28 OS.changeColor(raw_ostream::YELLOW);
30 case HighlightColor::String:
31 OS.changeColor(raw_ostream::GREEN);
33 case HighlightColor::Tag:
34 OS.changeColor(raw_ostream::BLUE);
36 case HighlightColor::Attribute:
37 OS.changeColor(raw_ostream::CYAN);
39 case HighlightColor::Enumerator:
40 OS.changeColor(raw_ostream::MAGENTA);
42 case HighlightColor::Macro:
43 OS.changeColor(raw_ostream::RED);
45 case HighlightColor::Error:
46 OS.changeColor(raw_ostream::RED, true);
48 case HighlightColor::Warning:
49 OS.changeColor(raw_ostream::MAGENTA, true);
51 case HighlightColor::Note:
52 OS.changeColor(raw_ostream::BLACK, true);
54 case HighlightColor::Remark:
55 OS.changeColor(raw_ostream::BLUE, true);
61 raw_ostream &WithColor::error() { return error(errs()); }
63 raw_ostream &WithColor::warning() { return warning(errs()); }
65 raw_ostream &WithColor::note() { return note(errs()); }
67 raw_ostream &WithColor::remark() { return remark(errs()); }
69 raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix,
73 return WithColor(OS, HighlightColor::Error, DisableColors).get()
77 raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
81 return WithColor(OS, HighlightColor::Warning, DisableColors).get()
85 raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
89 return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
92 raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
96 return WithColor(OS, HighlightColor::Remark, DisableColors).get()
100 bool WithColor::colorsEnabled() {
103 if (UseColor == cl::BOU_UNSET)
104 return OS.has_colors();
105 return UseColor == cl::BOU_TRUE;
108 WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
111 OS.changeColor(Color, Bold, BG);
115 WithColor &WithColor::resetColor() {
121 WithColor::~WithColor() { resetColor(); }