[llvm-pdbdump] Colorize output.
[oota-llvm.git] / tools / llvm-pdbdump / LinePrinter.cpp
1 //===- LinePrinter.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 "LinePrinter.h"
11
12 #include <algorithm>
13
14 using namespace llvm;
15
16 LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
17     : IndentSpaces(Indent), CurrentIndent(0), OS(Stream) {}
18
19 void LinePrinter::Indent() { CurrentIndent += IndentSpaces; }
20
21 void LinePrinter::Unindent() {
22   CurrentIndent = std::max(0, CurrentIndent - IndentSpaces);
23 }
24
25 void LinePrinter::NewLine() {
26   OS << "\n";
27   OS.indent(CurrentIndent);
28 }
29
30 WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) {
31   if (C == PDB_ColorItem::None)
32     OS.resetColor();
33   else {
34     raw_ostream::Colors Color;
35     bool Bold;
36     translateColor(C, Color, Bold);
37     OS.changeColor(Color, Bold);
38   }
39 }
40
41 WithColor::~WithColor() { OS.resetColor(); }
42
43 void WithColor::translateColor(PDB_ColorItem C, raw_ostream::Colors &Color,
44                                bool &Bold) const {
45   switch (C) {
46   case PDB_ColorItem::Address:
47     Color = raw_ostream::YELLOW;
48     Bold = true;
49     return;
50   case PDB_ColorItem::Keyword:
51     Color = raw_ostream::MAGENTA;
52     Bold = true;
53     return;
54   case PDB_ColorItem::Offset:
55     Color = raw_ostream::YELLOW;
56     Bold = false;
57     return;
58   case PDB_ColorItem::Type:
59     Color = raw_ostream::CYAN;
60     Bold = true;
61     return;
62   case PDB_ColorItem::Identifier:
63     Color = raw_ostream::CYAN;
64     Bold = false;
65     return;
66   case PDB_ColorItem::Path:
67     Color = raw_ostream::CYAN;
68     Bold = false;
69     return;
70   case PDB_ColorItem::SectionHeader:
71     Color = raw_ostream::RED;
72     Bold = true;
73     return;
74   case PDB_ColorItem::LiteralValue:
75     Color = raw_ostream::GREEN;
76     Bold = true;
77   default:
78     return;
79   }
80 }