Skip to content

Commit 5fef1c1

Browse files
authored
Merge pull request #2064 from JDevlieghere/🍒/bastille/1197ee35b84e1fe1c1884b3228b95351719fbb09+3b33b41604784f903c7c5c38665d75da93dbf805+00bb397b0dc79fcad27bfe63456a2100039706f2
🍒/bastille/1197ee35b84e1fe1c1884b3228b95351719fbb09+3b33b41604784f903c7c5c38665d75da93dbf805+00bb397b0dc79fcad27bfe63456a2100039706f2
2 parents de224aa + 2bd02ac commit 5fef1c1

File tree

13 files changed

+242
-121
lines changed

13 files changed

+242
-121
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ class CommandInterpreter : public Broadcaster,
541541
bool SaveTranscript(CommandReturnObject &result,
542542
llvm::Optional<std::string> output_file = llvm::None);
543543

544+
FileSpec GetCurrentSourceDir();
545+
544546
protected:
545547
friend class Debugger;
546548

@@ -627,7 +629,13 @@ class CommandInterpreter : public Broadcaster,
627629
ChildrenTruncatedWarningStatus m_truncation_warning; // Whether we truncated
628630
// children and whether
629631
// the user has been told
632+
633+
// FIXME: Stop using this to control adding to the history and then replace
634+
// this with m_command_source_dirs.size().
630635
uint32_t m_command_source_depth;
636+
/// A stack of directory paths. When not empty, the last one is the directory
637+
/// of the file that's currently sourced.
638+
std::vector<FileSpec> m_command_source_dirs;
631639
std::vector<uint32_t> m_command_source_flags;
632640
CommandInterpreterRunResult m_result;
633641

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ class ScriptInterpreter : public PluginInterface {
507507
virtual bool
508508
LoadScriptingModule(const char *filename, bool init_session,
509509
lldb_private::Status &error,
510-
StructuredData::ObjectSP *module_sp = nullptr);
510+
StructuredData::ObjectSP *module_sp = nullptr,
511+
FileSpec extra_search_dir = {});
511512

512513
virtual bool IsReservedWord(const char *word) { return false; }
513514

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,9 @@ class CommandObjectCommandsScriptImport : public CommandObjectParsed {
12501250
case 'r':
12511251
// NO-OP
12521252
break;
1253+
case 'c':
1254+
relative_to_command_file = true;
1255+
break;
12531256
default:
12541257
llvm_unreachable("Unimplemented option");
12551258
}
@@ -1258,11 +1261,13 @@ class CommandObjectCommandsScriptImport : public CommandObjectParsed {
12581261
}
12591262

12601263
void OptionParsingStarting(ExecutionContext *execution_context) override {
1264+
relative_to_command_file = false;
12611265
}
12621266

12631267
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
12641268
return llvm::makeArrayRef(g_script_import_options);
12651269
}
1270+
bool relative_to_command_file = false;
12661271
};
12671272

12681273
bool DoExecute(Args &command, CommandReturnObject &result) override {
@@ -1272,6 +1277,17 @@ class CommandObjectCommandsScriptImport : public CommandObjectParsed {
12721277
return false;
12731278
}
12741279

1280+
FileSpec source_dir = {};
1281+
if (m_options.relative_to_command_file) {
1282+
source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir();
1283+
if (!source_dir) {
1284+
result.AppendError("command script import -c can only be specified "
1285+
"from a command file");
1286+
result.SetStatus(eReturnStatusFailed);
1287+
return false;
1288+
}
1289+
}
1290+
12751291
for (auto &entry : command.entries()) {
12761292
Status error;
12771293

@@ -1286,7 +1302,7 @@ class CommandObjectCommandsScriptImport : public CommandObjectParsed {
12861302
// more)
12871303
m_exe_ctx.Clear();
12881304
if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule(
1289-
entry.c_str(), init_session, error)) {
1305+
entry.c_str(), init_session, error, nullptr, source_dir)) {
12901306
result.SetStatus(eReturnStatusSuccessFinishNoResult);
12911307
} else {
12921308
result.AppendErrorWithFormat("module importing failed: %s",

lldb/source/Commands/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,10 @@ let Command = "script import" in {
704704
Desc<"Allow the script to be loaded even if it was already loaded before. "
705705
"This argument exists for backwards compatibility, but reloading is always "
706706
"allowed, whether you specify it or not.">;
707+
def relative_to_command_file : Option<"relative-to-command-file", "c">,
708+
Group<1>, Desc<"Resolve non-absolute paths relative to the location of the "
709+
"current command file. This argument can only be used when the command is "
710+
"being sourced from a file.">;
707711
}
708712

709713
let Command = "script add" in {

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,11 +2551,15 @@ void CommandInterpreter::HandleCommandsFromFile(
25512551
debugger.SetAsyncExecution(false);
25522552

25532553
m_command_source_depth++;
2554+
m_command_source_dirs.push_back(cmd_file.CopyByRemovingLastPathComponent());
25542555

25552556
debugger.RunIOHandlerSync(io_handler_sp);
25562557
if (!m_command_source_flags.empty())
25572558
m_command_source_flags.pop_back();
2559+
2560+
m_command_source_dirs.pop_back();
25582561
m_command_source_depth--;
2562+
25592563
result.SetStatus(eReturnStatusSuccessFinishNoResult);
25602564
debugger.SetAsyncExecution(old_async_execution);
25612565
}
@@ -2961,6 +2965,12 @@ bool CommandInterpreter::SaveTranscript(
29612965
return true;
29622966
}
29632967

2968+
FileSpec CommandInterpreter::GetCurrentSourceDir() {
2969+
if (m_command_source_dirs.empty())
2970+
return {};
2971+
return m_command_source_dirs.back();
2972+
}
2973+
29642974
void CommandInterpreter::GetLLDBCommandsFromIOHandler(
29652975
const char *prompt, IOHandlerDelegate &delegate, void *baton) {
29662976
Debugger &debugger = GetDebugger();

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ void ScriptInterpreter::CollectDataForWatchpointCommandCallback(
4747
"This script interpreter does not support watchpoint callbacks.");
4848
}
4949

50-
bool ScriptInterpreter::LoadScriptingModule(
51-
const char *filename, bool init_session, lldb_private::Status &error,
52-
StructuredData::ObjectSP *module_sp) {
50+
bool ScriptInterpreter::LoadScriptingModule(const char *filename,
51+
bool init_session,
52+
lldb_private::Status &error,
53+
StructuredData::ObjectSP *module_sp,
54+
FileSpec extra_search_dir) {
5355
error.SetErrorString(
5456
"This script interpreter does not support importing modules.");
5557
return false;

lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void ScriptInterpreterLua::ExecuteInterpreterLoop() {
124124

125125
bool ScriptInterpreterLua::LoadScriptingModule(
126126
const char *filename, bool init_session, lldb_private::Status &error,
127-
StructuredData::ObjectSP *module_sp) {
127+
StructuredData::ObjectSP *module_sp, FileSpec extra_search_dir) {
128128

129129
FileSystem::Instance().Collect(filename);
130130
if (llvm::Error e = m_lua->LoadModule(filename)) {

lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class ScriptInterpreterLua : public ScriptInterpreter {
2525

2626
void ExecuteInterpreterLoop() override;
2727

28-
bool
29-
LoadScriptingModule(const char *filename, bool init_session,
30-
lldb_private::Status &error,
31-
StructuredData::ObjectSP *module_sp = nullptr) override;
28+
bool LoadScriptingModule(const char *filename, bool init_session,
29+
lldb_private::Status &error,
30+
StructuredData::ObjectSP *module_sp = nullptr,
31+
FileSpec extra_search_dir = {}) override;
3232

3333
// Static Functions
3434
static void Initialize();

0 commit comments

Comments
 (0)