Author: Jim Ingham Date: 2021-09-29T19:33:41-07:00 New Revision: 3bf3b96629e8dfc55d01ba0cb05ca01a467017fa
URL: https://github.com/llvm/llvm-project/commit/3bf3b96629e8dfc55d01ba0cb05ca01a467017fa DIFF: https://github.com/llvm/llvm-project/commit/3bf3b96629e8dfc55d01ba0cb05ca01a467017fa.diff LOG: Add the --relative-to-command-file to "command source" so you can have linked command files in a source tree and get to them all from one main command file. Differential Revision: https://reviews.llvm.org/D110601 Added: lldb/test/API/commands/command/source/commands2.txt lldb/test/API/commands/command/source/not-relative.txt lldb/test/API/commands/command/source/subdir/subcmds.txt Modified: lldb/source/Commands/CommandObjectCommands.cpp lldb/source/Commands/Options.td lldb/test/API/commands/command/source/TestCommandSource.py Removed: ################################################################################ diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index 9a8b81c007ad8..639279875e715 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -77,7 +77,7 @@ class CommandObjectCommandsSource : public CommandObjectParsed { public: CommandOptions() : Options(), m_stop_on_error(true), m_silent_run(false), - m_stop_on_continue(true) {} + m_stop_on_continue(true), m_cmd_relative_to_command_file(false) {} ~CommandOptions() override = default; @@ -95,6 +95,10 @@ class CommandObjectCommandsSource : public CommandObjectParsed { error = m_stop_on_continue.SetValueFromString(option_arg); break; + case 'C': + m_cmd_relative_to_command_file = true; + break; + case 's': error = m_silent_run.SetValueFromString(option_arg); break; @@ -110,6 +114,7 @@ class CommandObjectCommandsSource : public CommandObjectParsed { m_stop_on_error.Clear(); m_silent_run.Clear(); m_stop_on_continue.Clear(); + m_cmd_relative_to_command_file.Clear(); } llvm::ArrayRef<OptionDefinition> GetDefinitions() override { @@ -121,6 +126,7 @@ class CommandObjectCommandsSource : public CommandObjectParsed { OptionValueBoolean m_stop_on_error; OptionValueBoolean m_silent_run; OptionValueBoolean m_stop_on_continue; + OptionValueBoolean m_cmd_relative_to_command_file; }; bool DoExecute(Args &command, CommandReturnObject &result) override { @@ -131,7 +137,29 @@ class CommandObjectCommandsSource : public CommandObjectParsed { return false; } + FileSpec source_dir = {}; + if (m_options.m_cmd_relative_to_command_file) { + source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir(); + if (!source_dir) { + result.AppendError("command source -C can only be specified " + "from a command file"); + result.SetStatus(eReturnStatusFailed); + return false; + } + } + FileSpec cmd_file(command[0].ref()); + if (source_dir) { + // Prepend the source_dir to the cmd_file path: + if (!cmd_file.IsRelative()) { + result.AppendError("command source -C can only be used " + "with a relative path."); + result.SetStatus(eReturnStatusFailed); + return false; + } + cmd_file.MakeAbsolute(source_dir); + } + FileSystem::Instance().Resolve(cmd_file); CommandInterpreterRunOptions options; diff --git a/lldb/source/Commands/Options.td b/lldb/source/Commands/Options.td index 83df2ac22c578..3d69bb8ad8d05 100644 --- a/lldb/source/Commands/Options.td +++ b/lldb/source/Commands/Options.td @@ -536,6 +536,10 @@ let Command = "source" in { Desc<"If true, stop executing commands on continue.">; def source_silent_run : Option<"silent-run", "s">, Arg<"Boolean">, Desc<"If true don't echo commands while executing.">; + def cmd_relative_to_command_file : Option<"relative-to-command-file", "C">, + Desc<"Resolve non-absolute paths relative to the location of the " + "current command file. This argument can only be used when the command is " + "being sourced from a file.">; } let Command = "alias" in { diff --git a/lldb/test/API/commands/command/source/TestCommandSource.py b/lldb/test/API/commands/command/source/TestCommandSource.py index 6d2717b16e2dc..dc32e20ddba0a 100644 --- a/lldb/test/API/commands/command/source/TestCommandSource.py +++ b/lldb/test/API/commands/command/source/TestCommandSource.py @@ -21,7 +21,18 @@ def test_command_source(self): # Sourcing .lldb in the current working directory, which in turn imports # the "my" package that defines the date() function. self.runCmd("command source .lldb") + self.check_results() + + @no_debug_info_test + def test_command_source_relative(self): + """Test that lldb command "command source" works correctly with relative paths.""" + # Sourcing .lldb in the current working directory, which in turn imports + # the "my" package that defines the date() function. + self.runCmd("command source commands2.txt") + self.check_results() + + def check_results(self, failure=False): # Python should evaluate "my.date()" successfully. command_interpreter = self.dbg.GetCommandInterpreter() self.assertTrue(command_interpreter, VALID_COMMAND_INTERPRETER) @@ -29,6 +40,18 @@ def test_command_source(self): command_interpreter.HandleCommand("script my.date()", result) import datetime - self.expect(result.GetOutput(), "script my.date() runs successfully", - exe=False, - substrs=[str(datetime.date.today())]) + if failure: + self.expect(result.GetOutput(), "script my.date() runs successfully", + exe=False, error=True) + else: + self.expect(result.GetOutput(), "script my.date() runs successfully", + exe=False, + substrs=[str(datetime.date.today())]) + + @no_debug_info_test + def test_command_source_relative_error(self): + """Test that 'command source -C' gives an error for a relative path""" + source_dir = self.getSourceDir() + result = lldb.SBCommandReturnObject() + self.runCmd("command source --stop-on-error 1 not-relative.txt") + self.check_results(failure=True) diff --git a/lldb/test/API/commands/command/source/commands2.txt b/lldb/test/API/commands/command/source/commands2.txt new file mode 100644 index 0000000000000..f0850b5d8f172 --- /dev/null +++ b/lldb/test/API/commands/command/source/commands2.txt @@ -0,0 +1 @@ +command source -C subdir/subcmds.txt diff --git a/lldb/test/API/commands/command/source/not-relative.txt b/lldb/test/API/commands/command/source/not-relative.txt new file mode 100644 index 0000000000000..ca90def14c9a6 --- /dev/null +++ b/lldb/test/API/commands/command/source/not-relative.txt @@ -0,0 +1,2 @@ +command source -C /tmp/somefile.txt +script import my diff --git a/lldb/test/API/commands/command/source/subdir/subcmds.txt b/lldb/test/API/commands/command/source/subdir/subcmds.txt new file mode 100644 index 0000000000000..c7e5933ea7f08 --- /dev/null +++ b/lldb/test/API/commands/command/source/subdir/subcmds.txt @@ -0,0 +1 @@ +command source -C ../commands.txt _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits