Skip to content

Commit fd6439e

Browse files
authored
Merge pull request #1717 from medismailben/swift/master-rebranch
[lldb/interpreter] Add Swift REPL init file support
2 parents 227d496 + c1bba7c commit fd6439e

File tree

9 files changed

+55
-29
lines changed

9 files changed

+55
-29
lines changed

lldb/docs/man/lldb.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,11 @@ program. This would be ~/.lldbinit-lldb for the command line :program:`lldb`
312312
and ~/.lldbinit-Xcode for Xcode. If there is no application specific init
313313
file, :program:`lldb` will look for an init file in the home directory.
314314
If launched with a `REPL`_ option, it will first look for a REPL configuration
315-
file, specific to the REPL language. If this file doesn't exist, or :program:`lldb`
316-
wasn't launch with `REPL`_, meaning there is neither a REPL init file nor an
317-
application specific init file, `lldb` will fallback to the global ~/.lldbinit.
315+
file, specific to the REPL language. The init file should be named as follow:
316+
`.lldbinit-<language>-repl` (i.e. `.lldbinit-swift-repl`). If this file doesn't
317+
exist, or :program:`lldb` wasn't launch with `REPL`_, meaning there is neither
318+
a REPL init file nor an application specific init file, `lldb` will fallback to
319+
the global ~/.lldbinit.
318320

319321
Secondly, it will look for an .lldbinit file in the current working directory.
320322
For security reasons, :program:`lldb` will print a warning and not source this

lldb/include/lldb/Host/FileSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ class FileSystem {
154154
/// Call into the Host to see if it can help find the file.
155155
bool ResolveExecutableLocation(FileSpec &file_spec);
156156

157+
/// Get the user home directory.
158+
bool GetHomeDirectory(llvm::SmallVectorImpl<char> &path) const;
159+
bool GetHomeDirectory(FileSpec &file_spec) const;
160+
157161
enum EnumerateDirectoryResult {
158162
/// Enumerate next entry in the current directory.
159163
eEnumerateDirectoryResultNext,

lldb/source/API/SBHostOS.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ SBFileSpec SBHostOS::GetUserHomeDirectory() {
9696
LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBFileSpec, SBHostOS,
9797
GetUserHomeDirectory);
9898

99-
SBFileSpec sb_fspec;
100-
101-
llvm::SmallString<64> home_dir_path;
102-
llvm::sys::path::home_directory(home_dir_path);
103-
FileSpec homedir(home_dir_path.c_str());
99+
FileSpec homedir;
100+
FileSystem::Instance().GetHomeDirectory(homedir);
104101
FileSystem::Instance().Resolve(homedir);
105102

103+
SBFileSpec sb_fspec;
106104
sb_fspec.SetFileSpec(homedir);
105+
107106
return LLDB_RECORD_RESULT(sb_fspec);
108107
}
109108

lldb/source/Host/common/Editline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class EditlineHistory {
207207
// Compute the history path lazily.
208208
if (m_path.empty() && m_history && !m_prefix.empty()) {
209209
llvm::SmallString<128> lldb_history_file;
210-
llvm::sys::path::home_directory(lldb_history_file);
210+
FileSystem::Instance().GetHomeDirectory(lldb_history_file);
211211
llvm::sys::path::append(lldb_history_file, ".lldb");
212212

213213
// LLDB stores its history in ~/.lldb/. If for some reason this directory

lldb/source/Host/common/FileSystem.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
360360
return true;
361361
}
362362

363+
bool FileSystem::GetHomeDirectory(SmallVectorImpl<char> &path) const {
364+
return llvm::sys::path::home_directory(path);
365+
}
366+
367+
bool FileSystem::GetHomeDirectory(FileSpec &file_spec) const {
368+
SmallString<128> home_dir;
369+
if (!GetHomeDirectory(home_dir))
370+
return false;
371+
file_spec.SetPath(home_dir);
372+
return true;
373+
}
374+
363375
static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
364376
int mode) {
365377
return const_cast<FileSystem &>(fs).Open(path, flags, mode);

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "lldb/Interpreter/Property.h"
6868
#include "lldb/Utility/Args.h"
6969

70+
#include "lldb/Target/Language.h"
7071
#include "lldb/Target/Process.h"
7172
#include "lldb/Target/StopInfo.h"
7273
#include "lldb/Target/TargetList.h"
@@ -2087,25 +2088,27 @@ static void GetHomeInitFile(llvm::SmallVectorImpl<char> &init_file,
20872088
init_file_name.append(suffix.str());
20882089
}
20892090

2090-
llvm::sys::path::home_directory(init_file);
2091+
FileSystem::Instance().GetHomeDirectory(init_file);
20912092
llvm::sys::path::append(init_file, init_file_name);
20922093

20932094
FileSystem::Instance().Resolve(init_file);
20942095
}
20952096

2096-
static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file,
2097-
LanguageType language) {
2098-
std::string init_file_name;
2099-
2100-
switch (language) {
2101-
// TODO: Add support for a language used with a REPL.
2102-
default:
2097+
static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file) {
2098+
LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
2099+
LanguageType language = eLanguageTypeUnknown;
2100+
if (auto main_repl_language = repl_languages.GetSingularLanguage())
2101+
language = *main_repl_language;
2102+
else
21032103
return;
2104-
}
21052104

2106-
llvm::sys::path::home_directory(init_file);
2105+
std::string init_file_name =
2106+
(llvm::Twine(".lldbinit-") +
2107+
llvm::Twine(Language::GetNameForLanguageType(language)) +
2108+
llvm::Twine("-repl"))
2109+
.str();
2110+
FileSystem::Instance().GetHomeDirectory(init_file);
21072111
llvm::sys::path::append(init_file, init_file_name);
2108-
21092112
FileSystem::Instance().Resolve(init_file);
21102113
}
21112114

@@ -2194,13 +2197,8 @@ void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result,
21942197

21952198
llvm::SmallString<128> init_file;
21962199

2197-
if (is_repl) {
2198-
LanguageType language = {};
2199-
TargetSP target_sp = GetDebugger().GetSelectedTarget();
2200-
if (target_sp)
2201-
language = target_sp->GetLanguage();
2202-
GetHomeREPLInitFile(init_file, language);
2203-
}
2200+
if (is_repl)
2201+
GetHomeREPLInitFile(init_file);
22042202

22052203
if (init_file.empty())
22062204
GetHomeInitFile(init_file);

lldb/source/Target/Platform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ PlatformProperties::PlatformProperties() {
8585
return;
8686

8787
llvm::SmallString<64> user_home_dir;
88-
if (!llvm::sys::path::home_directory(user_home_dir))
88+
if (!FileSystem::Instance().GetHomeDirectory(user_home_dir))
8989
return;
9090

9191
module_cache_dir = FileSpec(user_home_dir.c_str());
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Test that the Swift REPL init file works.
2+
// REQUIRES: system-darwin
3+
// RUN: export HOME=%t
4+
// RUN: mkdir -p %t
5+
// RUN: echo 'br set -f main.c -l 123' > ~/.lldbinit
6+
// RUN: echo 'br set -f swift-repl.c -l 456' > ~/.lldbinit-swift-repl
7+
// RUN: %lldb-init --repl < %s 2>&1 | FileCheck %s
8+
9+
:br list
10+
// CHECK: Current breakpoints
11+
// CHECK-NEXT: file = 'swift-repl.c', line = 456, exact_match = 0, locations = 0 (pending)

lldb/tools/driver/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ int Driver::MainLoop() {
491491
SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
492492

493493
// Before we handle any options from the command line, we parse the
494-
// .lldbinit file in the user's home directory.
494+
// REPL init file or the default file in the user's home directory.
495495
SBCommandReturnObject result;
496496
sb_interpreter.SourceInitFileInHomeDirectory(result, m_option_data.m_repl);
497497
if (m_option_data.m_debug_mode) {

0 commit comments

Comments
 (0)