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

Conversation

JDevlieghere
Copy link
Member

Support case-insensitive regex matches for SBTarget::FindGlobalFunctions and SBTarget::FindGlobalVariables.

Support case-insensitive regex matches for SBTarget::FindGlobalFunctions
and SBTarget::FindGlobalVariables.
@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2024

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Support case-insensitive regex matches for SBTarget::FindGlobalFunctions and SBTarget::FindGlobalVariables.


Full diff: https://github.com/llvm/llvm-project/pull/95350.diff

5 Files Affected:

  • (modified) lldb/include/lldb/Utility/RegularExpression.h (+7-1)
  • (modified) lldb/include/lldb/lldb-enumerations.h (+6-1)
  • (modified) lldb/source/API/SBTarget.cpp (+10)
  • (modified) lldb/source/Utility/RegularExpression.cpp (+3-2)
  • (modified) lldb/test/API/lang/cpp/class_static/TestStaticVariables.py (+4)
diff --git a/lldb/include/lldb/Utility/RegularExpression.h b/lldb/include/lldb/Utility/RegularExpression.h
index 415f1b58b1110..e8bd47bb53c29 100644
--- a/lldb/include/lldb/Utility/RegularExpression.h
+++ b/lldb/include/lldb/Utility/RegularExpression.h
@@ -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;
 
diff --git a/lldb/include/lldb/lldb-enumerations.h b/lldb/include/lldb/lldb-enumerations.h
index 8e05f6ba9c876..74ff458bf87de 100644
--- a/lldb/include/lldb/lldb-enumerations.h
+++ b/lldb/include/lldb/lldb-enumerations.h
@@ -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){
diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp
index adb9e645610ba..2c336296f0b8d 100644
--- a/lldb/source/API/SBTarget.cpp
+++ b/lldb/source/API/SBTarget.cpp
@@ -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),
@@ -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),
diff --git a/lldb/source/Utility/RegularExpression.cpp b/lldb/source/Utility/RegularExpression.cpp
index 20bebbfe15f27..026793462221c 100644
--- a/lldb/source/Utility/RegularExpression.cpp
+++ b/lldb/source/Utility/RegularExpression.cpp
@@ -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()) {}
diff --git a/lldb/test/API/lang/cpp/class_static/TestStaticVariables.py b/lldb/test/API/lang/cpp/class_static/TestStaticVariables.py
index 8211d532b2638..dea7bff3a3d03 100644
--- a/lldb/test/API/lang/cpp/class_static/TestStaticVariables.py
+++ b/lldb/test/API/lang/cpp/class_static/TestStaticVariables.py
@@ -263,6 +263,10 @@ 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)

Copy link

github-actions bot commented Jun 13, 2024

✅ With the latest revision this PR passed the Python code formatter.

@medismailben
Copy link
Member

LGTM, may be we could also support this for the command line

(lldb) target modules lookup -F square
1 match found in /tmp/step:
        Address: step[0x0000000100003ee8] (step.__TEXT.__text + 0)
        Summary: step`square at step.c:3
(lldb) target modules lookup -F Square

@felipepiovezan
Copy link
Contributor

LGTM, may be we could also support this for the command line

Just keep in mind that those are very different queries w.r.t. speed: one goes through a fast path in the accelerator table, the other one doesn't.

@JDevlieghere JDevlieghere merged commit 8f2a4e8 into llvm:main Jun 13, 2024
6 checks passed
@JDevlieghere JDevlieghere deleted the case-insensitive branch June 13, 2024 16:37
EthanLuisMcDonough pushed a commit to EthanLuisMcDonough/llvm-project that referenced this pull request Aug 13, 2024
Support case-insensitive regex matches for
`SBTarget::FindGlobalFunctions` and `SBTarget::FindGlobalVariables`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants