Skip to content

Commit

Permalink
Revert "Be more careful to maintain quoting information when parsing …
Browse files Browse the repository at this point in the history
…commands."

This reverts commit 6c089b2.

This was causing the test test_help_run_hides_options from TestHelp.py to
fail on Linux and Windows (but the test succeeds on macOS).  The decision
to print option information is determined by CommandObjectAlias::IsDashDashCommand
which was changed, but only by replacing an inline string constant with a const char *
CommandInterpreter::g_argument which has the same string value.  I can't see why this
would fail, I'll have to spin up a vm to see if I can repo there.
  • Loading branch information
jimingham committed Sep 13, 2022
1 parent a2d0a01 commit ac05bc0
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 142 deletions.
8 changes: 0 additions & 8 deletions lldb/include/lldb/Interpreter/CommandInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,6 @@ class CommandInterpreter : public Broadcaster,
eCommandTypesAllThem = 0xFFFF //< all commands
};

// The CommandAlias and CommandInterpreter both have a hand in
// substituting for alias commands. They work by writing special tokens
// in the template form of the Alias command, and then detecting them when the
// command is executed. These are the special tokens:
static const char * const g_no_argument;
static const char * const g_need_argument;
static const char * const g_argument;

CommandInterpreter(Debugger &debugger, bool synchronous_execution);

~CommandInterpreter() override = default;
Expand Down
7 changes: 0 additions & 7 deletions lldb/packages/Python/lldbsuite/test/lldbtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,13 +2472,6 @@ def assertSuccess(self, obj, msg=None):
self.fail(self._formatMessage(msg,
"'{}' is not success".format(error)))

"""Assert that a command return object is successful"""
def assertCommandReturn(self, obj, msg=None):
if not obj.Succeeded():
error = obj.GetError()
self.fail(self._formatMessage(msg,
"'{}' is not success".format(error)))

"""Assert two states are equal"""
def assertState(self, first, second, msg=None):
if first != second:
Expand Down
14 changes: 5 additions & 9 deletions lldb/source/Interpreter/CommandAlias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ static bool ProcessAliasOptionsArgs(lldb::CommandObjectSP &cmd_obj_sp,

if (!options_string.empty()) {
if (cmd_obj_sp->WantsRawCommandString())
option_arg_vector->emplace_back(CommandInterpreter::g_argument,
-1, options_string);
option_arg_vector->emplace_back("<argument>", -1, options_string);
else {
for (auto &entry : args.entries()) {
if (!entry.ref().empty())
option_arg_vector->emplace_back(std::string(CommandInterpreter::g_argument), -1,
option_arg_vector->emplace_back(std::string("<argument>"), -1,
std::string(entry.ref()));
}
}
Expand Down Expand Up @@ -154,12 +153,11 @@ void CommandAlias::GetAliasExpansion(StreamString &help_string) const {

for (const auto &opt_entry : *options) {
std::tie(opt, std::ignore, value) = opt_entry;
if (opt == CommandInterpreter::g_argument) {
if (opt == "<argument>") {
help_string.Printf(" %s", value.c_str());
} else {
help_string.Printf(" %s", opt.c_str());
if ((value != CommandInterpreter::g_no_argument)
&& (value != CommandInterpreter::g_need_argument)) {
if ((value != "<no-argument>") && (value != "<need-argument")) {
help_string.Printf(" %s", value.c_str());
}
}
Expand All @@ -180,7 +178,7 @@ bool CommandAlias::IsDashDashCommand() {

for (const auto &opt_entry : *GetOptionArguments()) {
std::tie(opt, std::ignore, value) = opt_entry;
if (opt == CommandInterpreter::g_argument && !value.empty() &&
if (opt == "<argument>" && !value.empty() &&
llvm::StringRef(value).endswith("--")) {
m_is_dashdash_alias = eLazyBoolYes;
break;
Expand Down Expand Up @@ -208,8 +206,6 @@ std::pair<lldb::CommandObjectSP, OptionArgVectorSP> CommandAlias::Desugar() {
return {nullptr, nullptr};

if (underlying->IsAlias()) {
// FIXME: This doesn't work if the original alias fills a slot in the
// underlying alias, since this just appends the two lists.
auto desugared = ((CommandAlias *)underlying.get())->Desugar();
auto options = GetOptionArguments();
options->insert(options->begin(), desugared.second->begin(),
Expand Down
62 changes: 14 additions & 48 deletions lldb/source/Interpreter/CommandInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ static constexpr const char *InitFileWarning =
"and\n"
"accept the security risk.";

constexpr const char *CommandInterpreter::g_no_argument = "<no-argument>";
constexpr const char *CommandInterpreter::g_need_argument = "<need-argument>";
constexpr const char *CommandInterpreter::g_argument = "<argument>";


#define LLDB_PROPERTIES_interpreter
#include "InterpreterProperties.inc"

Expand Down Expand Up @@ -1639,7 +1634,7 @@ CommandObject *CommandInterpreter::BuildAliasResult(
std::string value;
for (const auto &entry : *option_arg_vector) {
std::tie(option, value_type, value) = entry;
if (option == g_argument) {
if (option == "<argument>") {
result_str.Printf(" %s", value.c_str());
continue;
}
Expand All @@ -1661,33 +1656,11 @@ CommandObject *CommandInterpreter::BuildAliasResult(
index);
return nullptr;
} else {
const Args::ArgEntry &entry = cmd_args[index];
size_t strpos = raw_input_string.find(entry.c_str());
const char quote_char = entry.GetQuoteChar();
if (strpos != std::string::npos) {
const size_t start_fudge = quote_char == '\0' ? 0 : 1;
const size_t len_fudge = quote_char == '\0' ? 0 : 2;

// Make sure we aren't going outside the bounds of the cmd string:
if (strpos < start_fudge) {
result.AppendError("Unmatched quote at command beginning.");
return nullptr;
}
llvm::StringRef arg_text = entry.ref();
if (strpos - start_fudge + arg_text.size() + len_fudge
> raw_input_string.size()) {
result.AppendError("Unmatched quote at command end.");
return nullptr;
}
size_t strpos = raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
if (strpos != std::string::npos)
raw_input_string = raw_input_string.erase(
strpos - start_fudge,
strlen(cmd_args.GetArgumentAtIndex(index)) + len_fudge);
}
if (quote_char == '\0')
result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index));
else
result_str.Printf("%c%s%c", quote_char,
entry.c_str(), quote_char);
strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index));
}
}

Expand Down Expand Up @@ -1938,6 +1911,13 @@ bool CommandInterpreter::HandleCommand(const char *command_line,
return true;
}

Status error(PreprocessCommand(command_string));

if (error.Fail()) {
result.AppendError(error.AsCString());
return false;
}

// Phase 1.

// Before we do ANY kind of argument processing, we need to figure out what
Expand All @@ -1955,20 +1935,6 @@ bool CommandInterpreter::HandleCommand(const char *command_line,

CommandObject *cmd_obj = ResolveCommandImpl(command_string, result);

// We have to preprocess the whole command string for Raw commands, since we
// don't know the structure of the command. For parsed commands, we only
// treat backticks as quote characters specially.
// FIXME: We probably want to have raw commands do their own preprocessing.
// For instance, I don't think people expect substitution in expr expressions.
if (cmd_obj && cmd_obj->WantsRawCommandString()) {
Status error(PreprocessCommand(command_string));

if (error.Fail()) {
result.AppendError(error.AsCString());
return false;
}
}

// Although the user may have abbreviated the command, the command_string now
// has the command expanded to the full name. For example, if the input was
// "br s -n main", command_string is now "breakpoint set -n main".
Expand Down Expand Up @@ -2197,7 +2163,7 @@ void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
std::string value;
for (const auto &option_entry : *option_arg_vector) {
std::tie(option, value_type, value) = option_entry;
if (option == g_argument) {
if (option == "<argument>") {
if (!wants_raw_input || (value != "--")) {
// Since we inserted this above, make sure we don't insert it twice
new_args.AppendArgument(value);
Expand All @@ -2208,7 +2174,7 @@ void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
if (value_type != OptionParser::eOptionalArgument)
new_args.AppendArgument(option);

if (value == g_no_argument)
if (value == "<no-argument>")
continue;

int index = GetOptionArgumentPosition(value.c_str());
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Interpreter/CommandObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ bool CommandObjectParsed::Execute(const char *args_string,
}
if (!handled) {
for (auto entry : llvm::enumerate(cmd_args.entries())) {
if (!entry.value().ref().empty() && entry.value().GetQuoteChar() == '`') {
if (!entry.value().ref().empty() && entry.value().ref().front() == '`') {
cmd_args.ReplaceArgumentAtIndex(
entry.index(),
m_interpreter.ProcessEmbeddedScriptCommands(entry.value().c_str()));
Expand Down
38 changes: 14 additions & 24 deletions lldb/source/Interpreter/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,47 +1002,37 @@ llvm::Expected<Args> Options::ParseAlias(const Args &args,
.str(),
llvm::inconvertibleErrorCode());
}
if (!option_arg)
option_arg = "<no-argument>";
option_arg_vector->emplace_back(std::string(option_str.GetString()),
has_arg, std::string(option_arg));

// Find option in the argument list; also see if it was supposed to take an
// argument and if one was supplied. Remove option (and argument, if
// given) from the argument list. Also remove them from the
// raw_input_string, if one was passed in.
// Note: We also need to preserve any option argument values that were
// surrounded by backticks, as we lose track of them in the
// option_args_vector.
size_t idx =
FindArgumentIndexForOption(args_copy, long_options[long_options_index]);
std::string option_to_insert;
if (option_arg) {
if (idx != size_t(-1) && has_arg) {
bool arg_has_backtick = args_copy[idx + 1].GetQuoteChar() == '`';
if (arg_has_backtick)
option_to_insert = "`";
option_to_insert += option_arg;
if (arg_has_backtick)
option_to_insert += "`";
} else
option_to_insert = option_arg;
} else
option_to_insert = CommandInterpreter::g_no_argument;

option_arg_vector->emplace_back(std::string(option_str.GetString()),
has_arg, option_to_insert);

if (idx == size_t(-1))
continue;

if (!input_line.empty()) {
llvm::StringRef tmp_arg = args_copy[idx].ref();
auto tmp_arg = args_copy[idx].ref();
size_t pos = input_line.find(std::string(tmp_arg));
if (pos != std::string::npos)
input_line.erase(pos, tmp_arg.size());
}
args_copy.DeleteArgumentAtIndex(idx);
if (option_to_insert != CommandInterpreter::g_no_argument) {
if ((long_options[long_options_index].definition->option_has_arg !=
OptionParser::eNoArgument) &&
(OptionParser::GetOptionArgument() != nullptr) &&
(idx < args_copy.GetArgumentCount()) &&
(args_copy[idx].ref() == OptionParser::GetOptionArgument())) {
if (input_line.size() > 0) {
size_t pos = input_line.find(option_to_insert);
auto tmp_arg = args_copy[idx].ref();
size_t pos = input_line.find(std::string(tmp_arg));
if (pos != std::string::npos)
input_line.erase(pos, option_to_insert.size());
input_line.erase(pos, tmp_arg.size());
}
args_copy.DeleteArgumentAtIndex(idx);
}
Expand Down
5 changes: 0 additions & 5 deletions lldb/source/Utility/Args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ bool Args::GetCommandString(std::string &command) const {
for (size_t i = 0; i < m_entries.size(); ++i) {
if (i > 0)
command += ' ';
char quote = m_entries[i].quote;
if (quote != '\0')
command += quote;
command += m_entries[i].ref();
if (quote != '\0')
command += quote;
}

return !m_entries.empty();
Expand Down
3 changes: 0 additions & 3 deletions lldb/test/API/commands/command/backticks/Makefile

This file was deleted.

32 changes: 0 additions & 32 deletions lldb/test/API/commands/command/backticks/TestBackticksInAlias.py

This file was deleted.

5 changes: 0 additions & 5 deletions lldb/test/API/commands/command/backticks/main.c

This file was deleted.

0 comments on commit ac05bc0

Please sign in to comment.