WithColor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. switch (Color) {
  21. case HighlightColor::Address:
  22. OS.changeColor(raw_ostream::YELLOW);
  23. break;
  24. case HighlightColor::String:
  25. OS.changeColor(raw_ostream::GREEN);
  26. break;
  27. case HighlightColor::Tag:
  28. OS.changeColor(raw_ostream::BLUE);
  29. break;
  30. case HighlightColor::Attribute:
  31. OS.changeColor(raw_ostream::CYAN);
  32. break;
  33. case HighlightColor::Enumerator:
  34. OS.changeColor(raw_ostream::MAGENTA);
  35. break;
  36. case HighlightColor::Macro:
  37. OS.changeColor(raw_ostream::RED);
  38. break;
  39. case HighlightColor::Error:
  40. OS.changeColor(raw_ostream::RED, true);
  41. break;
  42. case HighlightColor::Warning:
  43. OS.changeColor(raw_ostream::MAGENTA, true);
  44. break;
  45. case HighlightColor::Note:
  46. OS.changeColor(raw_ostream::BLACK, true);
  47. break;
  48. case HighlightColor::Remark:
  49. OS.changeColor(raw_ostream::BLUE, true);
  50. break;
  51. }
  52. }
  53. }
  54. raw_ostream &WithColor::error() { return error(errs()); }
  55. raw_ostream &WithColor::warning() { return warning(errs()); }
  56. raw_ostream &WithColor::note() { return note(errs()); }
  57. raw_ostream &WithColor::remark() { return remark(errs()); }
  58. raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix,
  59. bool DisableColors) {
  60. if (!Prefix.empty())
  61. OS << Prefix << ": ";
  62. return WithColor(OS, HighlightColor::Error, DisableColors).get()
  63. << "error: ";
  64. }
  65. raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
  66. bool DisableColors) {
  67. if (!Prefix.empty())
  68. OS << Prefix << ": ";
  69. return WithColor(OS, HighlightColor::Warning, DisableColors).get()
  70. << "warning: ";
  71. }
  72. raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
  73. bool DisableColors) {
  74. if (!Prefix.empty())
  75. OS << Prefix << ": ";
  76. return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
  77. }
  78. raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
  79. bool DisableColors) {
  80. if (!Prefix.empty())
  81. OS << Prefix << ": ";
  82. return WithColor(OS, HighlightColor::Remark, DisableColors).get()
  83. << "remark: ";
  84. }
  85. bool WithColor::colorsEnabled() {
  86. if (DisableColors)
  87. return false;
  88. if (UseColor == cl::BOU_UNSET)
  89. return OS.has_colors();
  90. return UseColor == cl::BOU_TRUE;
  91. }
  92. WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
  93. bool BG) {
  94. if (colorsEnabled())
  95. OS.changeColor(Color, Bold, BG);
  96. return *this;
  97. }
  98. WithColor &WithColor::resetColor() {
  99. if (colorsEnabled())
  100. OS.resetColor();
  101. return *this;
  102. }
  103. WithColor::~WithColor() { resetColor(); }