LCOV - code coverage report
Current view: top level - lib/Support - WithColor.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 23 54 42.6 %
Date: 2018-10-20 13:21:21 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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      156244 : bool WithColor::colorsEnabled(raw_ostream &OS) {
      23      156244 :   if (UseColor == cl::BOU_UNSET)
      24      156244 :     return OS.has_colors();
      25           0 :   return UseColor == cl::BOU_TRUE;
      26             : }
      27             : 
      28       78122 : WithColor::WithColor(raw_ostream &OS, HighlightColor Color) : OS(OS) {
      29             :   // Detect color from terminal type unless the user passed the --color option.
      30       78122 :   if (colorsEnabled(OS)) {
      31           0 :     switch (Color) {
      32           0 :     case HighlightColor::Address:
      33           0 :       OS.changeColor(raw_ostream::YELLOW);
      34           0 :       break;
      35           0 :     case HighlightColor::String:
      36           0 :       OS.changeColor(raw_ostream::GREEN);
      37           0 :       break;
      38           0 :     case HighlightColor::Tag:
      39           0 :       OS.changeColor(raw_ostream::BLUE);
      40           0 :       break;
      41           0 :     case HighlightColor::Attribute:
      42           0 :       OS.changeColor(raw_ostream::CYAN);
      43           0 :       break;
      44           0 :     case HighlightColor::Enumerator:
      45           0 :       OS.changeColor(raw_ostream::MAGENTA);
      46           0 :       break;
      47           0 :     case HighlightColor::Macro:
      48           0 :       OS.changeColor(raw_ostream::RED);
      49           0 :       break;
      50           0 :     case HighlightColor::Error:
      51           0 :       OS.changeColor(raw_ostream::RED, true);
      52           0 :       break;
      53           0 :     case HighlightColor::Warning:
      54           0 :       OS.changeColor(raw_ostream::MAGENTA, true);
      55           0 :       break;
      56           0 :     case HighlightColor::Note:
      57           0 :       OS.changeColor(raw_ostream::BLACK, true);
      58           0 :       break;
      59             :     }
      60             :   }
      61       78122 : }
      62             : 
      63         234 : raw_ostream &WithColor::error() { return error(errs()); }
      64             : 
      65         282 : raw_ostream &WithColor::warning() { return warning(errs()); }
      66             : 
      67         282 : raw_ostream &WithColor::note() { return note(errs()); }
      68             : 
      69         264 : raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix) {
      70         264 :   if (!Prefix.empty())
      71          55 :     OS << Prefix << ": ";
      72         264 :   return WithColor(OS, HighlightColor::Error).get() << "error: ";
      73             : }
      74             : 
      75         184 : raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix) {
      76         184 :   if (!Prefix.empty())
      77          22 :     OS << Prefix << ": ";
      78         184 :   return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
      79             : }
      80             : 
      81         151 : raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix) {
      82         151 :   if (!Prefix.empty())
      83           0 :     OS << Prefix << ": ";
      84         151 :   return WithColor(OS, HighlightColor::Note).get() << "note: ";
      85             : }
      86             : 
      87      156244 : WithColor::~WithColor() {
      88       78122 :   if (colorsEnabled(OS))
      89           0 :     OS.resetColor();
      90       78122 : }

Generated by: LCOV version 1.13