diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp index ea336378bebb3..4b80a44746127 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp @@ -1138,5 +1138,8 @@ std::optional llvm::StripTemplateParameters(StringRef Name) { while (NumLeftAnglesToSkip--) StartOfTemplate = Name.find('<', StartOfTemplate) + 1; - return Name.substr(0, StartOfTemplate - 1); + StringRef Result = Name.substr(0, StartOfTemplate - 1); + if (Result.empty()) + return std::nullopt; + return Result; } diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFAcceleratorTableTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFAcceleratorTableTest.cpp index dedcf816cf63f..82cc02c921d15 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFAcceleratorTableTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFAcceleratorTableTest.cpp @@ -299,4 +299,18 @@ TEST(DWARFDebugNames, UnsupportedForm) { Sections, FailedWithMessage("unsupported Form for YAML debug_names emitter")); } + +TEST(DWARFDebugNames, TestStripTemplateParameters) { + + std::optional stripped_name; + // Make sure we can extract the name "foo" from the template parameters. + stripped_name = StripTemplateParameters("foo"); + ASSERT_TRUE(stripped_name.has_value()); + ASSERT_EQ(*stripped_name, StringRef("foo")); + // Make sure that we don't get a valid name back when the string starts with + // '<'. + stripped_name = StripTemplateParameters(""); + ASSERT_FALSE(stripped_name.has_value()); +} + } // end anonymous namespace