[lldb/SWIG] Refactor extensions to be non Python-specific (3/3)

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
This commit is contained in:
Jonas Devlieghere 2020-01-08 20:56:11 -08:00
parent d48ac7d54d
commit 51bdd98b8a
18 changed files with 59 additions and 253 deletions

View File

@ -125,32 +125,7 @@
%}
}
%extend lldb::SBProcess {
%nothreadallow;
PyObject *lldb::SBProcess::__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::SBSection {
%nothreadallow;
PyObject *lldb::SBSection::__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)):
@ -180,17 +155,6 @@
{}
}
%extend lldb::SBSymbol {
%nothreadallow;
PyObject *lldb::SBSymbol::__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)):
@ -205,32 +169,6 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBSymbolContext {
%nothreadallow;
PyObject *lldb::SBSymbolContext::__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::SBSymbolContextList {
%nothreadallow;
PyObject *lldb::SBSymbolContextList::__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::SBTarget {
%pythoncode %{
@ -248,44 +186,7 @@
%}
}
%extend lldb::SBType {
%nothreadallow;
PyObject *lldb::SBType::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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::SBTypeCategory {
%nothreadallow;
PyObject *lldb::SBTypeCategory::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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::SBTypeFilter {
%nothreadallow;
PyObject *lldb::SBTypeFilter::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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)):
@ -300,70 +201,8 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBTypeFormat {
%nothreadallow;
PyObject *lldb::SBTypeFormat::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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::SBTypeMember {
%nothreadallow;
PyObject *lldb::SBTypeMember::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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::SBTypeMemberFunction {
%nothreadallow;
PyObject *lldb::SBTypeMemberFunction::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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::SBTypeEnumMember {
%nothreadallow;
PyObject *lldb::SBTypeEnumMember::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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::SBTypeNameSpecifier {
%nothreadallow;
PyObject *lldb::SBTypeNameSpecifier::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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)):
@ -378,18 +217,8 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBTypeSummary {
%nothreadallow;
PyObject *lldb::SBTypeSummary::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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)):
@ -404,18 +233,8 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBTypeSynthetic {
%nothreadallow;
PyObject *lldb::SBTypeSynthetic::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelBrief);
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)):
@ -430,18 +249,8 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBThread {
%nothreadallow;
PyObject *lldb::SBThread::__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)):
@ -456,64 +265,6 @@
return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
%}
}
%extend lldb::SBValue {
%nothreadallow;
PyObject *lldb::SBValue::__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::SBValueList {
%nothreadallow;
PyObject *lldb::SBValueList::__str__ (){
lldb::SBStream description;
const size_t n = $self->GetSize();
if (n)
{
for (size_t i=0; i<n; ++i)
$self->GetValueAtIndex(i).GetDescription(description);
}
else
{
description.Printf("<empty> lldb.SBValueList()");
}
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::SBWatchpoint {
%nothreadallow;
PyObject *lldb::SBWatchpoint::__str__ (){
lldb::SBStream description;
$self->GetDescription (description, lldb::eDescriptionLevelVerbose);
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 {
// // FIXME: We can't get the callback and baton
// PyObject *lldb::SBDebugger (){
// // Only call Py_XDECREF if we have a Python object (or NULL)
// if (LLDBSwigPythonCallPythonLogOutputCallback == $self->GetLogOutPutCallback())
// Py_XDECREF($self->GetCallbackBaton());
// }
// }
%pythoncode %{

View File

@ -417,6 +417,8 @@ public:
lldb::SBProcessInfo
GetProcessInfo();
STRING_EXTENSION(SBProcess)
#ifdef SWIGPYTHON
%pythoncode %{
def __get_is_alive__(self):

View File

@ -114,6 +114,8 @@ public:
bool
operator != (const lldb::SBSection &rhs);
STRING_EXTENSION(SBSection)
#ifdef SWIGPYTHON
%pythoncode %{
def __iter__(self):

View File

@ -72,6 +72,8 @@ public:
bool
operator != (const lldb::SBSymbol &rhs) const;
STRING_EXTENSION(SBSymbol)
#ifdef SWIGPYTHON
%pythoncode %{
def get_instructions_from_current_target (self):

View File

@ -81,6 +81,7 @@ public:
bool
GetDescription (lldb::SBStream &description);
STRING_EXTENSION(SBSymbolContext)
#ifdef SWIGPYTHON
%pythoncode %{

View File

@ -60,6 +60,8 @@ public:
void
Clear();
STRING_EXTENSION(SBSymbolContextList)
#ifdef SWIGPYTHON
%pythoncode %{
def __iter__(self):

View File

@ -402,6 +402,8 @@ public:
bool
SafeToCallFunctions ();
STRING_EXTENSION(SBThread)
#ifdef SWIGPYTHON
%pythoncode %{
def __iter__(self):

View File

@ -43,6 +43,8 @@ public:
uint32_t
GetBitfieldSizeInBits();
STRING_EXTENSION_LEVEL(SBTypeMember, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
name = property(GetName, None, doc='''A read only property that returns the name for this member as a string.''')
@ -100,6 +102,7 @@ public:
GetDescription (lldb::SBStream &description,
lldb::DescriptionLevel description_level);
STRING_EXTENSION_LEVEL(SBTypeMemberFunction, lldb::eDescriptionLevelBrief)
protected:
lldb::TypeMemberFunctionImplSP m_opaque_sp;
};
@ -314,6 +317,8 @@ public:
bool operator!=(lldb::SBType &rhs);
STRING_EXTENSION_LEVEL(SBType, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
def template_arg_array(self):

View File

@ -124,6 +124,8 @@ namespace lldb {
bool
DeleteTypeSynthetic (lldb::SBTypeNameSpecifier);
STRING_EXTENSION_LEVEL(SBTypeCategory, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{

View File

@ -43,6 +43,7 @@ public:
GetDescription (lldb::SBStream &description,
lldb::DescriptionLevel description_level);
STRING_EXTENSION_LEVEL(SBTypeEnumMember, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
name = property(GetName, None, doc='''A read only property that returns the name for this enum member as a string.''')

View File

@ -61,6 +61,8 @@ namespace lldb {
bool
operator != (lldb::SBTypeFilter &rhs);
STRING_EXTENSION_LEVEL(SBTypeFilter, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
options = property(GetOptions, SetOptions)

View File

@ -61,6 +61,8 @@ namespace lldb {
bool
operator != (lldb::SBTypeFormat &rhs);
STRING_EXTENSION_LEVEL(SBTypeFormat, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
format = property(GetFormat, SetFormat)

View File

@ -53,6 +53,8 @@ namespace lldb {
bool
operator != (lldb::SBTypeNameSpecifier &rhs);
STRING_EXTENSION_LEVEL(SBTypeNameSpecifier, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
name = property(GetName)

View File

@ -101,6 +101,8 @@ namespace lldb {
bool
operator != (lldb::SBTypeSummary &rhs);
STRING_EXTENSION_LEVEL(SBTypeSummary, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
options = property(GetOptions, SetOptions)

View File

@ -63,6 +63,8 @@ namespace lldb {
bool
operator != (lldb::SBTypeSynthetic &rhs);
STRING_EXTENSION_LEVEL(SBTypeSynthetic, lldb::eDescriptionLevelBrief)
#ifdef SWIGPYTHON
%pythoncode %{
options = property(GetOptions, SetOptions)

View File

@ -440,6 +440,8 @@ public:
const SBExpressionOptions &options,
const char *name) const;
STRING_EXTENSION(SBValue)
#ifdef SWIGPYTHON
%pythoncode %{
def __get_dynamic__ (self):

View File

@ -101,6 +101,29 @@ public:
lldb::SBValue
GetFirstValueByName (const char* name) const;
%extend {
%nothreadallow;
std::string lldb::SBValueList::__str__ (){
lldb::SBStream description;
const size_t n = $self->GetSize();
if (n)
{
for (size_t i=0; i<n; ++i)
$self->GetValueAtIndex(i).GetDescription(description);
}
else
{
description.Printf("<empty> lldb.SBValueList()");
}
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 std::string(desc, desc_len);
}
%clearnothreadallow;
}
#ifdef SWIGPYTHON
%pythoncode %{
def __iter__(self):

View File

@ -90,6 +90,7 @@ public:
static lldb::SBWatchpoint
GetWatchpointFromEvent (const lldb::SBEvent& event);
STRING_EXTENSION_LEVEL(SBWatchpoint, lldb::eDescriptionLevelVerbose)
};
} // namespace lldb