• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
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"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cstdio>
23 #include <cstring>
24 #include <iostream>
25 #include <string>
26 
27 using namespace llvm;
28 
29 cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
30                               cl::ZeroOrMore);
31 
demangle(const std::string & S)32 static void demangle(const std::string &S) {
33   int Status;
34   char *ResultBuf = microsoftDemangle(S.c_str(), nullptr, nullptr, &Status);
35   if (Status == llvm::demangle_success) {
36     outs() << ResultBuf << "\n";
37     outs().flush();
38   } else {
39     errs() << "Error: Invalid mangled name\n";
40   }
41   std::free(ResultBuf);
42 }
43 
main(int argc,char ** argv)44 int main(int argc, char **argv) {
45   InitLLVM X(argc, argv);
46 
47   cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
48 
49   if (Symbols.empty()) {
50     while (true) {
51       std::string LineStr;
52       std::getline(std::cin, LineStr);
53       if (std::cin.eof())
54         break;
55 
56       StringRef Line(LineStr);
57       Line = Line.trim();
58       if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
59         continue;
60 
61       // If the user is manually typing in these decorated names, don't echo
62       // them to the terminal a second time.  If they're coming from redirected
63       // input, however, then we should display the input line so that the
64       // mangled and demangled name can be easily correlated in the output.
65       if (!sys::Process::StandardInIsUserInput()) {
66         outs() << Line << "\n";
67         outs().flush();
68       }
69       demangle(Line);
70       outs() << "\n";
71     }
72   } else {
73     for (StringRef S : Symbols) {
74       outs() << S << "\n";
75       outs().flush();
76       demangle(S);
77       outs() << "\n";
78     }
79   }
80 
81   return 0;
82 }
83