Skip to content

Commit

Permalink
[Demangle] Add support for multiple identifiers in D qualified names
Browse files Browse the repository at this point in the history
Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D114305
  • Loading branch information
dwblaikie committed Nov 30, 2021
1 parent e63c799 commit 6e08abd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
30 changes: 28 additions & 2 deletions llvm/lib/Demangle/DLangDemangle.cpp
Expand Up @@ -69,6 +69,15 @@ struct Demangler {
/// \see https://dlang.org/spec/abi.html#Number .
const char *decodeNumber(const char *Mangled, unsigned long *Ret);

/// Check whether it is the beginning of a symbol name.
///
/// \param Mangled string to extract the symbol name.
///
/// \return true on success, false otherwise.
///
/// \see https://dlang.org/spec/abi.html#SymbolName .
bool isSymbolName(const char *Mangled);

/// Extract and demangle an identifier from a given mangled symbol append it
/// to the output string.
///
Expand Down Expand Up @@ -136,6 +145,14 @@ const char *Demangler::decodeNumber(const char *Mangled, unsigned long *Ret) {
return Mangled;
}

bool Demangler::isSymbolName(const char *Mangled) {
if (std::isdigit(*Mangled))
return true;

// TODO: Handle symbol back references and template instances.
return false;
}

const char *Demangler::parseMangle(OutputBuffer *Demangled,
const char *Mangled) {
// A D mangled symbol is comprised of both scope and type information.
Expand Down Expand Up @@ -180,9 +197,18 @@ const char *Demangler::parseQualified(OutputBuffer *Demangled,
// SymbolName M TypeModifiers TypeFunctionNoReturn
// The start pointer should be at the above location.

// TODO: Parse multiple identifiers
// Whether it has more than one symbol
size_t NotFirst = false;
do {
if (NotFirst)
*Demangled << '.';
NotFirst = true;

Mangled = parseIdentifier(Demangled, Mangled);

} while (Mangled && isSymbolName(Mangled));

return parseIdentifier(Demangled, Mangled);
return Mangled;
}

const char *Demangler::parseIdentifier(OutputBuffer *Demangled,
Expand Down
17 changes: 9 additions & 8 deletions llvm/unittests/Demangle/DLangDemangleTest.cpp
Expand Up @@ -26,11 +26,12 @@ TEST_P(DLangDemangleTestFixture, DLangDemangleTest) {
EXPECT_STREQ(Demangled, GetParam().second);
}

INSTANTIATE_TEST_SUITE_P(DLangDemangleTest, DLangDemangleTestFixture,
testing::Values(std::make_pair("_Dmain", "D main"),
std::make_pair(nullptr, nullptr),
std::make_pair("_Z", nullptr),
std::make_pair("_DDD", nullptr),
std::make_pair("_D88", nullptr),
std::make_pair("_D8demangleZ",
"demangle")));
INSTANTIATE_TEST_SUITE_P(
DLangDemangleTest, DLangDemangleTestFixture,
testing::Values(
std::make_pair("_Dmain", "D main"), std::make_pair(nullptr, nullptr),
std::make_pair("_Z", nullptr), std::make_pair("_DDD", nullptr),
std::make_pair("_D88", nullptr),
std::make_pair("_D8demangleZ", "demangle"),
std::make_pair("_D8demangle4testZ", "demangle.test"),
std::make_pair("_D8demangle4test5test2Z", "demangle.test.test2")));

0 comments on commit 6e08abd

Please sign in to comment.