Skip to content

Commit 6d9b82b

Browse files
committed
[lldb] Add JSON serialization for FileSpec and FileSpecList
Add a `ToJSON` method in FileSpec and OptionValueFileSpecList to enable JSON serialization.
1 parent ab1dcac commit 6d9b82b

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

lldb/include/lldb/Interpreter/OptionValueFileSpecList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class OptionValueFileSpecList
3333
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
3434
uint32_t dump_mask) override;
3535

36+
llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) override;
37+
3638
Status
3739
SetValueFromString(llvm::StringRef value,
3840
VarSetOperationType op = eVarSetOperationAssign) override;

lldb/include/lldb/Utility/FileSpec.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/StringRef.h"
1919
#include "llvm/Support/FileSystem.h"
2020
#include "llvm/Support/FormatVariadic.h"
21+
#include "llvm/Support/JSON.h"
2122
#include "llvm/Support/Path.h"
2223

2324
#include <cstddef>
@@ -214,6 +215,17 @@ class FileSpec {
214215
/// The stream to which to dump the object description.
215216
void Dump(llvm::raw_ostream &s) const;
216217

218+
///
219+
/// Convert the filespec object to a json value.
220+
///
221+
/// Convert the filespec object to a json value. If the object contains a
222+
/// valid directory name, it will be displayed followed by a directory
223+
/// delimiter, and the filename.
224+
///
225+
/// \return
226+
/// A json value representation of a filespec.
227+
llvm::json::Value ToJSON() const;
228+
217229
Style GetPathStyle() const;
218230

219231
/// Directory string const get accessor.

lldb/source/Interpreter/OptionValueFileSpecList.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ void OptionValueFileSpecList::DumpValue(const ExecutionContext *exe_ctx,
4141
}
4242
}
4343

44+
llvm::json::Value
45+
OptionValueFileSpecList::ToJSON(const ExecutionContext *exe_ctx) {
46+
std::lock_guard<std::recursive_mutex> lock(m_mutex);
47+
llvm::json::Array spec_list;
48+
for (const auto &file_spec : m_current_value) {
49+
spec_list.emplace_back(file_spec.ToJSON());
50+
}
51+
return spec_list;
52+
}
53+
4454
Status OptionValueFileSpecList::SetValueFromString(llvm::StringRef value,
4555
VarSetOperationType op) {
4656
std::lock_guard<std::recursive_mutex> lock(m_mutex);

lldb/source/Utility/FileSpec.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ void FileSpec::Dump(llvm::raw_ostream &s) const {
330330
s << path_separator;
331331
}
332332

333+
llvm::json::Value FileSpec::ToJSON() const {
334+
std::string file_spec{};
335+
llvm::raw_string_ostream stream(file_spec);
336+
this->Dump(stream);
337+
return llvm::json::Value(std::move(file_spec));
338+
}
339+
333340
FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
334341

335342
void FileSpec::SetDirectory(ConstString directory) {

0 commit comments

Comments
 (0)