Skip to content

Commit dd0f3b2

Browse files
Merge pull request #9784 from adrian-prantl/cherry-pick-swift-release-6.1-lldb-Expose-structured-errors-in-SBError-120784
[Cherry-pick into swift/release/6.1] [lldb] Expose structured errors in SBError (llvm#120784)
2 parents 38c9bd4 + 6ddc07c commit dd0f3b2

File tree

12 files changed

+233
-118
lines changed

12 files changed

+233
-118
lines changed

lldb/bindings/python/static-binding/LLDBWrapPython.cpp

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30287,6 +30287,34 @@ SWIGINTERN PyObject *_wrap_SBError_GetError(PyObject *self, PyObject *args) {
3028730287
}
3028830288

3028930289

30290+
SWIGINTERN PyObject *_wrap_SBError_GetErrorData(PyObject *self, PyObject *args) {
30291+
PyObject *resultobj = 0;
30292+
lldb::SBError *arg1 = (lldb::SBError *) 0 ;
30293+
void *argp1 = 0 ;
30294+
int res1 = 0 ;
30295+
PyObject *swig_obj[1] ;
30296+
lldb::SBStructuredData result;
30297+
30298+
(void)self;
30299+
if (!args) SWIG_fail;
30300+
swig_obj[0] = args;
30301+
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBError, 0 | 0 );
30302+
if (!SWIG_IsOK(res1)) {
30303+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBError_GetErrorData" "', argument " "1"" of type '" "lldb::SBError const *""'");
30304+
}
30305+
arg1 = reinterpret_cast< lldb::SBError * >(argp1);
30306+
{
30307+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
30308+
result = ((lldb::SBError const *)arg1)->GetErrorData();
30309+
SWIG_PYTHON_THREAD_END_ALLOW;
30310+
}
30311+
resultobj = SWIG_NewPointerObj((new lldb::SBStructuredData(result)), SWIGTYPE_p_lldb__SBStructuredData, SWIG_POINTER_OWN | 0 );
30312+
return resultobj;
30313+
fail:
30314+
return NULL;
30315+
}
30316+
30317+
3029030318
SWIGINTERN PyObject *_wrap_SBError_GetType(PyObject *self, PyObject *args) {
3029130319
PyObject *resultobj = 0;
3029230320
lldb::SBError *arg1 = (lldb::SBError *) 0 ;
@@ -37422,34 +37450,6 @@ SWIGINTERN PyObject *_wrap_SBFrame_IsSwiftThunk(PyObject *self, PyObject *args)
3742237450
}
3742337451

3742437452

37425-
SWIGINTERN PyObject *_wrap_SBFrame_GetLanguageSpecificData(PyObject *self, PyObject *args) {
37426-
PyObject *resultobj = 0;
37427-
lldb::SBFrame *arg1 = (lldb::SBFrame *) 0 ;
37428-
void *argp1 = 0 ;
37429-
int res1 = 0 ;
37430-
PyObject *swig_obj[1] ;
37431-
lldb::SBStructuredData result;
37432-
37433-
(void)self;
37434-
if (!args) SWIG_fail;
37435-
swig_obj[0] = args;
37436-
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBFrame, 0 | 0 );
37437-
if (!SWIG_IsOK(res1)) {
37438-
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBFrame_GetLanguageSpecificData" "', argument " "1"" of type '" "lldb::SBFrame const *""'");
37439-
}
37440-
arg1 = reinterpret_cast< lldb::SBFrame * >(argp1);
37441-
{
37442-
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
37443-
result = ((lldb::SBFrame const *)arg1)->GetLanguageSpecificData();
37444-
SWIG_PYTHON_THREAD_END_ALLOW;
37445-
}
37446-
resultobj = SWIG_NewPointerObj((new lldb::SBStructuredData(result)), SWIGTYPE_p_lldb__SBStructuredData, SWIG_POINTER_OWN | 0 );
37447-
return resultobj;
37448-
fail:
37449-
return NULL;
37450-
}
37451-
37452-
3745337453
SWIGINTERN PyObject *_wrap_SBFrame_IsInlined__SWIG_0(PyObject *self, Py_ssize_t nobjs, PyObject **swig_obj) {
3745437454
PyObject *resultobj = 0;
3745537455
lldb::SBFrame *arg1 = (lldb::SBFrame *) 0 ;
@@ -37931,6 +37931,34 @@ SWIGINTERN PyObject *_wrap_SBFrame_EvaluateExpression(PyObject *self, PyObject *
3793137931
}
3793237932

3793337933

37934+
SWIGINTERN PyObject *_wrap_SBFrame_GetLanguageSpecificData(PyObject *self, PyObject *args) {
37935+
PyObject *resultobj = 0;
37936+
lldb::SBFrame *arg1 = (lldb::SBFrame *) 0 ;
37937+
void *argp1 = 0 ;
37938+
int res1 = 0 ;
37939+
PyObject *swig_obj[1] ;
37940+
lldb::SBStructuredData result;
37941+
37942+
(void)self;
37943+
if (!args) SWIG_fail;
37944+
swig_obj[0] = args;
37945+
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBFrame, 0 | 0 );
37946+
if (!SWIG_IsOK(res1)) {
37947+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBFrame_GetLanguageSpecificData" "', argument " "1"" of type '" "lldb::SBFrame const *""'");
37948+
}
37949+
arg1 = reinterpret_cast< lldb::SBFrame * >(argp1);
37950+
{
37951+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
37952+
result = ((lldb::SBFrame const *)arg1)->GetLanguageSpecificData();
37953+
SWIG_PYTHON_THREAD_END_ALLOW;
37954+
}
37955+
resultobj = SWIG_NewPointerObj((new lldb::SBStructuredData(result)), SWIGTYPE_p_lldb__SBStructuredData, SWIG_POINTER_OWN | 0 );
37956+
return resultobj;
37957+
fail:
37958+
return NULL;
37959+
}
37960+
37961+
3793437962
SWIGINTERN PyObject *_wrap_SBFrame_GetFrameBlock(PyObject *self, PyObject *args) {
3793537963
PyObject *resultobj = 0;
3793637964
lldb::SBFrame *arg1 = (lldb::SBFrame *) 0 ;
@@ -96340,6 +96368,7 @@ static PyMethodDef SwigMethods[] = {
9634096368
{ "SBError_Fail", _wrap_SBError_Fail, METH_O, "SBError_Fail(SBError self) -> bool"},
9634196369
{ "SBError_Success", _wrap_SBError_Success, METH_O, "SBError_Success(SBError self) -> bool"},
9634296370
{ "SBError_GetError", _wrap_SBError_GetError, METH_O, "SBError_GetError(SBError self) -> uint32_t"},
96371+
{ "SBError_GetErrorData", _wrap_SBError_GetErrorData, METH_O, "SBError_GetErrorData(SBError self) -> SBStructuredData"},
9634396372
{ "SBError_GetType", _wrap_SBError_GetType, METH_O, "SBError_GetType(SBError self) -> lldb::ErrorType"},
9634496373
{ "SBError_SetError", _wrap_SBError_SetError, METH_VARARGS, "SBError_SetError(SBError self, uint32_t err, lldb::ErrorType type)"},
9634596374
{ "SBError_SetErrorToErrno", _wrap_SBError_SetErrorToErrno, METH_O, "SBError_SetErrorToErrno(SBError self)"},
@@ -96654,7 +96683,6 @@ static PyMethodDef SwigMethods[] = {
9665496683
" .\n"
9665596684
""},
9665696685
{ "SBFrame_IsSwiftThunk", _wrap_SBFrame_IsSwiftThunk, METH_O, "SBFrame_IsSwiftThunk(SBFrame self) -> bool"},
96657-
{ "SBFrame_GetLanguageSpecificData", _wrap_SBFrame_GetLanguageSpecificData, METH_O, "SBFrame_GetLanguageSpecificData(SBFrame self) -> SBStructuredData"},
9665896686
{ "SBFrame_IsInlined", _wrap_SBFrame_IsInlined, METH_VARARGS, "\n"
9665996687
"SBFrame_IsInlined(SBFrame self) -> bool\n"
9666096688
"SBFrame_IsInlined(SBFrame self) -> bool\n"
@@ -96681,6 +96709,7 @@ static PyMethodDef SwigMethods[] = {
9668196709
" The version that doesn't supply a 'use_dynamic' value will use the\n"
9668296710
" target's default.\n"
9668396711
""},
96712+
{ "SBFrame_GetLanguageSpecificData", _wrap_SBFrame_GetLanguageSpecificData, METH_O, "SBFrame_GetLanguageSpecificData(SBFrame self) -> SBStructuredData"},
9668496713
{ "SBFrame_GetFrameBlock", _wrap_SBFrame_GetFrameBlock, METH_O, "\n"
9668596714
"SBFrame_GetFrameBlock(SBFrame self) -> SBBlock\n"
9668696715
"\n"
@@ -102332,6 +102361,8 @@ SWIG_init(void) {
102332102361
SWIG_Python_SetConstant(d, "eArgTypeRemotePath",SWIG_From_int(static_cast< int >(lldb::eArgTypeRemotePath)));
102333102362
SWIG_Python_SetConstant(d, "eArgTypeRemoteFilename",SWIG_From_int(static_cast< int >(lldb::eArgTypeRemoteFilename)));
102334102363
SWIG_Python_SetConstant(d, "eArgTypeModule",SWIG_From_int(static_cast< int >(lldb::eArgTypeModule)));
102364+
SWIG_Python_SetConstant(d, "eArgTypeCPUName",SWIG_From_int(static_cast< int >(lldb::eArgTypeCPUName)));
102365+
SWIG_Python_SetConstant(d, "eArgTypeCPUFeatures",SWIG_From_int(static_cast< int >(lldb::eArgTypeCPUFeatures)));
102335102366
SWIG_Python_SetConstant(d, "eArgTypeLastArg",SWIG_From_int(static_cast< int >(lldb::eArgTypeLastArg)));
102336102367
SWIG_Python_SetConstant(d, "eSymbolTypeAny",SWIG_From_int(static_cast< int >(lldb::eSymbolTypeAny)));
102337102368
SWIG_Python_SetConstant(d, "eSymbolTypeInvalid",SWIG_From_int(static_cast< int >(lldb::eSymbolTypeInvalid)));

lldb/bindings/python/static-binding/lldb.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,10 @@ def lldb_iter(obj, getsize, getelem):
10171017

10181018
eArgTypeModule = _lldb.eArgTypeModule
10191019

1020+
eArgTypeCPUName = _lldb.eArgTypeCPUName
1021+
1022+
eArgTypeCPUFeatures = _lldb.eArgTypeCPUFeatures
1023+
10201024
eArgTypeLastArg = _lldb.eArgTypeLastArg
10211025

10221026
eSymbolTypeAny = _lldb.eSymbolTypeAny
@@ -5425,6 +5429,10 @@ def GetError(self):
54255429
r"""GetError(SBError self) -> uint32_t"""
54265430
return _lldb.SBError_GetError(self)
54275431

5432+
def GetErrorData(self):
5433+
r"""GetErrorData(SBError self) -> SBStructuredData"""
5434+
return _lldb.SBError_GetErrorData(self)
5435+
54285436
def GetType(self):
54295437
r"""GetType(SBError self) -> lldb::ErrorType"""
54305438
return _lldb.SBError_GetType(self)
@@ -6435,10 +6443,6 @@ def IsSwiftThunk(self):
64356443
r"""IsSwiftThunk(SBFrame self) -> bool"""
64366444
return _lldb.SBFrame_IsSwiftThunk(self)
64376445

6438-
def GetLanguageSpecificData(self):
6439-
r"""GetLanguageSpecificData(SBFrame self) -> SBStructuredData"""
6440-
return _lldb.SBFrame_GetLanguageSpecificData(self)
6441-
64426446
def IsInlined(self, *args):
64436447
r"""
64446448
IsInlined(SBFrame self) -> bool
@@ -6477,6 +6481,10 @@ def EvaluateExpression(self, *args):
64776481
"""
64786482
return _lldb.SBFrame_EvaluateExpression(self, *args)
64796483

6484+
def GetLanguageSpecificData(self):
6485+
r"""GetLanguageSpecificData(SBFrame self) -> SBStructuredData"""
6486+
return _lldb.SBFrame_GetLanguageSpecificData(self)
6487+
64806488
def GetFrameBlock(self):
64816489
r"""
64826490
GetFrameBlock(SBFrame self) -> SBBlock

lldb/include/lldb/API/SBError.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ class LLDB_API SBError {
4444

4545
bool Success() const;
4646

47+
/// Get the error code.
4748
uint32_t GetError() const;
4849

50+
/// Get the error in machine-readable form. Particularly useful for
51+
/// compiler diagnostics.
52+
SBStructuredData GetErrorData() const;
53+
4954
lldb::ErrorType GetType() const;
5055

5156
void SetError(uint32_t err, lldb::ErrorType type);

lldb/include/lldb/API/SBStructuredData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class SBStructuredData {
115115
friend class SBLaunchInfo;
116116
friend class SBDebugger;
117117
friend class SBFrame;
118+
friend class SBError;
118119
friend class SBTarget;
119120
friend class SBProcess;
120121
friend class SBThread;

lldb/include/lldb/Utility/DiagnosticsRendering.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,25 @@ struct DiagnosticDetail {
5757
std::string rendered;
5858
};
5959

60+
StructuredData::ObjectSP Serialize(llvm::ArrayRef<DiagnosticDetail> details);
61+
62+
void RenderDiagnosticDetails(Stream &stream,
63+
std::optional<uint16_t> offset_in_command,
64+
bool show_inline,
65+
llvm::ArrayRef<DiagnosticDetail> details);
66+
6067
class DiagnosticError
6168
: public llvm::ErrorInfo<DiagnosticError, CloneableECError> {
6269
public:
6370
using llvm::ErrorInfo<DiagnosticError, CloneableECError>::ErrorInfo;
6471
DiagnosticError(std::error_code ec) : ErrorInfo(ec) {}
6572
lldb::ErrorType GetErrorType() const override;
6673
virtual llvm::ArrayRef<DiagnosticDetail> GetDetails() const = 0;
74+
StructuredData::ObjectSP GetAsStructuredData() const override {
75+
return Serialize(GetDetails());
76+
}
6777
static char ID;
6878
};
6979

70-
void RenderDiagnosticDetails(Stream &stream,
71-
std::optional<uint16_t> offset_in_command,
72-
bool show_inline,
73-
llvm::ArrayRef<DiagnosticDetail> details);
7480
} // namespace lldb_private
7581
#endif

lldb/include/lldb/Utility/Status.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_UTILITY_STATUS_H
1111

1212
#include "lldb/Utility/FileSpec.h"
13+
#include "lldb/Utility/StructuredData.h"
1314
#include "lldb/lldb-defines.h"
1415
#include "lldb/lldb-enumerations.h"
1516
#include "llvm/ADT/StringRef.h"
@@ -38,6 +39,7 @@ class CloneableError
3839
CloneableError() : ErrorInfo() {}
3940
virtual std::unique_ptr<CloneableError> Clone() const = 0;
4041
virtual lldb::ErrorType GetErrorType() const = 0;
42+
virtual StructuredData::ObjectSP GetAsStructuredData() const = 0;
4143
static char ID;
4244
};
4345

@@ -49,6 +51,7 @@ class CloneableECError
4951
std::error_code convertToErrorCode() const override { return EC; }
5052
void log(llvm::raw_ostream &OS) const override { OS << EC.message(); }
5153
lldb::ErrorType GetErrorType() const override;
54+
virtual StructuredData::ObjectSP GetAsStructuredData() const override;
5255
static char ID;
5356

5457
protected:
@@ -183,6 +186,9 @@ class Status {
183186
/// NULL otherwise.
184187
const char *AsCString(const char *default_error_str = "unknown error") const;
185188

189+
/// Get the error in machine-readable form.
190+
StructuredData::ObjectSP GetAsStructuredData() const;
191+
186192
/// Clear the object state.
187193
///
188194
/// Reverts the state of this object to contain a generic success value and

lldb/source/API/SBError.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "lldb/API/SBError.h"
1010
#include "Utils.h"
1111
#include "lldb/API/SBStream.h"
12+
#include "lldb/API/SBStructuredData.h"
13+
#include "lldb/Core/StructuredDataImpl.h"
1214
#include "lldb/Utility/Instrumentation.h"
1315
#include "lldb/Utility/Status.h"
1416
#include "lldb/Utility/VASPrintf.h"
@@ -97,6 +99,18 @@ uint32_t SBError::GetError() const {
9799
return err;
98100
}
99101

102+
SBStructuredData SBError::GetErrorData() const {
103+
LLDB_INSTRUMENT_VA(this);
104+
105+
SBStructuredData sb_data;
106+
if (!m_opaque_up)
107+
return sb_data;
108+
109+
StructuredData::ObjectSP data(m_opaque_up->GetAsStructuredData());
110+
sb_data.m_impl_up->SetObjectSP(data);
111+
return sb_data;
112+
}
113+
100114
ErrorType SBError::GetType() const {
101115
LLDB_INSTRUMENT_VA(this);
102116

lldb/source/Interpreter/CommandReturnObject.cpp

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -145,57 +145,7 @@ std::string CommandReturnObject::GetErrorString(bool with_diagnostics) {
145145
}
146146

147147
StructuredData::ObjectSP CommandReturnObject::GetErrorData() {
148-
auto make_array = []() { return std::make_unique<StructuredData::Array>(); };
149-
auto make_bool = [](bool b) {
150-
return std::make_unique<StructuredData::Boolean>(b);
151-
};
152-
auto make_dict = []() {
153-
return std::make_unique<StructuredData::Dictionary>();
154-
};
155-
auto make_int = [](unsigned i) {
156-
return std::make_unique<StructuredData::UnsignedInteger>(i);
157-
};
158-
auto make_string = [](llvm::StringRef s) {
159-
return std::make_unique<StructuredData::String>(s);
160-
};
161-
auto dict_up = make_dict();
162-
dict_up->AddItem("version", make_int(1));
163-
auto array_up = make_array();
164-
for (const DiagnosticDetail &diag : m_diagnostics) {
165-
auto detail_up = make_dict();
166-
if (auto &sloc = diag.source_location) {
167-
auto sloc_up = make_dict();
168-
sloc_up->AddItem("file", make_string(sloc->file.GetPath()));
169-
sloc_up->AddItem("line", make_int(sloc->line));
170-
sloc_up->AddItem("length", make_int(sloc->length));
171-
sloc_up->AddItem("hidden", make_bool(sloc->hidden));
172-
sloc_up->AddItem("in_user_input", make_bool(sloc->in_user_input));
173-
detail_up->AddItem("source_location", std::move(sloc_up));
174-
}
175-
llvm::StringRef severity = "unknown";
176-
switch (diag.severity) {
177-
case lldb::eSeverityError:
178-
severity = "error";
179-
break;
180-
case lldb::eSeverityWarning:
181-
severity = "warning";
182-
break;
183-
case lldb::eSeverityInfo:
184-
severity = "note";
185-
break;
186-
}
187-
detail_up->AddItem("severity", make_string(severity));
188-
detail_up->AddItem("message", make_string(diag.message));
189-
detail_up->AddItem("rendered", make_string(diag.rendered));
190-
array_up->AddItem(std::move(detail_up));
191-
}
192-
dict_up->AddItem("details", std::move(array_up));
193-
if (auto stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex)) {
194-
auto text = std::static_pointer_cast<StreamString>(stream_sp)->GetString();
195-
if (!text.empty())
196-
dict_up->AddItem("text", make_string(text));
197-
}
198-
return dict_up;
148+
return Serialize(m_diagnostics);
199149
}
200150

201151
// Similar to AppendError, but do not prepend 'Status: ' to message, and don't

0 commit comments

Comments
 (0)