Skip to content

Commit 55aa909

Browse files
Keith Smileyjrose-apple
authored andcommitted
Update swift-demangle to interactively demangle (#4412)
This changes the swift-demangle interactive STDIN mode to immediately print the demangled symbol on return as opposed to printing them all at the end.
1 parent d74b97c commit 55aa909

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

tools/swift-demangle/swift-demangle.cpp

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Basic/DemangleWrappers.h"
18+
#include "llvm/ADT/SmallString.h"
1819
#include "llvm/Support/CommandLine.h"
1920
#include "llvm/Support/MemoryBuffer.h"
2021
#include "llvm/Support/PrettyStackTrace.h"
@@ -24,6 +25,11 @@
2425

2526
#include <cstdlib>
2627
#include <string>
28+
#if !defined(_MSC_VER) && !defined(__MINGW32__)
29+
#include <unistd.h>
30+
#else
31+
#include <io.h>
32+
#endif
2733

2834
static llvm::cl::opt<bool>
2935
ExpandMode("expand",
@@ -53,6 +59,16 @@ static llvm::cl::list<std::string>
5359
InputNames(llvm::cl::Positional, llvm::cl::desc("[mangled name...]"),
5460
llvm::cl::ZeroOrMore);
5561

62+
static llvm::StringRef substrBefore(llvm::StringRef whole,
63+
llvm::StringRef part) {
64+
return whole.slice(0, part.data() - whole.data());
65+
}
66+
67+
static llvm::StringRef substrAfter(llvm::StringRef whole,
68+
llvm::StringRef part) {
69+
return whole.substr((part.data() - whole.data()) + part.size());
70+
}
71+
5672
static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
5773
const swift::Demangle::DemangleOptions &options) {
5874
bool hadLeadingUnderscore = false;
@@ -86,22 +102,42 @@ static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
86102
}
87103
}
88104

89-
static llvm::StringRef substrBefore(llvm::StringRef whole,
90-
llvm::StringRef part) {
91-
return whole.slice(0, part.data() - whole.data());
92-
}
105+
static int demangleSTDIN(const swift::Demangle::DemangleOptions &options) {
106+
// This doesn't handle Unicode symbols, but maybe that's okay.
107+
llvm::Regex maybeSymbol("_T[_a-zA-Z0-9$]+");
93108

94-
static llvm::StringRef substrAfter(llvm::StringRef whole,
95-
llvm::StringRef part) {
96-
return whole.substr((part.data() - whole.data()) + part.size());
109+
while (true) {
110+
char *inputLine = NULL;
111+
size_t size;
112+
if (getline(&inputLine, &size, stdin) == -1 || size <= 0) {
113+
if (errno == 0) {
114+
break;
115+
}
116+
117+
return EXIT_FAILURE;
118+
}
119+
120+
llvm::StringRef inputContents(inputLine);
121+
llvm::SmallVector<llvm::StringRef, 1> matches;
122+
while (maybeSymbol.match(inputContents, &matches)) {
123+
llvm::outs() << substrBefore(inputContents, matches.front());
124+
demangle(llvm::outs(), matches.front(), options);
125+
inputContents = substrAfter(inputContents, matches.front());
126+
}
127+
128+
llvm::outs() << inputContents;
129+
free(inputLine);
130+
}
131+
132+
return EXIT_SUCCESS;
97133
}
98134

99135
int main(int argc, char **argv) {
100136
#if defined(__CYGWIN__)
101137
// Cygwin clang 3.5.2 with '-O3' generates CRASHING BINARY,
102138
// if main()'s first function call is passing argv[0].
103139
std::rand();
104-
#endif
140+
#endif
105141
llvm::cl::ParseCommandLineOptions(argc, argv);
106142

107143
swift::Demangle::DemangleOptions options;
@@ -111,29 +147,13 @@ int main(int argc, char **argv) {
111147

112148
if (InputNames.empty()) {
113149
CompactMode = true;
114-
auto input = llvm::MemoryBuffer::getSTDIN();
115-
if (!input) {
116-
llvm::errs() << input.getError().message() << '\n';
117-
return EXIT_FAILURE;
118-
}
119-
llvm::StringRef inputContents = input.get()->getBuffer();
120-
121-
// This doesn't handle Unicode symbols, but maybe that's okay.
122-
llvm::Regex maybeSymbol("_T[_a-zA-Z0-9$]+");
123-
llvm::SmallVector<llvm::StringRef, 1> matches;
124-
while (maybeSymbol.match(inputContents, &matches)) {
125-
llvm::outs() << substrBefore(inputContents, matches.front());
126-
demangle(llvm::outs(), matches.front(), options);
127-
inputContents = substrAfter(inputContents, matches.front());
128-
}
129-
llvm::outs() << inputContents;
130-
150+
return demangleSTDIN(options);
131151
} else {
132152
for (llvm::StringRef name : InputNames) {
133153
demangle(llvm::outs(), name, options);
134154
llvm::outs() << '\n';
135155
}
136-
}
137156

138-
return EXIT_SUCCESS;
157+
return EXIT_SUCCESS;
158+
}
139159
}

0 commit comments

Comments
 (0)