[Lldb-commits] [lldb] 3c45476 - Fix a thinko in the parsing of substitutions in CommandObjectRegexCommand.

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Tue Jul 27 18:59:23 PDT 2021


Author: Jim Ingham
Date: 2021-07-27T18:58:56-07:00
New Revision: 3c4547692368239fca21ec294a5a406ea5a44889

URL: https://github.com/llvm/llvm-project/commit/3c4547692368239fca21ec294a5a406ea5a44889
DIFF: https://github.com/llvm/llvm-project/commit/3c4547692368239fca21ec294a5a406ea5a44889.diff

LOG: Fix a thinko in the parsing of substitutions in CommandObjectRegexCommand.

The old code incorrectly calculated the start position for the search
for the third (and subsequent) instance of a particular substitution
pattern (e.g. %1).

I also added a few test cases for this parsing covering this failure.

Added: 
    lldb/test/API/commands/command/regex/TestRegexCommand.py
    lldb/test/API/commands/command/regex/echo_command.py

Modified: 
    lldb/source/Commands/CommandObjectRegexCommand.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectRegexCommand.cpp b/lldb/source/Commands/CommandObjectRegexCommand.cpp
index be0f84d8142c5..46295421834a8 100644
--- a/lldb/source/Commands/CommandObjectRegexCommand.cpp
+++ b/lldb/source/Commands/CommandObjectRegexCommand.cpp
@@ -43,7 +43,7 @@ bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
                              percent_var, idx)) != std::string::npos;) {
             new_command.erase(percent_var_idx, percent_var_len);
             new_command.insert(percent_var_idx, match_str);
-            idx += percent_var_idx + match_str.size();
+            idx = percent_var_idx + match_str.size();
           }
         }
       }

diff  --git a/lldb/test/API/commands/command/regex/TestRegexCommand.py b/lldb/test/API/commands/command/regex/TestRegexCommand.py
new file mode 100644
index 0000000000000..5a70dc919f7f8
--- /dev/null
+++ b/lldb/test/API/commands/command/regex/TestRegexCommand.py
@@ -0,0 +1,31 @@
+"""
+This tests some simple examples of parsing regex commands
+"""
+
+import os
+import lldb
+import lldbsuite.test.lldbutil as lldbutil
+from lldbsuite.test.lldbtest import *
+
+
+class TestCommandRegexParsing(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def test_sample_rename_this(self):
+        """Try out some simple regex commands, make sure they parse correctly."""
+        self.runCmd("command regex one-substitution 's/(.+)/echo-cmd %1-first %1-second %1-third/'")
+        self.expect("one-substitution ASTRING",
+                    substrs = ["ASTRING-first", "ASTRING-second", "ASTRING-third"])
+
+        self.runCmd("command regex two-substitution 's/([^ ]+) ([^ ]+)/echo-cmd %1-first %2-second %1-third %2-fourth/'")
+        self.expect("two-substitution ASTRING BSTRING",
+                    substrs = ["ASTRING-first", "BSTRING-second", "ASTRING-third", "BSTRING-fourth"])
+        
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+        self.runCmd("command script import " + os.path.join(self.getSourceDir(), "echo_command.py"))
+        self.runCmd("command script add echo-cmd -f echo_command.echo_command")

diff  --git a/lldb/test/API/commands/command/regex/echo_command.py b/lldb/test/API/commands/command/regex/echo_command.py
new file mode 100644
index 0000000000000..cc2d7ebce2d57
--- /dev/null
+++ b/lldb/test/API/commands/command/regex/echo_command.py
@@ -0,0 +1,6 @@
+import lldb
+
+def echo_command(debugger, args, result, dict):
+    result.Print(args+'\n')
+    result.SetStatus(lldb.eReturnStatusSuccessFinishResult)
+    return True


        


More information about the lldb-commits mailing list