[Lldb-commits] [lldb] [lldb] Support ordered patterns in lldbtest.expect (PR #131475)

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Sat Mar 15 14:45:19 PDT 2025


https://github.com/kastiglione created https://github.com/llvm/llvm-project/pull/131475

Change `lldbtest.expect` to search the given `patterns` in order, if the `ordered`
parameter is true.

The `ordered` parameter is true by default, so this change also fixes tests by either
tweaking the patterns to work in order, or by setting `ordered=False`.

I have often wanted to test with `patterns` and also verify the order. This change
allows that.


>From dd2bf73c7118ba70dc0640f10cbdc25ef6886648 Mon Sep 17 00:00:00 2001
From: Dave Lee <davelee.com at gmail.com>
Date: Sat, 15 Mar 2025 11:20:36 -0700
Subject: [PATCH] [lldb] Support ordered patterns in lldbtest.expect

Change `lldbtest.expect` to search the given `patterns` in order, if the `ordered`
parameter is true.

The `ordered` parameter is true by default, so this change also fixes tests by either
tweaking the patterns to work in order, or by setting `ordered=False`.

I have often wanted to test with `patterns` and also verify the order. This change
allows that.
---
 lldb/packages/Python/lldbsuite/test/lldbtest.py | 17 +++++++++--------
 .../functionalities/alias/TestBtAliasRepeat.py  |  2 +-
 .../TestDataFormatterObjCNSContainer.py         |  2 ++
 .../TestDataFormatterGenericUnordered.py        |  4 ++--
 .../libcxx/span/TestDataFormatterLibcxxSpan.py  |  2 +-
 .../TestRootReferenceChildren.py                |  4 ++--
 .../lang/cpp/signed_types/TestSignedTypes.py    |  2 +-
 .../API/lang/objc/foundation/TestObjCMethods.py |  2 +-
 .../API/source-manager/TestSourceManager.py     |  1 +
 9 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/lldb/packages/Python/lldbsuite/test/lldbtest.py b/lldb/packages/Python/lldbsuite/test/lldbtest.py
index 590024ef77119..fa9105b701dcd 100644
--- a/lldb/packages/Python/lldbsuite/test/lldbtest.py
+++ b/lldb/packages/Python/lldbsuite/test/lldbtest.py
@@ -2460,9 +2460,9 @@ def found_str(matched):
         if substrs and matched == matching:
             start = 0
             for substr in substrs:
-                index = output[start:].find(substr)
-                start = start + index + len(substr) if ordered and matching else 0
+                index = output.find(substr, start)
                 matched = index != -1
+                start = index + len(substr) if ordered and matched else 0
                 log_lines.append(
                     '{} sub string: "{}" ({})'.format(
                         expecting_str, substr, found_str(matched)
@@ -2473,20 +2473,21 @@ def found_str(matched):
                     break
 
         if patterns and matched == matching:
+            start = 0
             for pattern in patterns:
-                matched = re.search(pattern, output)
+                pat = re.compile(pattern)
+                match = pat.search(output, start)
+                matched = bool(match)
+                start = match.end() if ordered and matched else 0
 
                 pattern_line = '{} regex pattern: "{}" ({}'.format(
                     expecting_str, pattern, found_str(matched)
                 )
-                if matched:
-                    pattern_line += ', matched "{}"'.format(matched.group(0))
+                if match:
+                    pattern_line += ', matched "{}"'.format(match.group(0))
                 pattern_line += ")"
                 log_lines.append(pattern_line)
 
-                # Convert to bool because match objects
-                # are True-ish but is not True itself
-                matched = bool(matched)
                 if matched != matching:
                     break
 
diff --git a/lldb/test/API/functionalities/alias/TestBtAliasRepeat.py b/lldb/test/API/functionalities/alias/TestBtAliasRepeat.py
index 42b5accddc0ba..60c0dd0a233bf 100644
--- a/lldb/test/API/functionalities/alias/TestBtAliasRepeat.py
+++ b/lldb/test/API/functionalities/alias/TestBtAliasRepeat.py
@@ -9,7 +9,7 @@ def test(self):
         lldbutil.run_to_source_breakpoint(self, "return", lldb.SBFileSpec("main.c"))
 
         # Expect "frame #0" but not "frame #1".
-        self.expect("bt 1", inHistory=True, patterns=["frame #0", "^(?!.*frame #1)"])
+        self.expect("bt 1", inHistory=True, patterns=["frame #0", "(?!.*frame #1)"])
 
         # Run an empty command to run the repeat command for `bt`.
         # The repeat command for `bt N` lists the subsequent N frames.
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjCNSContainer.py b/lldb/test/API/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjCNSContainer.py
index c90a5c61d9c0b..e6fb549945666 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjCNSContainer.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjCNSContainer.py
@@ -52,6 +52,7 @@ def nscontainers_data_formatter_commands(self):
 
         self.expect(
             "frame variable -d run-target *nscfDictionary",
+            ordered=False,
             patterns=[
                 r"\(__NSCFDictionary\) \*nscfDictionary =",
                 'key = 0x.* @"foo"',
@@ -67,6 +68,7 @@ def nscontainers_data_formatter_commands(self):
 
         self.expect(
             "frame variable -d run-target *cfDictionaryRef",
+            ordered=False,
             patterns=[
                 r"\(const __CFDictionary\) \*cfDictionaryRef =",
                 'key = 0x.* @"foo"',
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/unordered/TestDataFormatterGenericUnordered.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/unordered/TestDataFormatterGenericUnordered.py
index 50dfbbf6b90a5..71cd12f3a3d56 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/unordered/TestDataFormatterGenericUnordered.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/unordered/TestDataFormatterGenericUnordered.py
@@ -122,8 +122,8 @@ def cleanup():
         )
 
     def look_for_content_and_continue(self, var_name, patterns):
-        self.expect(("frame variable %s" % var_name), patterns=patterns)
-        self.expect(("frame variable %s" % var_name), patterns=patterns)
+        self.expect(("frame variable %s" % var_name), ordered=False, patterns=patterns)
+        self.expect(("frame variable %s" % var_name), ordered=False, patterns=patterns)
         self.runCmd("continue")
 
     @add_test_categories(["libstdcxx"])
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py
index 4df4fa1acc8e7..42efe415b6acf 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py
@@ -172,4 +172,4 @@ def test_ref_and_ptr(self):
 
         # The pointer should just show the right number of elements:
 
-        self.expect("frame variable ptr", patterns=["ptr = 0x.*", " size=5"])
+        self.expect("frame variable ptr", patterns=["ptr = 0x[0-9a-f]+ size=5"])
diff --git a/lldb/test/API/functionalities/data-formatter/root-reference-children/TestRootReferenceChildren.py b/lldb/test/API/functionalities/data-formatter/root-reference-children/TestRootReferenceChildren.py
index 5de66177e7cad..7c366445b1c6e 100644
--- a/lldb/test/API/functionalities/data-formatter/root-reference-children/TestRootReferenceChildren.py
+++ b/lldb/test/API/functionalities/data-formatter/root-reference-children/TestRootReferenceChildren.py
@@ -20,8 +20,8 @@ def test(self):
             "v summary_and_children_ref", substrs=["some summary", "child = 30"]
         )
         self.expect(
-            "v summary_only_ref", patterns=["some summary", "(?s)^(?!.*child = )"]
+            "v summary_only_ref", patterns=["some summary", "(?s)(?!.*child = )"]
         )
         self.expect(
-            "v children_only_ref", patterns=["(?s)^(?!.*some summary)", "child = 30"]
+            "v children_only_ref", patterns=["(?s)(?!.*some summary)", "child = 30"]
         )
diff --git a/lldb/test/API/lang/cpp/signed_types/TestSignedTypes.py b/lldb/test/API/lang/cpp/signed_types/TestSignedTypes.py
index b8c2c23613868..2a5c9bb1bd766 100644
--- a/lldb/test/API/lang/cpp/signed_types/TestSignedTypes.py
+++ b/lldb/test/API/lang/cpp/signed_types/TestSignedTypes.py
@@ -57,8 +57,8 @@ def test(self):
             "frame variable --show-types --no-args",
             VARIABLES_DISPLAYED_CORRECTLY,
             patterns=[
-                r"\((short int|short)\) the_signed_short = 99",
                 r"\((signed char|char)\) the_signed_char = 'c'",
+                r"\((short int|short)\) the_signed_short = 99",
             ],
             substrs=[
                 "(int) the_signed_int = 99",
diff --git a/lldb/test/API/lang/objc/foundation/TestObjCMethods.py b/lldb/test/API/lang/objc/foundation/TestObjCMethods.py
index 5fa3f280d33bf..e82cb2f308602 100644
--- a/lldb/test/API/lang/objc/foundation/TestObjCMethods.py
+++ b/lldb/test/API/lang/objc/foundation/TestObjCMethods.py
@@ -166,7 +166,7 @@ def test_data_type_and_expr(self):
             "frame variable --show-types --scope",
             VARIABLES_DISPLAYED_CORRECTLY,
             substrs=["ARG: (MyString *) self"],
-            patterns=[r"ARG: \(.*\) _cmd", "(objc_selector *)|(SEL)"],
+            patterns=[r"ARG: \(SEL|objc_selector \*\) _cmd"],
         )
 
         # rdar://problem/8651752
diff --git a/lldb/test/API/source-manager/TestSourceManager.py b/lldb/test/API/source-manager/TestSourceManager.py
index 1283c73e152a9..eca0dd5e6159f 100644
--- a/lldb/test/API/source-manager/TestSourceManager.py
+++ b/lldb/test/API/source-manager/TestSourceManager.py
@@ -129,6 +129,7 @@ def do_display_source_python_api(
             stream.GetData(),
             "Source code displayed correctly:\n" + stream.GetData(),
             exe=False,
+            ordered=False,
             patterns=["=>", "%d.*Hello world" % self.line, needle_regex],
         )
 



More information about the lldb-commits mailing list