SyntaxHighlighting.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //===-- SyntaxHighlighting.cpp ----------------------------------*- C++ -*-===//
  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. #include "SyntaxHighlighting.h"
  10. #include "llvm/Support/CommandLine.h"
  11. using namespace llvm;
  12. using namespace dwarf;
  13. using namespace syntax;
  14. static cl::opt<cl::boolOrDefault>
  15. UseColor("color",
  16. cl::desc("use colored syntax highlighting (default=autodetect)"),
  17. cl::init(cl::BOU_UNSET));
  18. WithColor::WithColor(llvm::raw_ostream &OS, enum HighlightColor Type) : OS(OS) {
  19. // Detect color from terminal type unless the user passed the --color option.
  20. if (UseColor == cl::BOU_UNSET ? OS.has_colors() : UseColor == cl::BOU_TRUE) {
  21. switch (Type) {
  22. case Address: OS.changeColor(llvm::raw_ostream::YELLOW); break;
  23. case String: OS.changeColor(llvm::raw_ostream::GREEN); break;
  24. case Tag: OS.changeColor(llvm::raw_ostream::BLUE); break;
  25. case Attribute: OS.changeColor(llvm::raw_ostream::CYAN); break;
  26. case Enumerator: OS.changeColor(llvm::raw_ostream::MAGENTA); break;
  27. }
  28. }
  29. }
  30. WithColor::~WithColor() {
  31. if (UseColor == cl::BOU_UNSET ? OS.has_colors() : UseColor == cl::BOU_TRUE)
  32. OS.resetColor();
  33. }