Files
clang-p2996/lldb/source/Commands/CommandObjectRegexCommand.h
Jonas Devlieghere 2a6dbedf5a [lldb] Fix (unintentional) recursion in CommandObjectRegexCommand
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
2022-02-23 12:34:14 -08:00

67 lines
2.1 KiB
C++

//===-- CommandObjectRegexCommand.h -----------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_INTERPRETER_COMMANDOBJECTREGEXCOMMAND_H
#define LLDB_INTERPRETER_COMMANDOBJECTREGEXCOMMAND_H
#include <list>
#include "lldb/Interpreter/CommandObject.h"
#include "lldb/Utility/CompletionRequest.h"
#include "lldb/Utility/RegularExpression.h"
namespace lldb_private {
// CommandObjectRegexCommand
class CommandObjectRegexCommand : public CommandObjectRaw {
public:
CommandObjectRegexCommand(CommandInterpreter &interpreter,
llvm::StringRef name, llvm::StringRef help,
llvm::StringRef syntax, uint32_t max_matches,
uint32_t completion_type_mask, bool is_removable);
~CommandObjectRegexCommand() override;
bool IsRemovable() const override { return m_is_removable; }
bool AddRegexCommand(llvm::StringRef re_cstr, llvm::StringRef command_cstr);
bool HasRegexEntries() const { return !m_entries.empty(); }
void HandleCompletion(CompletionRequest &request) override;
protected:
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
/// Substitute variables of the format %\d+ in the input string.
static llvm::Expected<std::string> SubstituteVariables(
llvm::StringRef input,
const llvm::SmallVectorImpl<llvm::StringRef> &replacements);
struct Entry {
RegularExpression regex;
std::string command;
};
typedef std::list<Entry> EntryCollection;
const uint32_t m_max_matches;
const uint32_t m_completion_type_mask;
EntryCollection m_entries;
bool m_is_removable;
private:
CommandObjectRegexCommand(const CommandObjectRegexCommand &) = delete;
const CommandObjectRegexCommand &
operator=(const CommandObjectRegexCommand &) = delete;
};
} // namespace lldb_private
#endif // LLDB_INTERPRETER_COMMANDOBJECTREGEXCOMMAND_H