[lit] Add argument check: --timeout must be non-negative integer
[llvm-core.git] / tools / llvm-undname / llvm-undname.cpp
blobd673d16426fc0b91f505f45302a8e1ae98109e3e
1 //===-- llvm-undname.cpp - Microsoft ABI name undecorator
2 //------------------===//
3 //
4 // 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
7 //
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Demangle/Demangle.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/InitLLVM.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/WithColor.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <cstdio>
25 #include <cstring>
26 #include <iostream>
27 #include <string>
29 using namespace llvm;
31 cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
32 cl::desc("dump backreferences"), cl::Hidden,
33 cl::init(false));
34 cl::opt<std::string> RawFile("raw-file", cl::Optional,
35 cl::desc("for fuzzer data"), cl::Hidden);
36 cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
37 cl::ZeroOrMore);
39 static bool msDemangle(const std::string &S) {
40 int Status;
41 MSDemangleFlags Flags = MSDF_None;
42 if (DumpBackReferences)
43 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
45 char *ResultBuf =
46 microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);
47 if (Status == llvm::demangle_success) {
48 outs() << ResultBuf << "\n";
49 outs().flush();
50 } else {
51 WithColor::error() << "Invalid mangled name\n";
53 std::free(ResultBuf);
54 return Status == llvm::demangle_success;
57 int main(int argc, char **argv) {
58 InitLLVM X(argc, argv);
60 cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
62 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;
70 return msDemangle(FileOrErr->get()->getBuffer()) ? 0 : 1;
73 bool Success = true;
74 if (Symbols.empty()) {
75 while (true) {
76 std::string LineStr;
77 std::getline(std::cin, LineStr);
78 if (std::cin.eof())
79 break;
81 StringRef Line(LineStr);
82 Line = Line.trim();
83 if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
84 continue;
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.
90 if (!sys::Process::StandardInIsUserInput()) {
91 outs() << Line << "\n";
92 outs().flush();
94 if (!msDemangle(Line))
95 Success = false;
96 outs() << "\n";
98 } else {
99 for (StringRef S : Symbols) {
100 outs() << S << "\n";
101 outs().flush();
102 if (!msDemangle(S))
103 Success = false;
104 outs() << "\n";
108 return Success ? 0 : 1;