llvm.org GIT mirror llvm / aea3b23
Implement a very basic colored syntax highlighting for llvm-dwarfdump. The color scheme is the same as the one used by the colorize dwarfdump script on Darwin. A new --color option can be used to forcibly turn color on or off. http://reviews.llvm.org/D6852 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225269 91177308-0d34-0410-b5e6-96231b3b80d8 Adrian Prantl 5 years ago
5 changed file(s) with 113 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
1414 DWARFFormValue.cpp
1515 DWARFTypeUnit.cpp
1616 DWARFUnit.cpp
17 SyntaxHighlighting.cpp
1718 )
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "SyntaxHighlighting.h"
910 #include "llvm/DebugInfo/DWARFDebugInfoEntry.h"
1011 #include "llvm/DebugInfo/DWARFCompileUnit.h"
1112 #include "llvm/DebugInfo/DWARFContext.h"
1819 #include "llvm/Support/raw_ostream.h"
1920 using namespace llvm;
2021 using namespace dwarf;
22 using namespace syntax;
2123
2224 // Small helper to extract a DIE pointed by a reference
2325 // attribute. It looks up the Unit containing the DIE and calls
3840
3941 if (debug_info_data.isValidOffset(offset)) {
4042 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
41
42 OS << format("\n0x%8.8x: ", Offset);
43 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
44
4345 if (abbrCode) {
4446 if (AbbrevDecl) {
45 const char *tagString = TagString(getTag());
46 if (tagString)
47 OS.indent(indent) << tagString;
48 else
49 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
47 const char *tagString = TagString(getTag());
48 if (tagString)
49 WithColor(OS, syntax::Tag).get().indent(indent) << tagString;
50 else
51 WithColor(OS, syntax::Tag).get().indent(indent) <<
52 format("DW_TAG_Unknown_%x", getTag());
53
5054 OS << format(" [%u] %c\n", abbrCode,
5155 AbbrevDecl->hasChildren() ? '*' : ' ');
5256
113117 OS.indent(indent+2);
114118 const char *attrString = AttributeString(attr);
115119 if (attrString)
116 OS << attrString;
120 WithColor(OS, syntax::Attribute) << attrString;
117121 else
118 OS << format("DW_AT_Unknown_%x", attr);
122 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr);
123
119124 const char *formString = FormEncodingString(form);
120125 if (formString)
121126 OS << " [" << formString << ']';
131136
132137 const char *Name = nullptr;
133138 std::string File;
139 auto Color = syntax::Enumerator;
134140 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
141 Color = syntax::String;
135142 if (const auto *LT = u->getContext().getLineTableForUnit(u))
136143 if (LT->getFileNameByIndex(
137144 formValue.getAsUnsignedConstant().getValue(),
143150 } else if (Optional Val = formValue.getAsUnsignedConstant())
144151 Name = AttributeValueString(attr, *Val);
145152
146 if (Name) {
147 OS << Name;
148 } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
153 if (Name)
154 WithColor(OS, Color) << Name;
155 else if (attr == DW_AT_decl_line || attr == DW_AT_call_line)
149156 OS << *formValue.getAsUnsignedConstant();
150 } else {
157 else
151158 formValue.dump(OS, u);
152 }
153159
154160 // We have dumped the attribute raw value. For some attributes
155161 // having both the raw value and the pretty-printed value is
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "SyntaxHighlighting.h"
910 #include "llvm/DebugInfo/DWARFFormValue.h"
1011 #include "llvm/ADT/ArrayRef.h"
1112 #include "llvm/ADT/StringRef.h"
1819 #include
1920 using namespace llvm;
2021 using namespace dwarf;
22 using namespace syntax;
2123
2224 namespace {
2325 uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
422424 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)uvalue);
423425 Optional DbgStr = getAsCString(cu);
424426 if (DbgStr.hasValue()) {
425 OS << '"';
426 OS.write_escaped(DbgStr.getValue());
427 OS << '"';
427 raw_ostream &COS = WithColor(OS, syntax::String);
428 COS << '"';
429 COS.write_escaped(DbgStr.getValue());
430 COS << '"';
428431 }
429432 break;
430433 }
432435 OS << format(" indexed (%8.8x) string = ", (uint32_t)uvalue);
433436 Optional DbgStr = getAsCString(cu);
434437 if (DbgStr.hasValue()) {
435 OS << '"';
436 OS.write_escaped(DbgStr.getValue());
437 OS << '"';
438 raw_ostream &COS = WithColor(OS, syntax::String);
439 COS << '"';
440 COS.write_escaped(DbgStr.getValue());
441 COS << '"';
438442 }
439443 break;
440444 }
478482 break;
479483 }
480484
481 if (cu_relative_offset)
482 OS << format(" => {0x%8.8" PRIx64 "}", uvalue + (cu ? cu->getOffset() : 0));
485 if (cu_relative_offset) {
486 OS << " => {";
487 WithColor(OS, syntax::Address).get()
488 << format("0x%8.8" PRIx64, uvalue + (cu ? cu->getOffset() : 0));
489 OS << "}";
490 }
483491 }
484492
485493 Optional DWARFFormValue::getAsCString(const DWARFUnit *U) const {
0 //===-- SyntaxHighlighting.cpp ----------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
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
15 static cl::opt
16 UseColor("color",
17 cl::desc("use colored syntax highlighting (default=autodetect)"),
18 cl::init(cl::BOU_UNSET));
19
20 WithColor::WithColor(llvm::raw_ostream &OS, enum HighlightColor Type) : OS(OS) {
21 // Detect color from terminal type unless the user passed the --color option.
22 if (UseColor == cl::BOU_UNSET ? OS.has_colors() : UseColor == cl::BOU_TRUE) {
23 switch (Type) {
24 case Address: OS.changeColor(llvm::raw_ostream::YELLOW); break;
25 case String: OS.changeColor(llvm::raw_ostream::GREEN); break;
26 case Tag: OS.changeColor(llvm::raw_ostream::BLUE); break;
27 case Attribute: OS.changeColor(llvm::raw_ostream::CYAN); break;
28 case Enumerator: OS.changeColor(llvm::raw_ostream::MAGENTA); break;
29 }
30 }
31 }
32
33 WithColor::~WithColor() {
34 if (UseColor == cl::BOU_UNSET ? OS.has_colors() : UseColor == cl::BOU_TRUE)
35 OS.resetColor();
36 }
0 //===-- SyntaxHighlighting.h ------------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_LIB_DEBUGINFO_SYNTAXHIGHLIGHTING_H
10 #define LLVM_LIB_DEBUGINFO_SYNTAXHIGHLIGHTING_H
11
12 #include "llvm/Support/raw_ostream.h"
13
14 namespace llvm {
15 namespace dwarf {
16 namespace syntax {
17
18 // Symbolic names for various syntax elements.
19 enum HighlightColor { Address, String, Tag, Attribute, Enumerator };
20
21 /// An RAII object that temporarily switches an output stream to a
22 /// specific color.
23 class WithColor {
24 llvm::raw_ostream &OS;
25
26 public:
27 /// To be used like this: WithColor(OS, syntax::String) << "text";
28 WithColor(llvm::raw_ostream &OS, enum HighlightColor Type);
29 ~WithColor();
30
31 llvm::raw_ostream& get() { return OS; }
32 operator llvm::raw_ostream& () { return OS; }
33 };
34 }
35 }
36 }
37
38 #endif