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 bool WithColor::colorsEnabled(raw_ostream &OS) {
23 if (UseColor == cl::BOU_UNSET)
24 return OS.has_colors();
25 return UseColor == cl::BOU_TRUE;
28 WithColor::WithColor(raw_ostream &OS, HighlightColor Color) : OS(OS) {
29 // Detect color from terminal type unless the user passed the --color option.
30 if (colorsEnabled(OS)) {
32 case HighlightColor::Address:
33 OS.changeColor(raw_ostream::YELLOW);
35 case HighlightColor::String:
36 OS.changeColor(raw_ostream::GREEN);
38 case HighlightColor::Tag:
39 OS.changeColor(raw_ostream::BLUE);
41 case HighlightColor::Attribute:
42 OS.changeColor(raw_ostream::CYAN);
44 case HighlightColor::Enumerator:
45 OS.changeColor(raw_ostream::MAGENTA);
47 case HighlightColor::Macro:
48 OS.changeColor(raw_ostream::RED);
50 case HighlightColor::Error:
51 OS.changeColor(raw_ostream::RED, true);
53 case HighlightColor::Warning:
54 OS.changeColor(raw_ostream::MAGENTA, true);
56 case HighlightColor::Note:
57 OS.changeColor(raw_ostream::BLACK, true);
63 raw_ostream &WithColor::error() { return error(errs()); }
65 raw_ostream &WithColor::warning() { return warning(errs()); }
67 raw_ostream &WithColor::note() { return note(errs()); }
69 raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix) {
72 return WithColor(OS, HighlightColor::Error).get() << "error: ";
75 raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix) {
78 return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
81 raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix) {
84 return WithColor(OS, HighlightColor::Note).get() << "note: ";
87 WithColor::~WithColor() {
88 if (colorsEnabled(OS))