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