-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[lldb/Commands] Alias script
command to scripting run
#97263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
medismailben
merged 1 commit into
llvm:main
from
medismailben:scripting-top-level-command
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
//===-- CommandObjectScripting.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 "CommandObjectScripting.h" | ||
#include "lldb/Core/Debugger.h" | ||
#include "lldb/DataFormatters/DataVisualization.h" | ||
#include "lldb/Host/Config.h" | ||
#include "lldb/Host/OptionParser.h" | ||
#include "lldb/Interpreter/CommandInterpreter.h" | ||
#include "lldb/Interpreter/CommandOptionArgumentTable.h" | ||
#include "lldb/Interpreter/CommandReturnObject.h" | ||
#include "lldb/Interpreter/OptionArgParser.h" | ||
#include "lldb/Interpreter/ScriptInterpreter.h" | ||
#include "lldb/Utility/Args.h" | ||
|
||
using namespace lldb; | ||
using namespace lldb_private; | ||
|
||
#define LLDB_OPTIONS_scripting_run | ||
#include "CommandOptions.inc" | ||
|
||
class CommandObjectScriptingRun : public CommandObjectRaw { | ||
public: | ||
CommandObjectScriptingRun(CommandInterpreter &interpreter) | ||
: CommandObjectRaw( | ||
interpreter, "scripting run", | ||
"Invoke the script interpreter with provided code and display any " | ||
"results. Start the interactive interpreter if no code is " | ||
"supplied.", | ||
"scripting run [--language <scripting-language> --] " | ||
"[<script-code>]") {} | ||
|
||
~CommandObjectScriptingRun() override = default; | ||
|
||
Options *GetOptions() override { return &m_options; } | ||
|
||
class CommandOptions : public Options { | ||
public: | ||
CommandOptions() = default; | ||
~CommandOptions() override = default; | ||
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, | ||
ExecutionContext *execution_context) override { | ||
Status error; | ||
const int short_option = m_getopt_table[option_idx].val; | ||
|
||
switch (short_option) { | ||
case 'l': | ||
language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( | ||
option_arg, GetDefinitions()[option_idx].enum_values, | ||
eScriptLanguageNone, error); | ||
if (!error.Success()) | ||
error.SetErrorStringWithFormat("unrecognized value for language '%s'", | ||
option_arg.str().c_str()); | ||
break; | ||
default: | ||
llvm_unreachable("Unimplemented option"); | ||
} | ||
|
||
return error; | ||
} | ||
|
||
void OptionParsingStarting(ExecutionContext *execution_context) override { | ||
language = lldb::eScriptLanguageNone; | ||
} | ||
|
||
llvm::ArrayRef<OptionDefinition> GetDefinitions() override { | ||
return llvm::ArrayRef(g_scripting_run_options); | ||
} | ||
|
||
lldb::ScriptLanguage language = lldb::eScriptLanguageNone; | ||
}; | ||
|
||
protected: | ||
void DoExecute(llvm::StringRef command, | ||
CommandReturnObject &result) override { | ||
// Try parsing the language option but when the command contains a raw part | ||
// separated by the -- delimiter. | ||
OptionsWithRaw raw_args(command); | ||
if (raw_args.HasArgs()) { | ||
if (!ParseOptions(raw_args.GetArgs(), result)) | ||
return; | ||
command = raw_args.GetRawPart(); | ||
} | ||
|
||
lldb::ScriptLanguage language = | ||
(m_options.language == lldb::eScriptLanguageNone) | ||
? m_interpreter.GetDebugger().GetScriptLanguage() | ||
: m_options.language; | ||
|
||
if (language == lldb::eScriptLanguageNone) { | ||
result.AppendError( | ||
"the script-lang setting is set to none - scripting not available"); | ||
return; | ||
} | ||
|
||
ScriptInterpreter *script_interpreter = | ||
GetDebugger().GetScriptInterpreter(true, language); | ||
|
||
if (script_interpreter == nullptr) { | ||
result.AppendError("no script interpreter"); | ||
return; | ||
} | ||
|
||
// Script might change Python code we use for formatting. Make sure we keep | ||
// up to date with it. | ||
DataVisualization::ForceUpdate(); | ||
|
||
if (command.empty()) { | ||
script_interpreter->ExecuteInterpreterLoop(); | ||
result.SetStatus(eReturnStatusSuccessFinishNoResult); | ||
return; | ||
} | ||
|
||
// We can do better when reporting the status of one-liner script execution. | ||
if (script_interpreter->ExecuteOneLine(command, &result)) | ||
result.SetStatus(eReturnStatusSuccessFinishNoResult); | ||
else | ||
result.SetStatus(eReturnStatusFailed); | ||
} | ||
|
||
private: | ||
CommandOptions m_options; | ||
}; | ||
|
||
#pragma mark CommandObjectMultiwordScripting | ||
|
||
// CommandObjectMultiwordScripting | ||
|
||
CommandObjectMultiwordScripting::CommandObjectMultiwordScripting( | ||
CommandInterpreter &interpreter) | ||
: CommandObjectMultiword( | ||
interpreter, "scripting", | ||
"Commands for operating on the scripting functionnalities.", | ||
"scripting <subcommand> [<subcommand-options>]") { | ||
LoadSubCommand("run", | ||
CommandObjectSP(new CommandObjectScriptingRun(interpreter))); | ||
} | ||
|
||
CommandObjectMultiwordScripting::~CommandObjectMultiwordScripting() = default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===-- CommandObjectScripting.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_SOURCE_INTERPRETER_COMMANDOBJECTSCRIPTING_H | ||
#define LLDB_SOURCE_INTERPRETER_COMMANDOBJECTSCRIPTING_H | ||
|
||
#include "lldb/Interpreter/CommandObjectMultiword.h" | ||
|
||
namespace lldb_private { | ||
|
||
class CommandObjectMultiwordScripting : public CommandObjectMultiword { | ||
public: | ||
CommandObjectMultiwordScripting(CommandInterpreter &interpreter); | ||
|
||
~CommandObjectMultiwordScripting() override; | ||
}; | ||
|
||
} // namespace lldb_private | ||
|
||
#endif // LLDB_SOURCE_INTERPRETER_COMMANDOBJECTSCRIPTING_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,13 @@ def test_command_abbreviations_and_aliases(self): | |
# Check a command that wants the raw input. | ||
command_interpreter.ResolveCommand(r"""sc print("\n\n\tHello!\n")""", result) | ||
self.assertTrue(result.Succeeded()) | ||
Comment on lines
81
to
82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could test all the prefixes with something like this:
|
||
self.assertEqual(r"""script print("\n\n\tHello!\n")""", result.GetOutput()) | ||
medismailben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual( | ||
r"""scripting run print("\n\n\tHello!\n")""", result.GetOutput() | ||
) | ||
|
||
command_interpreter.ResolveCommand("script 1+1", result) | ||
self.assertTrue(result.Succeeded()) | ||
self.assertEqual("scripting run 1+1", result.GetOutput()) | ||
|
||
# Prompt changing stuff should be tested, but this doesn't seem like the | ||
# right test to do it in. It has nothing to do with aliases or abbreviations. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is unfortunate, do these pollute help output? if so, can we hide them?
is there no way to make a prefix match an alias, when it's shorter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that if we have the command
scripting
and the aliasscript
and we seesc
we need to resolve that command ambiguity. The only tool we have for ambiguous command resolution at present is "exact matches always win".If we wanted to solve this w/o making all the exact matches we intend to win, we could add a ranking system to the commands and aliases, which bias ambiguous matches in favor of one or the other of the commands, or some similar system. I worry that would get messy pretty quickly, but anyway, you'd need something like that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's not the most elegant solution but I'd prefer solving the partial matching issue in a follow-up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be a good idea to write a comment explaining the constraint here? If I came across this code snippet I'd definitely want to know why we do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1