Skip to content

Commit

Permalink
[lldb] Improve RichManglingContext ergonomics (NFC)
Browse files Browse the repository at this point in the history
Have the different ::Parse.* methods return the demangled string
directly instead of having to go through ::GetBufferRef.

Differential revision: https://reviews.llvm.org/D118953
  • Loading branch information
JDevlieghere committed Feb 4, 2022
1 parent 9d1857f commit fa52788
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 89 deletions.
29 changes: 8 additions & 21 deletions lldb/include/lldb/Core/RichManglingContext.h
Expand Up @@ -43,35 +43,22 @@ class RichManglingContext {
bool IsCtorOrDtor() const;

/// Get the base name of a function. This doesn't include trailing template
/// arguments, ie "a::b<int>" gives "b". The result will overwrite the
/// internal buffer. It can be obtained via GetBufferRef().
void ParseFunctionBaseName();
/// arguments, ie "a::b<int>" gives "b".
llvm::StringRef ParseFunctionBaseName();

/// Get the context name for a function. For "a::b::c", this function returns
/// "a::b". The result will overwrite the internal buffer. It can be obtained
/// via GetBufferRef().
void ParseFunctionDeclContextName();

/// Get the entire demangled name. The result will overwrite the internal
/// buffer. It can be obtained via GetBufferRef().
void ParseFullName();

/// Obtain a StringRef to the internal buffer that holds the result of the
/// most recent ParseXy() operation. The next ParseXy() call invalidates it.
llvm::StringRef GetBufferRef() const {
assert(m_provider != None && "Initialize a provider first");
return m_buffer;
}
/// "a::b".
llvm::StringRef ParseFunctionDeclContextName();

/// Get the entire demangled name.
llvm::StringRef ParseFullName();

private:
enum InfoProvider { None, ItaniumPartialDemangler, PluginCxxLanguage };

/// Selects the rich mangling info provider.
InfoProvider m_provider = None;

/// Reference to the buffer used for results of ParseXy() operations.
llvm::StringRef m_buffer;

/// Members for ItaniumPartialDemangler
llvm::ItaniumPartialDemangler m_ipd;
/// Note: m_ipd_buf is a raw pointer due to being resized by realloc via
Expand All @@ -93,7 +80,7 @@ class RichManglingContext {
void ResetProvider(InfoProvider new_provider);

/// Uniform handling of string buffers for ItaniumPartialDemangler.
void processIPDStrResult(char *ipd_res, size_t res_len);
llvm::StringRef processIPDStrResult(char *ipd_res, size_t res_len);

/// Cast the given parser to the given type. Ideally we would have a type
/// trait to deduce \a ParserT from a given InfoProvider, but unfortunately we
Expand Down
3 changes: 1 addition & 2 deletions lldb/source/Core/Mangled.cpp
Expand Up @@ -217,8 +217,7 @@ bool Mangled::DemangleWithRichManglingInfo(
if (context.FromItaniumName(m_mangled)) {
// If we got an info, we have a name. Copy to string pool and connect the
// counterparts to accelerate later access in GetDemangledName().
context.ParseFullName();
m_demangled.SetStringWithMangledCounterpart(context.GetBufferRef(),
m_demangled.SetStringWithMangledCounterpart(context.ParseFullName(),
m_mangled);
return true;
} else {
Expand Down
46 changes: 20 additions & 26 deletions lldb/source/Core/RichManglingContext.cpp
Expand Up @@ -80,15 +80,15 @@ bool RichManglingContext::IsCtorOrDtor() const {
llvm_unreachable("Fully covered switch above!");
}

void RichManglingContext::processIPDStrResult(char *ipd_res, size_t res_size) {
llvm::StringRef RichManglingContext::processIPDStrResult(char *ipd_res,
size_t res_size) {
// Error case: Clear the buffer.
if (LLVM_UNLIKELY(ipd_res == nullptr)) {
assert(res_size == m_ipd_buf_size &&
"Failed IPD queries keep the original size in the N parameter");

m_ipd_buf[0] = '\0';
m_buffer = llvm::StringRef(m_ipd_buf, 0);
return;
return llvm::StringRef(m_ipd_buf, 0);
}

// IPD's res_size includes null terminator.
Expand All @@ -106,60 +106,54 @@ void RichManglingContext::processIPDStrResult(char *ipd_res, size_t res_size) {
}

// 99% case: Just remember the string length.
m_buffer = llvm::StringRef(m_ipd_buf, res_size - 1);
return llvm::StringRef(m_ipd_buf, res_size - 1);
}

void RichManglingContext::ParseFunctionBaseName() {
llvm::StringRef RichManglingContext::ParseFunctionBaseName() {
assert(m_provider != None && "Initialize a provider first");
switch (m_provider) {
case ItaniumPartialDemangler: {
auto n = m_ipd_buf_size;
auto buf = m_ipd.getFunctionBaseName(m_ipd_buf, &n);
processIPDStrResult(buf, n);
return;
return processIPDStrResult(buf, n);
}
case PluginCxxLanguage:
m_buffer =
get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)->GetBasename();
return;
return get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
->GetBasename();
case None:
return;
return {};
}
}

void RichManglingContext::ParseFunctionDeclContextName() {
llvm::StringRef RichManglingContext::ParseFunctionDeclContextName() {
assert(m_provider != None && "Initialize a provider first");
switch (m_provider) {
case ItaniumPartialDemangler: {
auto n = m_ipd_buf_size;
auto buf = m_ipd.getFunctionDeclContextName(m_ipd_buf, &n);
processIPDStrResult(buf, n);
return;
return processIPDStrResult(buf, n);
}
case PluginCxxLanguage:
m_buffer =
get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)->GetContext();
return;
return get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
->GetContext();
case None:
return;
return {};
}
}

void RichManglingContext::ParseFullName() {
llvm::StringRef RichManglingContext::ParseFullName() {
assert(m_provider != None && "Initialize a provider first");
switch (m_provider) {
case ItaniumPartialDemangler: {
auto n = m_ipd_buf_size;
auto buf = m_ipd.finishDemangle(m_ipd_buf, &n);
processIPDStrResult(buf, n);
return;
return processIPDStrResult(buf, n);
}
case PluginCxxLanguage:
m_buffer = get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
->GetFullName()
.GetStringRef();
return;
return get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
->GetFullName()
.GetStringRef();
case None:
return;
return {};
}
}
7 changes: 2 additions & 5 deletions lldb/source/Symbol/Symtab.cpp
Expand Up @@ -383,16 +383,13 @@ void Symtab::RegisterMangledNameEntry(
std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
RichManglingContext &rmc) {
// Only register functions that have a base name.
rmc.ParseFunctionBaseName();
llvm::StringRef base_name = rmc.GetBufferRef();
llvm::StringRef base_name = rmc.ParseFunctionBaseName();
if (base_name.empty())
return;

// The base name will be our entry's name.
NameToIndexMap::Entry entry(ConstString(base_name), value);

rmc.ParseFunctionDeclContextName();
llvm::StringRef decl_context = rmc.GetBufferRef();
llvm::StringRef decl_context = rmc.ParseFunctionDeclContextName();

// Register functions with no context.
if (decl_context.empty()) {
Expand Down
51 changes: 16 additions & 35 deletions lldb/unittests/Core/RichManglingContextTest.cpp
Expand Up @@ -18,18 +18,12 @@ using namespace lldb_private;
TEST(RichManglingContextTest, Basic) {
RichManglingContext RMC;
ConstString mangled("_ZN3foo3barEv");
EXPECT_TRUE(RMC.FromItaniumName(mangled));

EXPECT_TRUE(RMC.FromItaniumName(mangled));
EXPECT_FALSE(RMC.IsCtorOrDtor());

RMC.ParseFunctionDeclContextName();
EXPECT_EQ("foo", RMC.GetBufferRef());

RMC.ParseFunctionBaseName();
EXPECT_EQ("bar", RMC.GetBufferRef());

RMC.ParseFullName();
EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
EXPECT_EQ("foo", RMC.ParseFunctionDeclContextName());
EXPECT_EQ("bar", RMC.ParseFunctionBaseName());
EXPECT_EQ("foo::bar()", RMC.ParseFullName());
}

TEST(RichManglingContextTest, FromCxxMethodName) {
Expand All @@ -41,19 +35,12 @@ TEST(RichManglingContextTest, FromCxxMethodName) {
ConstString demangled("foo::bar()");
EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(demangled));

EXPECT_TRUE(ItaniumRMC.IsCtorOrDtor() == CxxMethodRMC.IsCtorOrDtor());

ItaniumRMC.ParseFunctionDeclContextName();
CxxMethodRMC.ParseFunctionDeclContextName();
EXPECT_TRUE(ItaniumRMC.GetBufferRef() == CxxMethodRMC.GetBufferRef());

ItaniumRMC.ParseFunctionBaseName();
CxxMethodRMC.ParseFunctionBaseName();
EXPECT_TRUE(ItaniumRMC.GetBufferRef() == CxxMethodRMC.GetBufferRef());

ItaniumRMC.ParseFullName();
CxxMethodRMC.ParseFullName();
EXPECT_TRUE(ItaniumRMC.GetBufferRef() == CxxMethodRMC.GetBufferRef());
EXPECT_EQ(ItaniumRMC.IsCtorOrDtor(), CxxMethodRMC.IsCtorOrDtor());
EXPECT_EQ(ItaniumRMC.ParseFunctionDeclContextName(),
CxxMethodRMC.ParseFunctionDeclContextName());
EXPECT_EQ(ItaniumRMC.ParseFunctionBaseName(),
CxxMethodRMC.ParseFunctionBaseName());
EXPECT_EQ(ItaniumRMC.ParseFullName(), CxxMethodRMC.ParseFullName());

// Construct with a random name.
{
Expand All @@ -68,8 +55,7 @@ TEST(RichManglingContextTest, FromCxxMethodName) {
ConstString("void * operator new(unsigned __int64)")));

// We expect its context is empty.
CxxMethodRMC.ParseFunctionDeclContextName();
EXPECT_TRUE(CxxMethodRMC.GetBufferRef().empty());
EXPECT_TRUE(CxxMethodRMC.ParseFunctionDeclContextName().empty());
}
}

Expand All @@ -79,16 +65,13 @@ TEST(RichManglingContextTest, SwitchProvider) {
llvm::StringRef demangled = "foo::bar()";

EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
RMC.ParseFullName();
EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
EXPECT_EQ("foo::bar()", RMC.ParseFullName());

EXPECT_TRUE(RMC.FromCxxMethodName(ConstString(demangled)));
RMC.ParseFullName();
EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
EXPECT_EQ("foo::bar()", RMC.ParseFullName());

EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
RMC.ParseFullName();
EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
EXPECT_EQ("foo::bar()", RMC.ParseFullName());
}

TEST(RichManglingContextTest, IPDRealloc) {
Expand Down Expand Up @@ -116,13 +99,11 @@ TEST(RichManglingContextTest, IPDRealloc) {

// Demangle the short one.
EXPECT_TRUE(RMC.FromItaniumName(ConstString(ShortMangled)));
RMC.ParseFullName();
const char *ShortDemangled = RMC.GetBufferRef().data();
const char *ShortDemangled = RMC.ParseFullName().data();

// Demangle the long one.
EXPECT_TRUE(RMC.FromItaniumName(ConstString(LongMangled)));
RMC.ParseFullName();
const char *LongDemangled = RMC.GetBufferRef().data();
const char *LongDemangled = RMC.ParseFullName().data();

// Make sure a new buffer was allocated or the default buffer was extended.
bool AllocatedNewBuffer = (ShortDemangled != LongDemangled);
Expand Down

0 comments on commit fa52788

Please sign in to comment.