https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/140762
>From cc3c6d1c86ae0ed579c4f325778ee1b4cd90d6be Mon Sep 17 00:00:00 2001 From: Charles Zablit <c_zab...@apple.com> Date: Tue, 20 May 2025 17:45:20 +0100 Subject: [PATCH 1/2] refactor demangler range tracking --- lldb/include/lldb/Core/DemangledNameInfo.h | 18 +++++++++++++++++- lldb/include/lldb/Core/FormatEntity.h | 1 + lldb/source/Core/FormatEntity.cpp | 3 +++ lldb/source/Core/Mangled.cpp | 2 ++ .../Language/CPlusPlus/CPlusPlusLanguage.cpp | 4 ++-- lldb/unittests/Core/CMakeLists.txt | 2 +- ...{MangledTest.cpp => ItaniumMangledTest.cpp} | 13 +++++++------ 7 files changed, 33 insertions(+), 10 deletions(-) rename lldb/unittests/Core/{MangledTest.cpp => ItaniumMangledTest.cpp} (98%) diff --git a/lldb/include/lldb/Core/DemangledNameInfo.h b/lldb/include/lldb/Core/DemangledNameInfo.h index 11d3bb58871b8..76cf8908fcbe6 100644 --- a/lldb/include/lldb/Core/DemangledNameInfo.h +++ b/lldb/include/lldb/Core/DemangledNameInfo.h @@ -39,7 +39,7 @@ struct DemangledNameInfo { /// \endcode std::pair<size_t, size_t> ScopeRange; - /// Indicates the [start, end) of the function argument lits. + /// Indicates the [start, end) of the function argument list. /// E.g., /// \code{.cpp} /// int (*getFunc<float>(float, double))(int, int) @@ -59,11 +59,27 @@ struct DemangledNameInfo { /// \endcode std::pair<size_t, size_t> QualifiersRange; + /// Indicates the [start, end) of the function's prefix. This is a + /// catch-all range for anything that is not tracked by the rest of + /// the pairs. + std::pair<size_t, size_t> PrefixRange; + + /// Indicates the [start, end) of the function's suffix. This is a + /// catch-all range for anything that is not tracked by the rest of + /// the pairs. + std::pair<size_t, size_t> SuffixRange; + /// Returns \c true if this object holds a valid basename range. bool hasBasename() const { return BasenameRange.second > BasenameRange.first && BasenameRange.second > 0; } + + /// Returns \c true if this object holds a valid arguments range. + bool hasArguments() const { + return ArgumentsRange.second > ArgumentsRange.first && + ArgumentsRange.second > 0; + } }; /// An OutputBuffer which keeps a record of where certain parts of a diff --git a/lldb/include/lldb/Core/FormatEntity.h b/lldb/include/lldb/Core/FormatEntity.h index 6acf6fbe43239..1aed3c6ff9e9d 100644 --- a/lldb/include/lldb/Core/FormatEntity.h +++ b/lldb/include/lldb/Core/FormatEntity.h @@ -88,6 +88,7 @@ struct Entry { FunctionNameWithArgs, FunctionNameNoArgs, FunctionMangledName, + FunctionPrefix, FunctionScope, FunctionBasename, FunctionTemplateArguments, diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp index 4f2d39873c7fb..4dcfa43a7bb04 100644 --- a/lldb/source/Core/FormatEntity.cpp +++ b/lldb/source/Core/FormatEntity.cpp @@ -124,6 +124,7 @@ constexpr Definition g_function_child_entries[] = { Definition("initial-function", EntryType::FunctionInitial), Definition("changed", EntryType::FunctionChanged), Definition("is-optimized", EntryType::FunctionIsOptimized), + Definition("prefix", EntryType::FunctionPrefix), Definition("scope", EntryType::FunctionScope), Definition("basename", EntryType::FunctionBasename), Definition("template-arguments", EntryType::FunctionTemplateArguments), @@ -385,6 +386,7 @@ const char *FormatEntity::Entry::TypeToCString(Type t) { ENUM_TO_CSTR(FunctionNameWithArgs); ENUM_TO_CSTR(FunctionNameNoArgs); ENUM_TO_CSTR(FunctionMangledName); + ENUM_TO_CSTR(FunctionPrefix); ENUM_TO_CSTR(FunctionScope); ENUM_TO_CSTR(FunctionBasename); ENUM_TO_CSTR(FunctionTemplateArguments); @@ -1835,6 +1837,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s, return true; } + case Entry::Type::FunctionPrefix: case Entry::Type::FunctionScope: case Entry::Type::FunctionBasename: case Entry::Type::FunctionTemplateArguments: diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp index ce4db4e0daa8b..e6f7d198d7316 100644 --- a/lldb/source/Core/Mangled.cpp +++ b/lldb/source/Core/Mangled.cpp @@ -172,6 +172,8 @@ GetItaniumDemangledStr(const char *M) { TrackingOutputBuffer OB(demangled_cstr, demangled_size); demangled_cstr = ipd.finishDemangle(&OB); + OB.NameInfo.SuffixRange.first = OB.NameInfo.QualifiersRange.second; + OB.NameInfo.SuffixRange.second = std::string(demangled_cstr).length(); info = std::move(OB.NameInfo); assert(demangled_cstr && diff --git a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp index 542f13bef23e7..f45b4fb816b3b 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp @@ -401,8 +401,8 @@ GetDemangledFunctionSuffix(const SymbolContext &sc) { if (!info->hasBasename()) return std::nullopt; - return demangled_name.slice(info->QualifiersRange.second, - llvm::StringRef::npos); + return demangled_name.slice(info->SuffixRange.first, + info->SuffixRange.second); } static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) { diff --git a/lldb/unittests/Core/CMakeLists.txt b/lldb/unittests/Core/CMakeLists.txt index 97268b3dbf8f8..234eb43b95257 100644 --- a/lldb/unittests/Core/CMakeLists.txt +++ b/lldb/unittests/Core/CMakeLists.txt @@ -6,7 +6,7 @@ add_lldb_unittest(LLDBCoreTests DumpDataExtractorTest.cpp DumpRegisterInfoTest.cpp FormatEntityTest.cpp - MangledTest.cpp + ItaniumMangledTest.cpp ModuleSpecTest.cpp PluginManagerTest.cpp ProgressReportTest.cpp diff --git a/lldb/unittests/Core/MangledTest.cpp b/lldb/unittests/Core/ItaniumMangledTest.cpp similarity index 98% rename from lldb/unittests/Core/MangledTest.cpp rename to lldb/unittests/Core/ItaniumMangledTest.cpp index 44651eb94c23b..fef2b14af0948 100644 --- a/lldb/unittests/Core/MangledTest.cpp +++ b/lldb/unittests/Core/ItaniumMangledTest.cpp @@ -1,4 +1,4 @@ -//===-- MangledTest.cpp ---------------------------------------------------===// +//===-- ItaniumMangledTest.cpp --------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -401,7 +401,7 @@ TEST(MangledTest, DemangledNameInfo_SetValue) { EXPECT_FALSE(mangled.GetDemangledInfo()->hasBasename()); } -struct DemanglingPartsTestCase { +struct ItaniumDemanglingPartsTestCase { const char *mangled; DemangledNameInfo expected_info; std::string_view basename; @@ -410,7 +410,7 @@ struct DemanglingPartsTestCase { bool valid_basename = true; }; -DemanglingPartsTestCase g_demangling_parts_test_cases[] = { +ItaniumDemanglingPartsTestCase g_demangling_itanium_parts_test_cases[] = { // clang-format off { "_ZNVKO3BarIN2ns3QuxIiEEE1CIPFi3FooIS_IiES6_EEE6methodIS6_EENS5_IT_SC_E5InnerIiEESD_SD_", { /*.BasenameRange=*/{92, 98}, /*.ScopeRange=*/{36, 92}, /*.ArgumentsRange=*/{ 108, 158 }, @@ -555,7 +555,7 @@ DemanglingPartsTestCase g_demangling_parts_test_cases[] = { }; struct DemanglingPartsTestFixture - : public ::testing::TestWithParam<DemanglingPartsTestCase> {}; + : public ::testing::TestWithParam<ItaniumDemanglingPartsTestCase> {}; namespace { class TestAllocator { @@ -608,5 +608,6 @@ TEST_P(DemanglingPartsTestFixture, DemanglingParts) { std::free(OB.getBuffer()); } -INSTANTIATE_TEST_SUITE_P(DemanglingPartsTests, DemanglingPartsTestFixture, - ::testing::ValuesIn(g_demangling_parts_test_cases)); +INSTANTIATE_TEST_SUITE_P( + DemanglingPartsTests, DemanglingPartsTestFixture, + ::testing::ValuesIn(g_demangling_itanium_parts_test_cases)); >From 296a105fa6eb6dbf19792229c349427330b50f0a Mon Sep 17 00:00:00 2001 From: Charles Zablit <c_zab...@apple.com> Date: Tue, 20 May 2025 17:50:48 +0100 Subject: [PATCH 2/2] remove redundant check --- lldb/include/lldb/Core/DemangledNameInfo.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lldb/include/lldb/Core/DemangledNameInfo.h b/lldb/include/lldb/Core/DemangledNameInfo.h index 76cf8908fcbe6..cda0cb6ff3dad 100644 --- a/lldb/include/lldb/Core/DemangledNameInfo.h +++ b/lldb/include/lldb/Core/DemangledNameInfo.h @@ -71,14 +71,12 @@ struct DemangledNameInfo { /// Returns \c true if this object holds a valid basename range. bool hasBasename() const { - return BasenameRange.second > BasenameRange.first && - BasenameRange.second > 0; + return BasenameRange.second > BasenameRange.first; } /// Returns \c true if this object holds a valid arguments range. bool hasArguments() const { - return ArgumentsRange.second > ArgumentsRange.first && - ArgumentsRange.second > 0; + return ArgumentsRange.second > ArgumentsRange.first; } }; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits