[llvm-objcopy] [COFF] Fix warnings abuilt missing field initialization. NFC.
[llvm-complete.git] / tools / llvm-undname / llvm-undname.cpp
blob60520c8f7be0c521bb4764432807a423e24e6568
1 //===-- 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 //===----------------------------------------------------------------------===//
11 // This utility works like the windows undname utility. It converts mangled
12 // Microsoft symbol names into pretty C/C++ human-readable names.
14 //===----------------------------------------------------------------------===//
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"
21 #include "llvm/Support/WithColor.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <cstdio>
24 #include <cstring>
25 #include <iostream>
26 #include <string>
28 using namespace llvm;
30 cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
31 cl::desc("dump backreferences"), cl::Hidden,
32 cl::init(false));
33 cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
34 cl::ZeroOrMore);
36 static void demangle(const std::string &S) {
37 int Status;
38 MSDemangleFlags Flags = MSDF_None;
39 if (DumpBackReferences)
40 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
42 char *ResultBuf =
43 microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);
44 if (Status == llvm::demangle_success) {
45 outs() << ResultBuf << "\n";
46 outs().flush();
47 } else {
48 WithColor::error() << "Invalid mangled name\n";
50 std::free(ResultBuf);
53 int main(int argc, char **argv) {
54 InitLLVM X(argc, argv);
56 cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
58 if (Symbols.empty()) {
59 while (true) {
60 std::string LineStr;
61 std::getline(std::cin, LineStr);
62 if (std::cin.eof())
63 break;
65 StringRef Line(LineStr);
66 Line = Line.trim();
67 if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
68 continue;
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.
74 if (!sys::Process::StandardInIsUserInput()) {
75 outs() << Line << "\n";
76 outs().flush();
78 demangle(Line);
79 outs() << "\n";
81 } else {
82 for (StringRef S : Symbols) {
83 outs() << S << "\n";
84 outs().flush();
85 demangle(S);
86 outs() << "\n";
90 return 0;