]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Support/WithColor.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Support / WithColor.cpp
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
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
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
63 raw_ostream &WithColor::error() { return error(errs()); }
64
65 raw_ostream &WithColor::warning() { return warning(errs()); }
66
67 raw_ostream &WithColor::note() { return note(errs()); }
68
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
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
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
87 WithColor::~WithColor() {
88   if (colorsEnabled(OS))
89     OS.resetColor();
90 }