jingham created this revision.
jingham added a reviewer: JDevlieghere.
Herald added a subscriber: dang.
jingham requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This option was recently added to "command script import" so that an 
"organizing" command file can find python script files relative to itself.  
It's natural to extend this to command files as well as script source files for 
much the same reason.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110601

Files:
  lldb/source/Commands/CommandObjectCommands.cpp
  lldb/source/Commands/Options.td
  lldb/test/API/commands/command/source/TestCommandSource.py
  lldb/test/API/commands/command/source/commands2.txt
  lldb/test/API/commands/command/source/subdir/subcmds.txt

Index: lldb/test/API/commands/command/source/subdir/subcmds.txt
===================================================================
--- /dev/null
+++ lldb/test/API/commands/command/source/subdir/subcmds.txt
@@ -0,0 +1 @@
+command source -C ../commands.txt
Index: lldb/test/API/commands/command/source/commands2.txt
===================================================================
--- /dev/null
+++ lldb/test/API/commands/command/source/commands2.txt
@@ -0,0 +1 @@
+command source -C subdir/subcmds.txt
Index: lldb/test/API/commands/command/source/TestCommandSource.py
===================================================================
--- lldb/test/API/commands/command/source/TestCommandSource.py
+++ lldb/test/API/commands/command/source/TestCommandSource.py
@@ -21,7 +21,18 @@
         # 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):
         # Python should evaluate "my.date()" successfully.
         command_interpreter = self.dbg.GetCommandInterpreter()
         self.assertTrue(command_interpreter, VALID_COMMAND_INTERPRETER)
Index: lldb/source/Commands/Options.td
===================================================================
--- lldb/source/Commands/Options.td
+++ lldb/source/Commands/Options.td
@@ -536,6 +536,10 @@
     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 {
Index: lldb/source/Commands/CommandObjectCommands.cpp
===================================================================
--- lldb/source/Commands/CommandObjectCommands.cpp
+++ lldb/source/Commands/CommandObjectCommands.cpp
@@ -77,7 +77,7 @@
   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 @@
         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 @@
       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 @@
     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,26 @@
       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 script import -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:
+      // We don't need to check whether cmd_file is relative, since MakeAbsolute
+      // doesn't alter paths that are already relative.
+      // Question, should we make it an error to pass -C and an absolute path?
+      cmd_file.MakeAbsolute(source_dir);
+    }
+
     FileSystem::Instance().Resolve(cmd_file);
 
     CommandInterpreterRunOptions options;
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to