WithColor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "llvm/Support/WithColor.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. using namespace llvm;
  11. cl::OptionCategory llvm::ColorCategory("Color Options");
  12. static cl::opt<cl::boolOrDefault>
  13. UseColor("color", cl::cat(ColorCategory),
  14. cl::desc("Use colors in output (default=autodetect)"),
  15. cl::init(cl::BOU_UNSET));
  16. WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
  17. : OS(OS), DisableColors(DisableColors) {
  18. // Detect color from terminal type unless the user passed the --color option.
  19. if (colorsEnabled()) {
  20. OS.enable_colors();
  21. switch (Color) {
  22. case HighlightColor::Address:
  23. OS.changeColor(raw_ostream::YELLOW);
  24. break;
  25. case HighlightColor::String:
  26. OS.changeColor(raw_ostream::GREEN);
  27. break;
  28. case HighlightColor::Tag:
  29. OS.changeColor(raw_ostream::BLUE);
  30. break;
  31. case HighlightColor::Attribute:
  32. OS.changeColor(raw_ostream::CYAN);
  33. break;
  34. case HighlightColor::Enumerator:
  35. OS.changeColor(raw_ostream::MAGENTA);
  36. break;
  37. case HighlightColor::Macro:
  38. OS.changeColor(raw_ostream::RED);
  39. break;
  40. case HighlightColor::Error:
  41. OS.changeColor(raw_ostream::RED, true);
  42. break;
  43. case HighlightColor::Warning:
  44. OS.changeColor(raw_ostream::MAGENTA, true);
  45. break;
  46. case HighlightColor::Note:
  47. OS.changeColor(raw_ostream::BLACK, true);
  48. break;
  49. case HighlightColor::Remark:
  50. OS.changeColor(raw_ostream::BLUE, true);
  51. break;
  52. }
  53. }
  54. }
  55. raw_ostream &WithColor::error() { return error(errs()); }
  56. raw_ostream &WithColor::warning() { return warning(errs()); }
  57. raw_ostream &WithColor::note() { return note(errs()); }
  58. raw_ostream &WithColor::remark() { return remark(errs()); }
  59. raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix,
  60. bool DisableColors) {
  61. if (!Prefix.empty())
  62. OS << Prefix << ": ";
  63. return WithColor(OS, HighlightColor::Error, DisableColors).get()
  64. << "error: ";
  65. }
  66. raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
  67. bool DisableColors) {
  68. if (!Prefix.empty())
  69. OS << Prefix << ": ";
  70. return WithColor(OS, HighlightColor::Warning, DisableColors).get()
  71. << "warning: ";
  72. }
  73. raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
  74. bool DisableColors) {
  75. if (!Prefix.empty())
  76. OS << Prefix << ": ";
  77. return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
  78. }
  79. raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
  80. bool DisableColors) {
  81. if (!Prefix.empty())
  82. OS << Prefix << ": ";
  83. return WithColor(OS, HighlightColor::Remark, DisableColors).get()
  84. << "remark: ";
  85. }
  86. bool WithColor::colorsEnabled() {
  87. if (DisableColors)
  88. return false;
  89. if (UseColor == cl::BOU_UNSET)
  90. return OS.has_colors();
  91. return UseColor == cl::BOU_TRUE;
  92. }
  93. WithColor &WithColor::changeColor(raw_ostream::Color C, bool Bold, bool BG) {
  94. if (colorsEnabled())
  95. OS.changeColor(C, Bold, BG);
  96. return *this;
  97. }
  98. WithColor &WithColor::resetColor() {
  99. if (colorsEnabled())
  100. OS.resetColor();
  101. return *this;
  102. }
  103. WithColor::~WithColor() { resetColor(); }