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"
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
2834static llvm::cl::opt<bool >
2935ExpandMode (" expand" ,
@@ -53,6 +59,16 @@ static llvm::cl::list<std::string>
5359InputNames (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+
5672static 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
99135int 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