forked from OSchip/llvm-project
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 llvm-svn: 342330
This commit is contained in:
parent
1641556593
commit
1359d654e3
|
@ -70,22 +70,6 @@ public:
|
||||||
BufferCapacity = BufferCapacity_;
|
BufferCapacity = BufferCapacity_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an OutputStream from a buffer and a size. If either of these are
|
|
||||||
/// null a buffer is allocated.
|
|
||||||
static OutputStream create(char *StartBuf, size_t *Size, size_t AllocSize) {
|
|
||||||
OutputStream Result;
|
|
||||||
|
|
||||||
if (!StartBuf || !Size) {
|
|
||||||
StartBuf = static_cast<char *>(std::malloc(AllocSize));
|
|
||||||
if (StartBuf == nullptr)
|
|
||||||
std::terminate();
|
|
||||||
Size = &AllocSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
Result.reset(StartBuf, *Size);
|
|
||||||
return Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
|
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
|
||||||
/// into the pack that we're currently printing.
|
/// into the pack that we're currently printing.
|
||||||
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
|
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
|
||||||
|
@ -185,4 +169,19 @@ public:
|
||||||
SwapAndRestore &operator=(const SwapAndRestore &) = delete;
|
SwapAndRestore &operator=(const SwapAndRestore &) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S,
|
||||||
|
size_t InitSize) {
|
||||||
|
size_t BufferSize;
|
||||||
|
if (Buf == nullptr) {
|
||||||
|
Buf = static_cast<char *>(std::malloc(InitSize));
|
||||||
|
if (Buf == nullptr)
|
||||||
|
return true;
|
||||||
|
BufferSize = InitSize;
|
||||||
|
} else
|
||||||
|
BufferSize = *N;
|
||||||
|
|
||||||
|
S.reset(Buf, BufferSize);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -310,21 +310,6 @@ public:
|
||||||
return Alloc.allocate(sizeof(Node *) * sz);
|
return Alloc.allocate(sizeof(Node *) * sz);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S,
|
|
||||||
size_t InitSize) {
|
|
||||||
size_t BufferSize;
|
|
||||||
if (Buf == nullptr) {
|
|
||||||
Buf = static_cast<char *>(std::malloc(InitSize));
|
|
||||||
if (Buf == nullptr)
|
|
||||||
return true;
|
|
||||||
BufferSize = InitSize;
|
|
||||||
} else
|
|
||||||
BufferSize = *N;
|
|
||||||
|
|
||||||
S.reset(Buf, BufferSize);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
|
@ -1016,7 +1016,10 @@ NamedIdentifierNode *Demangler::demangleBackRefName(StringView &MangledName) {
|
||||||
void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
|
void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
|
||||||
// Render this class template name into a string buffer so that we can
|
// Render this class template name into a string buffer so that we can
|
||||||
// memorize it for the purpose of back-referencing.
|
// memorize it for the purpose of back-referencing.
|
||||||
OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
|
OutputStream OS;
|
||||||
|
if (initializeOutputStream(nullptr, nullptr, OS, 1024))
|
||||||
|
// FIXME: Propagate out-of-memory as an error?
|
||||||
|
std::terminate();
|
||||||
Identifier->output(OS, OF_Default);
|
Identifier->output(OS, OF_Default);
|
||||||
OS << '\0';
|
OS << '\0';
|
||||||
char *Name = OS.getBuffer();
|
char *Name = OS.getBuffer();
|
||||||
|
@ -1346,7 +1349,9 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
|
||||||
if (MangledName.empty())
|
if (MangledName.empty())
|
||||||
goto StringLiteralError;
|
goto StringLiteralError;
|
||||||
|
|
||||||
OS = OutputStream::create(nullptr, nullptr, 1024);
|
if (initializeOutputStream(nullptr, nullptr, OS, 1024))
|
||||||
|
// FIXME: Propagate out-of-memory as an error?
|
||||||
|
std::terminate();
|
||||||
if (IsWcharT) {
|
if (IsWcharT) {
|
||||||
Result->Char = CharKind::Wchar;
|
Result->Char = CharKind::Wchar;
|
||||||
if (StringByteSize > 64)
|
if (StringByteSize > 64)
|
||||||
|
@ -1466,7 +1471,10 @@ Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
// Render the parent symbol's name into a buffer.
|
// Render the parent symbol's name into a buffer.
|
||||||
OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
|
OutputStream OS;
|
||||||
|
if (initializeOutputStream(nullptr, nullptr, OS, 1024))
|
||||||
|
// FIXME: Propagate out-of-memory as an error?
|
||||||
|
std::terminate();
|
||||||
OS << '`';
|
OS << '`';
|
||||||
Scope->output(OS, OF_Default);
|
Scope->output(OS, OF_Default);
|
||||||
OS << '\'';
|
OS << '\'';
|
||||||
|
@ -2289,7 +2297,9 @@ void Demangler::dumpBackReferences() {
|
||||||
(int)Backrefs.FunctionParamCount);
|
(int)Backrefs.FunctionParamCount);
|
||||||
|
|
||||||
// Create an output stream so we can render each type.
|
// Create an output stream so we can render each type.
|
||||||
OutputStream OS = OutputStream::create(nullptr, 0, 1024);
|
OutputStream OS;
|
||||||
|
if (initializeOutputStream(nullptr, nullptr, OS, 1024))
|
||||||
|
std::terminate();
|
||||||
for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
|
for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
|
||||||
OS.setCurrentPosition(0);
|
OS.setCurrentPosition(0);
|
||||||
|
|
||||||
|
@ -2314,21 +2324,29 @@ void Demangler::dumpBackReferences() {
|
||||||
|
|
||||||
char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
|
char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
|
||||||
int *Status, MSDemangleFlags Flags) {
|
int *Status, MSDemangleFlags Flags) {
|
||||||
|
int InternalStatus = demangle_success;
|
||||||
Demangler D;
|
Demangler D;
|
||||||
|
OutputStream S;
|
||||||
|
|
||||||
StringView Name{MangledName};
|
StringView Name{MangledName};
|
||||||
SymbolNode *S = D.parse(Name);
|
SymbolNode *AST = D.parse(Name);
|
||||||
|
|
||||||
if (Flags & MSDF_DumpBackrefs)
|
if (Flags & MSDF_DumpBackrefs)
|
||||||
D.dumpBackReferences();
|
D.dumpBackReferences();
|
||||||
OutputStream OS = OutputStream::create(Buf, N, 1024);
|
|
||||||
if (D.Error) {
|
if (D.Error)
|
||||||
OS << MangledName;
|
InternalStatus = demangle_invalid_mangled_name;
|
||||||
*Status = llvm::demangle_invalid_mangled_name;
|
else if (initializeOutputStream(Buf, N, S, 1024))
|
||||||
} else {
|
InternalStatus = demangle_memory_alloc_failure;
|
||||||
S->output(OS, OF_Default);
|
else {
|
||||||
*Status = llvm::demangle_success;
|
AST->output(S, OF_Default);
|
||||||
|
S += '\0';
|
||||||
|
if (N != nullptr)
|
||||||
|
*N = S.getCurrentPosition();
|
||||||
|
Buf = S.getBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
OS << '\0';
|
if (Status)
|
||||||
return OS.getBuffer();
|
*Status = InternalStatus;
|
||||||
|
return InternalStatus == demangle_success ? Buf : nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue