Skip to content

[lldb] Support case-insensitive regex matches #95350

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
merged 2 commits into from
Jun 13, 2024
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
8 changes: 7 additions & 1 deletion lldb/include/lldb/Utility/RegularExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ class RegularExpression {
/// \param[in] string
/// An llvm::StringRef that represents the regular expression to compile.
// String is not referenced anymore after the object is constructed.
explicit RegularExpression(llvm::StringRef string);
//
/// \param[in] flags
/// An llvm::Regex::RegexFlags that modifies the matching behavior. The
/// default is NoFlags.
explicit RegularExpression(
llvm::StringRef string,
llvm::Regex::RegexFlags flags = llvm::Regex::NoFlags);

~RegularExpression() = default;

Expand Down
7 changes: 6 additions & 1 deletion lldb/include/lldb/lldb-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,12 @@ enum MemberFunctionKind {
};

/// String matching algorithm used by SBTarget.
enum MatchType { eMatchTypeNormal, eMatchTypeRegex, eMatchTypeStartsWith };
enum MatchType {
eMatchTypeNormal,
eMatchTypeRegex,
eMatchTypeStartsWith,
eMatchTypeRegexInsensitive
};

/// Bitmask that describes details about a type.
FLAGS_ENUM(TypeFlags){
Expand Down
10 changes: 10 additions & 0 deletions lldb/source/API/SBTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,11 @@ lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
target_sp->GetImages().FindFunctions(RegularExpression(name_ref),
function_options, *sb_sc_list);
break;
case eMatchTypeRegexInsensitive:
target_sp->GetImages().FindFunctions(
RegularExpression(name_ref, llvm::Regex::RegexFlags::IgnoreCase),
function_options, *sb_sc_list);
break;
case eMatchTypeStartsWith:
regexstr = llvm::Regex::escape(name) + ".*";
target_sp->GetImages().FindFunctions(RegularExpression(regexstr),
Expand Down Expand Up @@ -1936,6 +1941,11 @@ SBValueList SBTarget::FindGlobalVariables(const char *name,
target_sp->GetImages().FindGlobalVariables(RegularExpression(name_ref),
max_matches, variable_list);
break;
case eMatchTypeRegexInsensitive:
target_sp->GetImages().FindGlobalVariables(
RegularExpression(name_ref, llvm::Regex::IgnoreCase), max_matches,
variable_list);
break;
case eMatchTypeStartsWith:
regexstr = "^" + llvm::Regex::escape(name) + ".*";
target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr),
Expand Down
5 changes: 3 additions & 2 deletions lldb/source/Utility/RegularExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

using namespace lldb_private;

RegularExpression::RegularExpression(llvm::StringRef str)
RegularExpression::RegularExpression(llvm::StringRef str,
llvm::Regex::RegexFlags flags)
: m_regex_text(std::string(str)),
// m_regex does not reference str anymore after it is constructed.
m_regex(llvm::Regex(str)) {}
m_regex(llvm::Regex(str, flags)) {}

RegularExpression::RegularExpression(const RegularExpression &rhs)
: RegularExpression(rhs.GetText()) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test display and Python APIs on file and class static variables.
"""


import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
Expand Down Expand Up @@ -263,6 +262,12 @@ def test_with_python_FindGlobalVariables(self):
self.assertTrue(found_a, "Regex search found A::g_points")
self.assertTrue(found_aa, "Regex search found AA::g_points")

# Regex lowercase should find both as well.
val_list = target.FindGlobalVariables(
"a::g_points", 10, lldb.eMatchTypeRegexInsensitive
)
self.assertEqual(val_list.GetSize(), 2, "Found A & AA")

# Normal search for full name should find one, but it looks like we don't match
# on identifier boundaries here yet:
val_list = target.FindGlobalVariables("A::g_points", 10, lldb.eMatchTypeNormal)
Expand Down
Loading