Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/WithColor.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- WithColor.cpp ------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/Support/WithColor.h"
10
#include "llvm/Support/raw_ostream.h"
11
12
using namespace llvm;
13
14
cl::OptionCategory llvm::ColorCategory("Color Options");
15
16
static cl::opt<cl::boolOrDefault>
17
    UseColor("color", cl::cat(ColorCategory),
18
             cl::desc("Use colors in output (default=autodetect)"),
19
             cl::init(cl::BOU_UNSET));
20
21
WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
22
176k
    : OS(OS), DisableColors(DisableColors) {
23
176k
  // Detect color from terminal type unless the user passed the --color option.
24
176k
  if (colorsEnabled()) {
25
0
    switch (Color) {
26
0
    case HighlightColor::Address:
27
0
      OS.changeColor(raw_ostream::YELLOW);
28
0
      break;
29
0
    case HighlightColor::String:
30
0
      OS.changeColor(raw_ostream::GREEN);
31
0
      break;
32
0
    case HighlightColor::Tag:
33
0
      OS.changeColor(raw_ostream::BLUE);
34
0
      break;
35
0
    case HighlightColor::Attribute:
36
0
      OS.changeColor(raw_ostream::CYAN);
37
0
      break;
38
0
    case HighlightColor::Enumerator:
39
0
      OS.changeColor(raw_ostream::MAGENTA);
40
0
      break;
41
0
    case HighlightColor::Macro:
42
0
      OS.changeColor(raw_ostream::RED);
43
0
      break;
44
0
    case HighlightColor::Error:
45
0
      OS.changeColor(raw_ostream::RED, true);
46
0
      break;
47
0
    case HighlightColor::Warning:
48
0
      OS.changeColor(raw_ostream::MAGENTA, true);
49
0
      break;
50
0
    case HighlightColor::Note:
51
0
      OS.changeColor(raw_ostream::BLACK, true);
52
0
      break;
53
0
    case HighlightColor::Remark:
54
0
      OS.changeColor(raw_ostream::BLUE, true);
55
0
      break;
56
0
    }
57
0
  }
58
176k
}
59
60
267
raw_ostream &WithColor::error() { return error(errs()); }
61
62
189
raw_ostream &WithColor::warning() { return warning(errs()); }
63
64
141
raw_ostream &WithColor::note() { return note(errs()); }
65
66
0
raw_ostream &WithColor::remark() { return remark(errs()); }
67
68
raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix,
69
82.2k
                              bool DisableColors) {
70
82.2k
  if (!Prefix.empty())
71
463
    OS << Prefix << ": ";
72
82.2k
  return WithColor(OS, HighlightColor::Error, DisableColors).get()
73
82.2k
         << "error: ";
74
82.2k
}
75
76
raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
77
8.11k
                                bool DisableColors) {
78
8.11k
  if (!Prefix.empty())
79
85
    OS << Prefix << ": ";
80
8.11k
  return WithColor(OS, HighlightColor::Warning, DisableColors).get()
81
8.11k
         << "warning: ";
82
8.11k
}
83
84
raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
85
3.96k
                             bool DisableColors) {
86
3.96k
  if (!Prefix.empty())
87
0
    OS << Prefix << ": ";
88
3.96k
  return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
89
3.96k
}
90
91
raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
92
1
                               bool DisableColors) {
93
1
  if (!Prefix.empty())
94
0
    OS << Prefix << ": ";
95
1
  return WithColor(OS, HighlightColor::Remark, DisableColors).get()
96
1
         << "remark: ";
97
1
}
98
99
912k
bool WithColor::colorsEnabled() {
100
912k
  if (DisableColors)
101
548
    return false;
102
911k
  if (UseColor == cl::BOU_UNSET)
103
911k
    return OS.has_colors();
104
0
  return UseColor == cl::BOU_TRUE;
105
0
}
106
107
WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
108
279k
                                  bool BG) {
109
279k
  if (colorsEnabled())
110
0
    OS.changeColor(Color, Bold, BG);
111
279k
  return *this;
112
279k
}
113
114
456k
WithColor &WithColor::resetColor() {
115
456k
  if (colorsEnabled())
116
0
    OS.resetColor();
117
456k
  return *this;
118
456k
}
119
120
456k
WithColor::~WithColor() { resetColor(); }