
Jim noticed that the regex command is unintentionally recursive. Let's use the following command regex as an example: (lldb) com regex humm 's/([^ ]+) ([^ ]+)/p %1 %2 %1 %2/' If we call it with arguments foo bar, thing behave as expected: (lldb) humm foo bar (...) foo bar foo bar However, if we include %2 in the arguments, things break down: (lldb) humm fo%2o bar (...) fobaro bar fobaro bar The problem is that the implementation of the substitution is too naive. It substitutes the %1 token into the target template in place, then does the %2 substitution starting with the resultant string. So if the previous substitution introduced a %2 token, it would get processed in the second sweep, etc. This patch addresses the issue by walking the command once and substituting the % variables in place. (lldb) humm fo%2o bar (...) fo%2o bar fo%2o bar Furthermore, this patch also reports an error if not enough variables were provided and add support for substituting %0. rdar://81236994 Differential revision: https://reviews.llvm.org/D120101
69 lines
2.7 KiB
C++
69 lines
2.7 KiB
C++
//===-- TestRegexCommand.cpp ----------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Commands/CommandObjectRegexCommand.h"
|
|
#include "llvm/Testing/Support/Error.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace lldb_private;
|
|
using namespace lldb;
|
|
|
|
namespace {
|
|
class TestRegexCommand : public CommandObjectRegexCommand {
|
|
public:
|
|
using CommandObjectRegexCommand::SubstituteVariables;
|
|
|
|
static std::string
|
|
Substitute(llvm::StringRef input,
|
|
const llvm::SmallVectorImpl<llvm::StringRef> &replacements) {
|
|
llvm::Expected<std::string> str = SubstituteVariables(input, replacements);
|
|
if (!str)
|
|
return llvm::toString(str.takeError());
|
|
return *str;
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
TEST(RegexCommandTest, SubstituteVariablesSuccess) {
|
|
const llvm::SmallVector<llvm::StringRef, 4> substitutions = {"all", "foo",
|
|
"bar", "baz"};
|
|
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%0", substitutions), "all");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%1", substitutions), "foo");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%2", substitutions), "bar");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%3", substitutions), "baz");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%1%2%3", substitutions), "foobarbaz");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("#%1#%2#%3#", substitutions),
|
|
"#foo#bar#baz#");
|
|
}
|
|
|
|
TEST(RegexCommandTest, SubstituteVariablesFailed) {
|
|
const llvm::SmallVector<llvm::StringRef, 4> substitutions = {"all", "foo",
|
|
"bar", "baz"};
|
|
|
|
ASSERT_THAT_EXPECTED(
|
|
TestRegexCommand::SubstituteVariables("%1%2%3%4", substitutions),
|
|
llvm::Failed());
|
|
ASSERT_THAT_EXPECTED(
|
|
TestRegexCommand::SubstituteVariables("%5", substitutions),
|
|
llvm::Failed());
|
|
ASSERT_THAT_EXPECTED(
|
|
TestRegexCommand::SubstituteVariables("%11", substitutions),
|
|
llvm::Failed());
|
|
}
|
|
|
|
TEST(RegexCommandTest, SubstituteVariablesNoRecursion) {
|
|
const llvm::SmallVector<llvm::StringRef, 4> substitutions = {"all", "%2",
|
|
"%3", "%4"};
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%0", substitutions), "all");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%1", substitutions), "%2");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%2", substitutions), "%3");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%3", substitutions), "%4");
|
|
EXPECT_EQ(TestRegexCommand::Substitute("%1%2%3", substitutions), "%2%3%4");
|
|
}
|