blob: 60520c8f7be0c521bb4764432807a423e24e6568 [file] [log] [blame]
Zachary Turner42e40ea2018-07-20 17:27:48 +00001//===-- llvm-undname.cpp - Microsoft ABI name undecorator
2//------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This utility works like the windows undname utility. It converts mangled
12// Microsoft symbol names into pretty C/C++ human-readable names.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Demangle/Demangle.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/InitLLVM.h"
20#include "llvm/Support/Process.h"
Jonas Devlieghered96e13f2018-11-11 22:11:47 +000021#include "llvm/Support/WithColor.h"
Zachary Turner42e40ea2018-07-20 17:27:48 +000022#include "llvm/Support/raw_ostream.h"
23#include <cstdio>
24#include <cstring>
25#include <iostream>
26#include <string>
27
28using namespace llvm;
29
Zachary Turner39fff692018-08-01 18:33:04 +000030cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
31 cl::desc("dump backreferences"), cl::Hidden,
32 cl::init(false));
Zachary Turner42e40ea2018-07-20 17:27:48 +000033cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
34 cl::ZeroOrMore);
35
36static void demangle(const std::string &S) {
37 int Status;
Zachary Turner39fff692018-08-01 18:33:04 +000038 MSDemangleFlags Flags = MSDF_None;
39 if (DumpBackReferences)
40 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
41
42 char *ResultBuf =
43 microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);
Zachary Turner42e40ea2018-07-20 17:27:48 +000044 if (Status == llvm::demangle_success) {
45 outs() << ResultBuf << "\n";
46 outs().flush();
47 } else {
Jonas Devlieghered96e13f2018-11-11 22:11:47 +000048 WithColor::error() << "Invalid mangled name\n";
Zachary Turner42e40ea2018-07-20 17:27:48 +000049 }
50 std::free(ResultBuf);
Martin Storsjo9f7b7482018-07-20 20:48:36 +000051}
Zachary Turner42e40ea2018-07-20 17:27:48 +000052
53int main(int argc, char **argv) {
54 InitLLVM X(argc, argv);
55
56 cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
57
58 if (Symbols.empty()) {
59 while (true) {
60 std::string LineStr;
61 std::getline(std::cin, LineStr);
62 if (std::cin.eof())
63 break;
64
65 StringRef Line(LineStr);
66 Line = Line.trim();
67 if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
68 continue;
69
70 // If the user is manually typing in these decorated names, don't echo
71 // them to the terminal a second time. If they're coming from redirected
72 // input, however, then we should display the input line so that the
73 // mangled and demangled name can be easily correlated in the output.
Zachary Turner322f6d02018-07-21 15:39:05 +000074 if (!sys::Process::StandardInIsUserInput()) {
Zachary Turner42e40ea2018-07-20 17:27:48 +000075 outs() << Line << "\n";
Zachary Turner322f6d02018-07-21 15:39:05 +000076 outs().flush();
77 }
Zachary Turner42e40ea2018-07-20 17:27:48 +000078 demangle(Line);
79 outs() << "\n";
80 }
81 } else {
82 for (StringRef S : Symbols) {
83 outs() << S << "\n";
Zachary Turner322f6d02018-07-21 15:39:05 +000084 outs().flush();
Zachary Turner42e40ea2018-07-20 17:27:48 +000085 demangle(S);
86 outs() << "\n";
87 }
88 }
89
90 return 0;
Martin Storsjo9f7b7482018-07-20 20:48:36 +000091}