Skip to content

Commit c90ca0c

Browse files
committed
[lldb] Implement WorkingDirectoryProvider in terms of DirectoryProvider (NFC)
Add an abstract base class that can be used to create other directory providers.
1 parent 66c882e commit c90ca0c

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

lldb/include/lldb/Utility/Reproducer.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,46 @@ class VersionProvider : public Provider<VersionProvider> {
138138
static char ID;
139139
};
140140

141-
/// Provider for the LLDB current working directory.
141+
/// Abstract provider to storing directory paths.
142+
template <typename T> class DirectoryProvider : public repro::Provider<T> {
143+
public:
144+
DirectoryProvider(const FileSpec &root) : Provider<T>(root) {}
145+
void SetDirectory(std::string directory) {
146+
m_directory = std::move(directory);
147+
}
148+
llvm::StringRef GetDirectory() { return m_directory; }
149+
150+
void Keep() override {
151+
FileSpec file = this->GetRoot().CopyByAppendingPathComponent(T::Info::file);
152+
std::error_code ec;
153+
llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
154+
if (ec)
155+
return;
156+
os << m_directory << "\n";
157+
}
158+
159+
protected:
160+
std::string m_directory;
161+
};
162+
163+
/// Provider for the current working directory.
142164
///
143165
/// When the reproducer is kept, it writes lldb's current working directory to
144166
/// a file named cwd.txt in the reproducer root.
145-
class WorkingDirectoryProvider : public Provider<WorkingDirectoryProvider> {
167+
class WorkingDirectoryProvider
168+
: public DirectoryProvider<WorkingDirectoryProvider> {
146169
public:
147-
WorkingDirectoryProvider(const FileSpec &directory) : Provider(directory) {
170+
WorkingDirectoryProvider(const FileSpec &directory)
171+
: DirectoryProvider(directory) {
148172
llvm::SmallString<128> cwd;
149173
if (std::error_code EC = llvm::sys::fs::current_path(cwd))
150174
return;
151-
m_cwd = std::string(cwd.str());
175+
SetDirectory(std::string(cwd));
152176
}
153-
154-
void Update(llvm::StringRef path) { m_cwd = std::string(path); }
155-
llvm::StringRef GetWorkingDirectory() { return m_cwd; }
156-
157177
struct Info {
158178
static const char *name;
159179
static const char *file;
160180
};
161-
void Keep() override;
162-
std::string m_cwd;
163181
static char ID;
164182
};
165183

lldb/source/API/SBReproducer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ const char *SBReproducer::GetPath() {
235235
void SBReproducer::SetWorkingDirectory(const char *path) {
236236
if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
237237
auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>();
238-
wp.Update(path);
238+
wp.SetDirectory(path);
239239
auto &fp = g->GetOrCreate<repro::FileProvider>();
240-
fp.RecordInterestingDirectory(wp.GetWorkingDirectory());
240+
fp.RecordInterestingDirectory(wp.GetDirectory());
241241
}
242242
}
243243

lldb/source/Initialization/SystemInitializerCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static llvm::Error InitializeFileSystem() {
7575

7676
repro::WorkingDirectoryProvider &wp =
7777
g->GetOrCreate<repro::WorkingDirectoryProvider>();
78-
fp.RecordInterestingDirectory(wp.GetWorkingDirectory());
78+
fp.RecordInterestingDirectory(wp.GetDirectory());
7979

8080
return llvm::Error::success();
8181
}

lldb/source/Utility/Reproducer.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,6 @@ void VersionProvider::Keep() {
290290
os << m_version << "\n";
291291
}
292292

293-
void WorkingDirectoryProvider::Keep() {
294-
FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
295-
std::error_code ec;
296-
llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
297-
if (ec)
298-
return;
299-
os << m_cwd << "\n";
300-
}
301-
302293
void FileProvider::RecordInterestingDirectory(const llvm::Twine &dir) {
303294
if (m_collector)
304295
m_collector->addFile(dir);

0 commit comments

Comments
 (0)