• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- WithColor.cpp ------------------------------------------------------===//
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 #include "llvm/Support/WithColor.h"
11 #include "llvm/Support/raw_ostream.h"
12 
13 using namespace llvm;
14 
15 cl::OptionCategory llvm::ColorCategory("Color Options");
16 
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));
21 
colorsEnabled(raw_ostream & OS)22 bool WithColor::colorsEnabled(raw_ostream &OS) {
23   if (UseColor == cl::BOU_UNSET)
24     return OS.has_colors();
25   return UseColor == cl::BOU_TRUE;
26 }
27 
WithColor(raw_ostream & OS,HighlightColor Color)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)) {
31     switch (Color) {
32     case HighlightColor::Address:
33       OS.changeColor(raw_ostream::YELLOW);
34       break;
35     case HighlightColor::String:
36       OS.changeColor(raw_ostream::GREEN);
37       break;
38     case HighlightColor::Tag:
39       OS.changeColor(raw_ostream::BLUE);
40       break;
41     case HighlightColor::Attribute:
42       OS.changeColor(raw_ostream::CYAN);
43       break;
44     case HighlightColor::Enumerator:
45       OS.changeColor(raw_ostream::MAGENTA);
46       break;
47     case HighlightColor::Macro:
48       OS.changeColor(raw_ostream::RED);
49       break;
50     case HighlightColor::Error:
51       OS.changeColor(raw_ostream::RED, true);
52       break;
53     case HighlightColor::Warning:
54       OS.changeColor(raw_ostream::MAGENTA, true);
55       break;
56     case HighlightColor::Note:
57       OS.changeColor(raw_ostream::BLACK, true);
58       break;
59     }
60   }
61 }
62 
error()63 raw_ostream &WithColor::error() { return error(errs()); }
64 
warning()65 raw_ostream &WithColor::warning() { return warning(errs()); }
66 
note()67 raw_ostream &WithColor::note() { return note(errs()); }
68 
error(raw_ostream & OS,StringRef Prefix)69 raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix) {
70   if (!Prefix.empty())
71     OS << Prefix << ": ";
72   return WithColor(OS, HighlightColor::Error).get() << "error: ";
73 }
74 
warning(raw_ostream & OS,StringRef Prefix)75 raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix) {
76   if (!Prefix.empty())
77     OS << Prefix << ": ";
78   return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
79 }
80 
note(raw_ostream & OS,StringRef Prefix)81 raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix) {
82   if (!Prefix.empty())
83     OS << Prefix << ": ";
84   return WithColor(OS, HighlightColor::Note).get() << "note: ";
85 }
86 
~WithColor()87 WithColor::~WithColor() {
88   if (colorsEnabled(OS))
89     OS.resetColor();
90 }
91