Skip to content

Commit ed17b6f

Browse files
committed
[lldb] Extract FileSystem initialization code into helper (NFC)
The FileSystem initialization depends on the reproducer mode. It has been growing organically to the point where it deserves its own helper function. This also allows for early returns to simplify the code.
1 parent b587ca9 commit ed17b6f

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

lldb/source/Initialization/SystemInitializerCommon.cpp

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,51 @@ SystemInitializerCommon::SystemInitializerCommon() {}
3939

4040
SystemInitializerCommon::~SystemInitializerCommon() {}
4141

42+
/// Initialize the FileSystem based on the current reproducer mode.
43+
static llvm::Error InitializeFileSystem() {
44+
auto &r = repro::Reproducer::Instance();
45+
if (repro::Loader *loader = r.GetLoader()) {
46+
FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
47+
if (vfs_mapping) {
48+
if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
49+
return e;
50+
} else {
51+
FileSystem::Initialize();
52+
}
53+
54+
llvm::Expected<std::string> cwd =
55+
loader->LoadBuffer<WorkingDirectoryProvider>();
56+
if (!cwd)
57+
return cwd.takeError();
58+
59+
llvm::StringRef working_dir = llvm::StringRef(*cwd).rtrim();
60+
if (std::error_code ec = FileSystem::Instance()
61+
.GetVirtualFileSystem()
62+
->setCurrentWorkingDirectory(working_dir)) {
63+
return llvm::errorCodeToError(ec);
64+
}
65+
66+
return llvm::Error::success();
67+
}
68+
69+
if (repro::Generator *g = r.GetGenerator()) {
70+
repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>();
71+
vp.SetVersion(lldb_private::GetVersion());
72+
73+
repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
74+
FileSystem::Initialize(fp.GetFileCollector());
75+
76+
repro::WorkingDirectoryProvider &wp =
77+
g->GetOrCreate<repro::WorkingDirectoryProvider>();
78+
fp.RecordInterestingDirectory(wp.GetWorkingDirectory());
79+
80+
return llvm::Error::success();
81+
}
82+
83+
FileSystem::Initialize();
84+
return llvm::Error::success();
85+
}
86+
4287
llvm::Error SystemInitializerCommon::Initialize() {
4388
#if defined(_WIN32)
4489
const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
@@ -69,36 +114,8 @@ llvm::Error SystemInitializerCommon::Initialize() {
69114
return e;
70115
}
71116

72-
auto &r = repro::Reproducer::Instance();
73-
if (repro::Loader *loader = r.GetLoader()) {
74-
FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
75-
if (vfs_mapping) {
76-
if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
77-
return e;
78-
} else {
79-
FileSystem::Initialize();
80-
}
81-
if (llvm::Expected<std::string> cwd =
82-
loader->LoadBuffer<WorkingDirectoryProvider>()) {
83-
llvm::StringRef working_dir = llvm::StringRef(*cwd).rtrim();
84-
if (std::error_code ec = FileSystem::Instance()
85-
.GetVirtualFileSystem()
86-
->setCurrentWorkingDirectory(working_dir)) {
87-
return llvm::errorCodeToError(ec);
88-
}
89-
} else {
90-
return cwd.takeError();
91-
}
92-
} else if (repro::Generator *g = r.GetGenerator()) {
93-
repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>();
94-
vp.SetVersion(lldb_private::GetVersion());
95-
repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
96-
FileSystem::Initialize(fp.GetFileCollector());
97-
repro::WorkingDirectoryProvider &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>();
98-
fp.RecordInterestingDirectory(wp.GetWorkingDirectory());
99-
} else {
100-
FileSystem::Initialize();
101-
}
117+
if (auto e = InitializeFileSystem())
118+
return e;
102119

103120
Log::Initialize();
104121
HostInfo::Initialize();

0 commit comments

Comments
 (0)