Skip to content

[cherry-pick][stable/20220421] [lldb][Breakpoint] Fix setting breakpoints on templates by basename #5459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,21 @@ std::string CPlusPlusLanguage::MethodName::GetScopeQualifiedName() {
return res;
}

llvm::StringRef
CPlusPlusLanguage::MethodName::GetBasenameNoTemplateParameters() {
llvm::StringRef basename = GetBasename();
size_t arg_start, arg_end;
llvm::StringRef parens("<>", 2);
if (ReverseFindMatchingChars(basename, parens, arg_start, arg_end))
return basename.substr(0, arg_start);

return basename;
}

bool CPlusPlusLanguage::MethodName::ContainsPath(llvm::StringRef path) {
if (!m_parsed)
Parse();

// If we can't parse the incoming name, then just check that it contains path.
if (m_parse_error)
return m_full.GetStringRef().contains(path);
Expand All @@ -285,8 +297,23 @@ bool CPlusPlusLanguage::MethodName::ContainsPath(llvm::StringRef path) {
if (!success)
return m_full.GetStringRef().contains(path);

if (identifier != GetBasename())
// Basename may include template arguments.
// E.g.,
// GetBaseName(): func<int>
// identifier : func
//
// ...but we still want to account for identifiers with template parameter
// lists, e.g., when users set breakpoints on template specializations.
//
// E.g.,
// GetBaseName(): func<uint32_t>
// identifier : func<int32_t*>
//
// Try to match the basename with or without template parameters.
if (GetBasename() != identifier &&
GetBasenameNoTemplateParameters() != identifier)
return false;

// Incoming path only had an identifier, so we match.
if (context.empty())
return true;
Expand Down
15 changes: 15 additions & 0 deletions lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ class CPlusPlusLanguage : public Language {

bool ContainsPath(llvm::StringRef path);

private:
/// Returns the Basename of this method without a template parameter
/// list, if any.
///
// Examples:
//
// +--------------------------------+---------+
// | MethodName | Returns |
// +--------------------------------+---------+
// | void func() | func |
// | void func<int>() | func |
// | void func<std::vector<int>>() | func |
// +--------------------------------+---------+
llvm::StringRef GetBasenameNoTemplateParameters();

protected:
void Parse();
bool TrySimplifiedParse();
Expand Down
25 changes: 24 additions & 1 deletion lldb/unittests/Language/CPlusPlus/CPlusPlusLanguageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ TEST(CPlusPlusLanguage, ContainsPath) {
CPlusPlusLanguage::MethodName reference_3(ConstString("int func01()"));
CPlusPlusLanguage::MethodName
reference_4(ConstString("bar::baz::operator bool()"));

CPlusPlusLanguage::MethodName reference_5(
ConstString("bar::baz::operator bool<int, Type<double>>()"));
CPlusPlusLanguage::MethodName reference_6(ConstString(
"bar::baz::operator<<<Type<double>, Type<std::vector<double>>>()"));

EXPECT_TRUE(reference_1.ContainsPath(""));
EXPECT_TRUE(reference_1.ContainsPath("func01"));
EXPECT_TRUE(reference_1.ContainsPath("bar::func01"));
EXPECT_TRUE(reference_1.ContainsPath("foo::bar::func01"));
Expand All @@ -153,17 +158,35 @@ TEST(CPlusPlusLanguage, ContainsPath) {
EXPECT_FALSE(reference_1.ContainsPath("::foo::baz::func01"));
EXPECT_FALSE(reference_1.ContainsPath("foo::bar::baz::func01"));

EXPECT_TRUE(reference_2.ContainsPath(""));
EXPECT_TRUE(reference_2.ContainsPath("foofoo::bar::func01"));
EXPECT_FALSE(reference_2.ContainsPath("foo::bar::func01"));

EXPECT_TRUE(reference_3.ContainsPath(""));
EXPECT_TRUE(reference_3.ContainsPath("func01"));
EXPECT_FALSE(reference_3.ContainsPath("func"));
EXPECT_FALSE(reference_3.ContainsPath("bar::func01"));

EXPECT_TRUE(reference_4.ContainsPath(""));
EXPECT_TRUE(reference_4.ContainsPath("operator"));
EXPECT_TRUE(reference_4.ContainsPath("operator bool"));
EXPECT_TRUE(reference_4.ContainsPath("baz::operator bool"));
EXPECT_TRUE(reference_4.ContainsPath("bar::baz::operator bool"));
EXPECT_FALSE(reference_4.ContainsPath("az::operator bool"));

EXPECT_TRUE(reference_5.ContainsPath(""));
EXPECT_TRUE(reference_5.ContainsPath("operator"));
EXPECT_TRUE(reference_5.ContainsPath("operator bool"));
EXPECT_TRUE(reference_5.ContainsPath("operator bool<int, Type<double>>"));
EXPECT_FALSE(reference_5.ContainsPath("operator bool<int, double>"));
EXPECT_FALSE(reference_5.ContainsPath("operator bool<int, Type<int>>"));

EXPECT_TRUE(reference_6.ContainsPath(""));
EXPECT_TRUE(reference_6.ContainsPath("operator"));
EXPECT_TRUE(reference_6.ContainsPath("operator<<"));
EXPECT_TRUE(reference_6.ContainsPath(
"bar::baz::operator<<<Type<double>, Type<std::vector<double>>>()"));
EXPECT_FALSE(reference_6.ContainsPath("operator<<<Type<double>>"));
}

TEST(CPlusPlusLanguage, ExtractContextAndIdentifier) {
Expand Down