blob: bbc7b7334211859b8fef08a5cf6ff2a586a31699 [file] [log] [blame]
Zachary Turnerf435a7e2018-07-20 17:27:48 +00001//===-- llvm-undname.cpp - Microsoft ABI name undecorator
2//------------------===//
3//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turnerf435a7e2018-07-20 17:27:48 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility works like the windows undname utility. It converts mangled
11// Microsoft symbol names into pretty C/C++ human-readable names.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Demangle/Demangle.h"
17#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/InitLLVM.h"
19#include "llvm/Support/Process.h"
Jonas Devliegherebada60a2018-11-11 22:11:47 +000020#include "llvm/Support/WithColor.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000021#include "llvm/Support/raw_ostream.h"
22#include <cstdio>
23#include <cstring>
24#include <iostream>
25#include <string>
26
27using namespace llvm;
28
Zachary Turner3a758e22018-08-01 18:33:04 +000029cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
30 cl::desc("dump backreferences"), cl::Hidden,
31 cl::init(false));
Zachary Turnerf435a7e2018-07-20 17:27:48 +000032cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
33 cl::ZeroOrMore);
34
James Hendersonce5b5b42019-01-17 15:18:44 +000035static void msDemangle(const std::string &S) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +000036 int Status;
Zachary Turner3a758e22018-08-01 18:33:04 +000037 MSDemangleFlags Flags = MSDF_None;
38 if (DumpBackReferences)
39 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
40
41 char *ResultBuf =
42 microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);
Zachary Turnerf435a7e2018-07-20 17:27:48 +000043 if (Status == llvm::demangle_success) {
44 outs() << ResultBuf << "\n";
45 outs().flush();
46 } else {
Jonas Devliegherebada60a2018-11-11 22:11:47 +000047 WithColor::error() << "Invalid mangled name\n";
Zachary Turnerf435a7e2018-07-20 17:27:48 +000048 }
49 std::free(ResultBuf);
Martin Storsjo21524be2018-07-20 20:48:36 +000050}
Zachary Turnerf435a7e2018-07-20 17:27:48 +000051
52int main(int argc, char **argv) {
53 InitLLVM X(argc, argv);
54
55 cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
56
57 if (Symbols.empty()) {
58 while (true) {
59 std::string LineStr;
60 std::getline(std::cin, LineStr);
61 if (std::cin.eof())
62 break;
63
64 StringRef Line(LineStr);
65 Line = Line.trim();
66 if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
67 continue;
68
69 // If the user is manually typing in these decorated names, don't echo
70 // them to the terminal a second time. If they're coming from redirected
71 // input, however, then we should display the input line so that the
72 // mangled and demangled name can be easily correlated in the output.
Zachary Turnerc93b8702018-07-21 15:39:05 +000073 if (!sys::Process::StandardInIsUserInput()) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +000074 outs() << Line << "\n";
Zachary Turnerc93b8702018-07-21 15:39:05 +000075 outs().flush();
76 }
James Hendersonce5b5b42019-01-17 15:18:44 +000077 msDemangle(Line);
Zachary Turnerf435a7e2018-07-20 17:27:48 +000078 outs() << "\n";
79 }
80 } else {
81 for (StringRef S : Symbols) {
82 outs() << S << "\n";
Zachary Turnerc93b8702018-07-21 15:39:05 +000083 outs().flush();
James Hendersonce5b5b42019-01-17 15:18:44 +000084 msDemangle(S);
Zachary Turnerf435a7e2018-07-20 17:27:48 +000085 outs() << "\n";
86 }
87 }
88
89 return 0;
Martin Storsjo21524be2018-07-20 20:48:36 +000090}