Skip to content

Commit 3c45476

Browse files
committed
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.
1 parent f2026f5 commit 3c45476

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

lldb/source/Commands/CommandObjectRegexCommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
4343
percent_var, idx)) != std::string::npos;) {
4444
new_command.erase(percent_var_idx, percent_var_len);
4545
new_command.insert(percent_var_idx, match_str);
46-
idx += percent_var_idx + match_str.size();
46+
idx = percent_var_idx + match_str.size();
4747
}
4848
}
4949
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
This tests some simple examples of parsing regex commands
3+
"""
4+
5+
import os
6+
import lldb
7+
import lldbsuite.test.lldbutil as lldbutil
8+
from lldbsuite.test.lldbtest import *
9+
10+
11+
class TestCommandRegexParsing(TestBase):
12+
13+
mydir = TestBase.compute_mydir(__file__)
14+
15+
NO_DEBUG_INFO_TESTCASE = True
16+
17+
def test_sample_rename_this(self):
18+
"""Try out some simple regex commands, make sure they parse correctly."""
19+
self.runCmd("command regex one-substitution 's/(.+)/echo-cmd %1-first %1-second %1-third/'")
20+
self.expect("one-substitution ASTRING",
21+
substrs = ["ASTRING-first", "ASTRING-second", "ASTRING-third"])
22+
23+
self.runCmd("command regex two-substitution 's/([^ ]+) ([^ ]+)/echo-cmd %1-first %2-second %1-third %2-fourth/'")
24+
self.expect("two-substitution ASTRING BSTRING",
25+
substrs = ["ASTRING-first", "BSTRING-second", "ASTRING-third", "BSTRING-fourth"])
26+
27+
def setUp(self):
28+
# Call super's setUp().
29+
TestBase.setUp(self)
30+
self.runCmd("command script import " + os.path.join(self.getSourceDir(), "echo_command.py"))
31+
self.runCmd("command script add echo-cmd -f echo_command.echo_command")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import lldb
2+
3+
def echo_command(debugger, args, result, dict):
4+
result.Print(args+'\n')
5+
result.SetStatus(lldb.eReturnStatusSuccessFinishResult)
6+
return True

0 commit comments

Comments
 (0)