Skip to content

Commit

Permalink
[lldb/SWIG] Refactor extensions to be non Python-specific (2/2)
Browse files Browse the repository at this point in the history
The current SWIG extensions for the string conversion operator is Python
specific because it uses the PythonObjects. This means that the code
cannot be reused for other SWIG supported languages such as Lua.

This reimplements the extensions in a more generic way that can be
reused. It uses a SWIG macro to reduce code duplication.

Differential revision: https://reviews.llvm.org/D72377
  • Loading branch information
JDevlieghere committed Jan 9, 2020
1 parent 7f1026a commit ae47a3d
Show file tree
Hide file tree
Showing 25 changed files with 95 additions and 287 deletions.
276 changes: 4 additions & 272 deletions lldb/scripts/Python/python-extensions.swig
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
%extend lldb::SBAddress {
%nothreadallow;
PyObject *lldb::SBAddress::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBBlock {
%nothreadallow;
PyObject *lldb::SBBlock::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBBreakpoint {
%nothreadallow;
PyObject *lldb::SBBreakpoint::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;

%pythoncode %{
def __eq__(self, rhs):
if not isinstance(rhs, type(self)):
Expand All @@ -50,34 +12,6 @@

return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}

}
%extend lldb::SBBreakpointLocation {
%nothreadallow;
PyObject *lldb::SBBreakpointLocation::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelFull);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}

%extend lldb::SBBreakpointName {
%nothreadallow;
PyObject *lldb::SBBreakpointName::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}

%extend lldb::SBBroadcaster {
Expand All @@ -97,18 +31,6 @@
}

%extend lldb::SBCommandReturnObject {
%nothreadallow;
PyObject *lldb::SBCommandReturnObject::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;

/* the write() and flush() calls are not part of the SB API proper, and are solely for Python usage
they are meant to make an SBCommandReturnObject into a file-like object so that instructions of the sort
print >>sb_command_return_object, "something"
Expand All @@ -122,18 +44,8 @@
void lldb::SBCommandReturnObject::flush ()
{}
}

%extend lldb::SBCompileUnit {
%nothreadallow;
PyObject *lldb::SBCompileUnit::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
%pythoncode %{
def __eq__(self, rhs):
if not isinstance(rhs, type(self)):
Expand All @@ -148,45 +60,8 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBData {
%nothreadallow;
PyObject *lldb::SBData::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBDebugger {
%nothreadallow;
PyObject *lldb::SBDebugger::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBDeclaration {
%nothreadallow;
PyObject *lldb::SBDeclaration::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;

%extend lldb::SBDeclaration {
%pythoncode %{
def __eq__(self, rhs):
if not isinstance(rhs, type(self)):
Expand All @@ -200,60 +75,9 @@

return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}

}
%extend lldb::SBError {
%nothreadallow;
PyObject *lldb::SBError::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBFileSpec {
%nothreadallow;
PyObject *lldb::SBFileSpec::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBFrame {
%nothreadallow;
PyObject *lldb::SBFrame::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBFunction {
%nothreadallow;
PyObject *lldb::SBFunction::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;

%extend lldb::SBFunction {
%pythoncode %{
def __eq__(self, rhs):
if not isinstance(rhs, type(self)):
Expand All @@ -267,47 +91,9 @@

return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}

}
%extend lldb::SBInstruction {
%nothreadallow;
PyObject *lldb::SBInstruction::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBInstructionList {
%nothreadallow;
PyObject *lldb::SBInstructionList::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}
%extend lldb::SBLineEntry {
%nothreadallow;
PyObject *lldb::SBLineEntry::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;

%extend lldb::SBLineEntry {
%pythoncode %{
def __eq__(self, rhs):
if not isinstance(rhs, type(self)):
Expand All @@ -323,33 +109,7 @@
%}
}

%extend lldb::SBMemoryRegionInfo {
%nothreadallow;
PyObject *lldb::SBMemoryRegionInfo::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}

%extend lldb::SBModule {
%nothreadallow;
PyObject *lldb::SBModule::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;

%pythoncode %{
def __eq__(self, rhs):
if not isinstance(rhs, type(self)):
Expand All @@ -365,34 +125,6 @@
%}
}

%extend lldb::SBModuleSpec {
%nothreadallow;
PyObject *lldb::SBModuleSpec::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}

%extend lldb::SBModuleSpecList {
%nothreadallow;
PyObject *lldb::SBModuleSpecList::__str__ (){
lldb::SBStream description;
$self->GetDescription (description);
const char *desc = description.GetData();
size_t desc_len = description.GetSize();
if (desc_len > 0 && (desc[desc_len-1] == '\n' || desc[desc_len-1] == '\r'))
--desc_len;
return PythonString(llvm::StringRef(desc, desc_len)).release();
}
%clearnothreadallow;
}

%extend lldb::SBProcess {
%nothreadallow;
PyObject *lldb::SBProcess::__str__ (){
Expand Down
2 changes: 2 additions & 0 deletions lldb/scripts/interface/SBAddress.i
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public:
lldb::SBLineEntry
GetLineEntry ();

STRING_EXTENSION(SBAddress)

#ifdef SWIGPYTHON
%pythoncode %{
def __get_load_addr_property__ (self):
Expand Down
2 changes: 2 additions & 0 deletions lldb/scripts/interface/SBBlock.i
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public:
bool locals,
bool statics);

STRING_EXTENSION(SBBlock)

#ifdef SWIGPYTHON
%pythoncode %{
def get_range_at_index(self, idx):
Expand Down
2 changes: 2 additions & 0 deletions lldb/scripts/interface/SBBreakpoint.i
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ public:
bool
IsHardware ();

STRING_EXTENSION(SBBreakpoint)

#ifdef SWIGPYTHON
%pythoncode %{

Expand Down
2 changes: 2 additions & 0 deletions lldb/scripts/interface/SBBreakpointLocation.i
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public:

SBBreakpoint
GetBreakpoint ();

STRING_EXTENSION_LEVEL(SBBreakpointLocation, lldb::eDescriptionLevelFull)
};

} // namespace lldb
1 change: 1 addition & 0 deletions lldb/scripts/interface/SBBreakpointName.i
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public:

bool GetDescription(lldb::SBStream &description);

STRING_EXTENSION(SBBreakpointName)
};

} // namespace lldb
Expand Down
2 changes: 2 additions & 0 deletions lldb/scripts/interface/SBCommandReturnObject.i
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public:
void SetImmediateOutputFile(lldb::FileSP BORROWED);
void SetImmediateErrorFile(lldb::FileSP BORROWED);

STRING_EXTENSION(SBCommandReturnObject)

%extend {
// transfer_ownership does nothing, and is here for compatibility with
// old scripts. Ownership is tracked by reference count in the ordinary way.
Expand Down
Loading

0 comments on commit ae47a3d

Please sign in to comment.