aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2022-07-04 19:20:19 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-02-08 19:02:26 +0000
commit81ad626541db97eb356e2c1d4a20eb2a26a766ab (patch)
tree311b6a8987c32b1e1dcbab65c54cfac3fdb56175 /contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp
parent5fff09660e06a66bed6482da9c70df328e16bbb6 (diff)
parent145449b1e420787bb99721a429341fa6be3adfb6 (diff)
Diffstat (limited to 'contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp')
-rw-r--r--contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp70
1 files changed, 45 insertions, 25 deletions
diff --git a/contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp b/contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp
index 7ddc5c0c7e08..857193036e39 100644
--- a/contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp
+++ b/contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp
@@ -10,6 +10,9 @@
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/Error.h"
+
using namespace lldb;
using namespace lldb_private;
@@ -25,46 +28,63 @@ CommandObjectRegexCommand::CommandObjectRegexCommand(
// Destructor
CommandObjectRegexCommand::~CommandObjectRegexCommand() = default;
+llvm::Expected<std::string> CommandObjectRegexCommand::SubstituteVariables(
+ llvm::StringRef input,
+ const llvm::SmallVectorImpl<llvm::StringRef> &replacements) {
+ std::string buffer;
+ llvm::raw_string_ostream output(buffer);
+
+ llvm::SmallVector<llvm::StringRef, 4> parts;
+ input.split(parts, '%');
+
+ output << parts[0];
+ for (llvm::StringRef part : drop_begin(parts)) {
+ size_t idx = 0;
+ if (part.consumeInteger(10, idx))
+ output << '%';
+ else if (idx < replacements.size())
+ output << replacements[idx];
+ else
+ return llvm::make_error<llvm::StringError>(
+ llvm::formatv("%{0} is out of range: not enough arguments specified",
+ idx),
+ llvm::errc::invalid_argument);
+ output << part;
+ }
+
+ return output.str();
+}
+
bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
CommandReturnObject &result) {
EntryCollection::const_iterator pos, end = m_entries.end();
for (pos = m_entries.begin(); pos != end; ++pos) {
llvm::SmallVector<llvm::StringRef, 4> matches;
if (pos->regex.Execute(command, &matches)) {
- std::string new_command(pos->command);
- char percent_var[8];
- size_t idx, percent_var_idx;
- for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
- if (match_idx < matches.size()) {
- const std::string match_str = matches[match_idx].str();
- const int percent_var_len =
- ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
- for (idx = 0; (percent_var_idx = new_command.find(
- 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();
- }
- }
+ llvm::Expected<std::string> new_command =
+ SubstituteVariables(pos->command, matches);
+ if (!new_command) {
+ result.SetError(new_command.takeError());
+ return false;
}
+
// Interpret the new command and return this as the result!
if (m_interpreter.GetExpandRegexAliases())
- result.GetOutputStream().Printf("%s\n", new_command.c_str());
- // Pass in true for "no context switching". The command that called us
- // should have set up the context appropriately, we shouldn't have to
- // redo that.
- return m_interpreter.HandleCommand(new_command.c_str(),
- eLazyBoolCalculate, result);
+ result.GetOutputStream().Printf("%s\n", new_command->c_str());
+ // We don't have to pass an override_context here, as the command that
+ // called us should have set up the context appropriately.
+ return m_interpreter.HandleCommand(new_command->c_str(),
+ eLazyBoolNo, result);
}
}
result.SetStatus(eReturnStatusFailed);
if (!GetSyntax().empty())
result.AppendError(GetSyntax());
else
- result.GetOutputStream() << "Command contents '" << command
- << "' failed to match any "
- "regular expression in the '"
- << m_cmd_name << "' regex ";
+ result.GetErrorStream() << "Command contents '" << command
+ << "' failed to match any "
+ "regular expression in the '"
+ << m_cmd_name << "' regex ";
return false;
}