llvm.org GIT mirror llvm / 53bb7b2
Update microsoftDemangle() to work more like itaniumDemangle(). * Use same method of initializing the output stream and its buffer * Allow a nullptr Status pointer * Don't print the mangled name on demangling error * Write to N (if it is non-nullptr) Differential Revision: https://reviews.llvm.org/D52104 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@342330 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Weber 1 year, 2 days ago
3 changed file(s) with 50 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
6767 CurrentPosition = 0;
6868 Buffer = Buffer_;
6969 BufferCapacity = BufferCapacity_;
70 }
71
72 /// Create an OutputStream from a buffer and a size. If either of these are
73 /// null a buffer is allocated.
74 static OutputStream create(char *StartBuf, size_t *Size, size_t AllocSize) {
75 OutputStream Result;
76
77 if (!StartBuf || !Size) {
78 StartBuf = static_cast(std::malloc(AllocSize));
79 if (StartBuf == nullptr)
80 std::terminate();
81 Size = &AllocSize;
82 }
83
84 Result.reset(StartBuf, *Size);
85 return Result;
8670 }
8771
8872 /// If a ParameterPackExpansion (or similar type) is encountered, the offset
184168 SwapAndRestore &operator=(const SwapAndRestore &) = delete;
185169 };
186170
171 inline bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S,
172 size_t InitSize) {
173 size_t BufferSize;
174 if (Buf == nullptr) {
175 Buf = static_cast(std::malloc(InitSize));
176 if (Buf == nullptr)
177 return true;
178 BufferSize = InitSize;
179 } else
180 BufferSize = *N;
181
182 S.reset(Buf, BufferSize);
183 return false;
184 }
185
187186 #endif
309309 return Alloc.allocate(sizeof(Node *) * sz);
310310 }
311311 };
312
313 bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S,
314 size_t InitSize) {
315 size_t BufferSize;
316 if (Buf == nullptr) {
317 Buf = static_cast(std::malloc(InitSize));
318 if (Buf == nullptr)
319 return true;
320 BufferSize = InitSize;
321 } else
322 BufferSize = *N;
323
324 S.reset(Buf, BufferSize);
325 return false;
326 }
327312 } // unnamed namespace
328313
329314 //===----------------------------------------------------------------------===//
10151015 void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
10161016 // Render this class template name into a string buffer so that we can
10171017 // memorize it for the purpose of back-referencing.
1018 OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
1018 OutputStream OS;
1019 if (initializeOutputStream(nullptr, nullptr, OS, 1024))
1020 // FIXME: Propagate out-of-memory as an error?
1021 std::terminate();
10191022 Identifier->output(OS, OF_Default);
10201023 OS << '\0';
10211024 char *Name = OS.getBuffer();
13451348 if (MangledName.empty())
13461349 goto StringLiteralError;
13471350
1348 OS = OutputStream::create(nullptr, nullptr, 1024);
1351 if (initializeOutputStream(nullptr, nullptr, OS, 1024))
1352 // FIXME: Propagate out-of-memory as an error?
1353 std::terminate();
13491354 if (IsWcharT) {
13501355 Result->Char = CharKind::Wchar;
13511356 if (StringByteSize > 64)
14651470 return nullptr;
14661471
14671472 // Render the parent symbol's name into a buffer.
1468 OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
1473 OutputStream OS;
1474 if (initializeOutputStream(nullptr, nullptr, OS, 1024))
1475 // FIXME: Propagate out-of-memory as an error?
1476 std::terminate();
14691477 OS << '`';
14701478 Scope->output(OS, OF_Default);
14711479 OS << '\'';
22882296 (int)Backrefs.FunctionParamCount);
22892297
22902298 // Create an output stream so we can render each type.
2291 OutputStream OS = OutputStream::create(nullptr, 0, 1024);
2299 OutputStream OS;
2300 if (initializeOutputStream(nullptr, nullptr, OS, 1024))
2301 std::terminate();
22922302 for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
22932303 OS.setCurrentPosition(0);
22942304
23132323
23142324 char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
23152325 int *Status, MSDemangleFlags Flags) {
2326 int InternalStatus = demangle_success;
23162327 Demangler D;
2328 OutputStream S;
2329
23172330 StringView Name{MangledName};
2318 SymbolNode *S = D.parse(Name);
2331 SymbolNode *AST = D.parse(Name);
23192332
23202333 if (Flags & MSDF_DumpBackrefs)
23212334 D.dumpBackReferences();
2322 OutputStream OS = OutputStream::create(Buf, N, 1024);
2323 if (D.Error) {
2324 OS << MangledName;
2325 *Status = llvm::demangle_invalid_mangled_name;
2326 } else {
2327 S->output(OS, OF_Default);
2328 *Status = llvm::demangle_success;
2329 }
2330
2331 OS << '\0';
2332 return OS.getBuffer();
2333 }
2335
2336 if (D.Error)
2337 InternalStatus = demangle_invalid_mangled_name;
2338 else if (initializeOutputStream(Buf, N, S, 1024))
2339 InternalStatus = demangle_memory_alloc_failure;
2340 else {
2341 AST->output(S, OF_Default);
2342 S += '\0';
2343 if (N != nullptr)
2344 *N = S.getCurrentPosition();
2345 Buf = S.getBuffer();
2346 }
2347
2348 if (Status)
2349 *Status = InternalStatus;
2350 return InternalStatus == demangle_success ? Buf : nullptr;
2351 }