blob: d673d16426fc0b91f505f45302a8e1ae98109e3e [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"
Nico Weber3bfa6a62019-04-16 12:51:40 +000018#include "llvm/Support/ErrorOr.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000019#include "llvm/Support/InitLLVM.h"
Nico Weber3bfa6a62019-04-16 12:51:40 +000020#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000021#include "llvm/Support/Process.h"
Jonas Devliegherebada60a2018-11-11 22:11:47 +000022#include "llvm/Support/WithColor.h"
Zachary Turnerf435a7e2018-07-20 17:27:48 +000023#include "llvm/Support/raw_ostream.h"
24#include <cstdio>
25#include <cstring>
26#include <iostream>
27#include <string>
28
29using namespace llvm;
30
Zachary Turner3a758e22018-08-01 18:33:04 +000031cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
32 cl::desc("dump backreferences"), cl::Hidden,
33 cl::init(false));
Nico Weber3bfa6a62019-04-16 12:51:40 +000034cl::opt<std::string> RawFile("raw-file", cl::Optional,
35 cl::desc("for fuzzer data"), cl::Hidden);
Zachary Turnerf435a7e2018-07-20 17:27:48 +000036cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
37 cl::ZeroOrMore);
38
Nico Weber3bfa6a62019-04-16 12:51:40 +000039static bool msDemangle(const std::string &S) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +000040 int Status;
Zachary Turner3a758e22018-08-01 18:33:04 +000041 MSDemangleFlags Flags = MSDF_None;
42 if (DumpBackReferences)
43 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
44
45 char *ResultBuf =
46 microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);
Zachary Turnerf435a7e2018-07-20 17:27:48 +000047 if (Status == llvm::demangle_success) {
48 outs() << ResultBuf << "\n";
49 outs().flush();
50 } else {
Jonas Devliegherebada60a2018-11-11 22:11:47 +000051 WithColor::error() << "Invalid mangled name\n";
Zachary Turnerf435a7e2018-07-20 17:27:48 +000052 }
53 std::free(ResultBuf);
Nico Weber3bfa6a62019-04-16 12:51:40 +000054 return Status == llvm::demangle_success;
Martin Storsjo21524be2018-07-20 20:48:36 +000055}
Zachary Turnerf435a7e2018-07-20 17:27:48 +000056
57int main(int argc, char **argv) {
58 InitLLVM X(argc, argv);
59
60 cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
61
Nico Weber3bfa6a62019-04-16 12:51:40 +000062 if (!RawFile.empty()) {
63 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
64 MemoryBuffer::getFileOrSTDIN(RawFile);
65 if (std::error_code EC = FileOrErr.getError()) {
66 WithColor::error() << "Could not open input file \'" << RawFile
67 << "\': " << EC.message() << '\n';
68 return 1;
69 }
70 return msDemangle(FileOrErr->get()->getBuffer()) ? 0 : 1;
71 }
72
73 bool Success = true;
Zachary Turnerf435a7e2018-07-20 17:27:48 +000074 if (Symbols.empty()) {
75 while (true) {
76 std::string LineStr;
77 std::getline(std::cin, LineStr);
78 if (std::cin.eof())
79 break;
80
81 StringRef Line(LineStr);
82 Line = Line.trim();
83 if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
84 continue;
85
86 // If the user is manually typing in these decorated names, don't echo
87 // them to the terminal a second time. If they're coming from redirected
88 // input, however, then we should display the input line so that the
89 // mangled and demangled name can be easily correlated in the output.
Zachary Turnerc93b8702018-07-21 15:39:05 +000090 if (!sys::Process::StandardInIsUserInput()) {
Zachary Turnerf435a7e2018-07-20 17:27:48 +000091 outs() << Line << "\n";
Zachary Turnerc93b8702018-07-21 15:39:05 +000092 outs().flush();
93 }
Nico Weber3bfa6a62019-04-16 12:51:40 +000094 if (!msDemangle(Line))
95 Success = false;
Zachary Turnerf435a7e2018-07-20 17:27:48 +000096 outs() << "\n";
97 }
98 } else {
99 for (StringRef S : Symbols) {
100 outs() << S << "\n";
Zachary Turnerc93b8702018-07-21 15:39:05 +0000101 outs().flush();
Nico Weber3bfa6a62019-04-16 12:51:40 +0000102 if (!msDemangle(S))
103 Success = false;
Zachary Turnerf435a7e2018-07-20 17:27:48 +0000104 outs() << "\n";
105 }
106 }
107
Nico Weber3bfa6a62019-04-16 12:51:40 +0000108 return Success ? 0 : 1;
Martin Storsjo21524be2018-07-20 20:48:36 +0000109}