From 51bdd98b8a52d07004bcaddff26caf376a1c32bf Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Wed, 8 Jan 2020 20:56:11 -0800 Subject: [PATCH] [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 --- lldb/scripts/Python/python-extensions.swig | 257 +-------------------------- lldb/scripts/interface/SBProcess.i | 2 + lldb/scripts/interface/SBSection.i | 2 + lldb/scripts/interface/SBSymbol.i | 2 + lldb/scripts/interface/SBSymbolContext.i | 1 + lldb/scripts/interface/SBSymbolContextList.i | 2 + lldb/scripts/interface/SBThread.i | 2 + lldb/scripts/interface/SBType.i | 5 + lldb/scripts/interface/SBTypeCategory.i | 2 + lldb/scripts/interface/SBTypeEnumMember.i | 1 + lldb/scripts/interface/SBTypeFilter.i | 2 + lldb/scripts/interface/SBTypeFormat.i | 2 + lldb/scripts/interface/SBTypeNameSpecifier.i | 2 + lldb/scripts/interface/SBTypeSummary.i | 2 + lldb/scripts/interface/SBTypeSynthetic.i | 2 + lldb/scripts/interface/SBValue.i | 2 + lldb/scripts/interface/SBValueList.i | 23 +++ lldb/scripts/interface/SBWatchpoint.i | 1 + 18 files changed, 59 insertions(+), 253 deletions(-) diff --git a/lldb/scripts/Python/python-extensions.swig b/lldb/scripts/Python/python-extensions.swig index 36dac19..0b23fdd 100644 --- a/lldb/scripts/Python/python-extensions.swig +++ b/lldb/scripts/Python/python-extensions.swig @@ -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; iGetValueAtIndex(i).GetDescription(description); - } - else - { - description.Printf(" 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 %{ diff --git a/lldb/scripts/interface/SBProcess.i b/lldb/scripts/interface/SBProcess.i index c5ebc24..ac6a265 100644 --- a/lldb/scripts/interface/SBProcess.i +++ b/lldb/scripts/interface/SBProcess.i @@ -417,6 +417,8 @@ public: lldb::SBProcessInfo GetProcessInfo(); + STRING_EXTENSION(SBProcess) + #ifdef SWIGPYTHON %pythoncode %{ def __get_is_alive__(self): diff --git a/lldb/scripts/interface/SBSection.i b/lldb/scripts/interface/SBSection.i index c1a84ac..3d1c900 100644 --- a/lldb/scripts/interface/SBSection.i +++ b/lldb/scripts/interface/SBSection.i @@ -114,6 +114,8 @@ public: bool operator != (const lldb::SBSection &rhs); + STRING_EXTENSION(SBSection) + #ifdef SWIGPYTHON %pythoncode %{ def __iter__(self): diff --git a/lldb/scripts/interface/SBSymbol.i b/lldb/scripts/interface/SBSymbol.i index e5880e6..4e17ab5 100644 --- a/lldb/scripts/interface/SBSymbol.i +++ b/lldb/scripts/interface/SBSymbol.i @@ -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): diff --git a/lldb/scripts/interface/SBSymbolContext.i b/lldb/scripts/interface/SBSymbolContext.i index a6aa4d7..b6b3365 100644 --- a/lldb/scripts/interface/SBSymbolContext.i +++ b/lldb/scripts/interface/SBSymbolContext.i @@ -81,6 +81,7 @@ public: bool GetDescription (lldb::SBStream &description); + STRING_EXTENSION(SBSymbolContext) #ifdef SWIGPYTHON %pythoncode %{ diff --git a/lldb/scripts/interface/SBSymbolContextList.i b/lldb/scripts/interface/SBSymbolContextList.i index 54adc65..f5adcfc 100644 --- a/lldb/scripts/interface/SBSymbolContextList.i +++ b/lldb/scripts/interface/SBSymbolContextList.i @@ -60,6 +60,8 @@ public: void Clear(); + STRING_EXTENSION(SBSymbolContextList) + #ifdef SWIGPYTHON %pythoncode %{ def __iter__(self): diff --git a/lldb/scripts/interface/SBThread.i b/lldb/scripts/interface/SBThread.i index c1c0454..95b15b1 100644 --- a/lldb/scripts/interface/SBThread.i +++ b/lldb/scripts/interface/SBThread.i @@ -402,6 +402,8 @@ public: bool SafeToCallFunctions (); + STRING_EXTENSION(SBThread) + #ifdef SWIGPYTHON %pythoncode %{ def __iter__(self): diff --git a/lldb/scripts/interface/SBType.i b/lldb/scripts/interface/SBType.i index d9da9e3..3cd8245 100644 --- a/lldb/scripts/interface/SBType.i +++ b/lldb/scripts/interface/SBType.i @@ -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): diff --git a/lldb/scripts/interface/SBTypeCategory.i b/lldb/scripts/interface/SBTypeCategory.i index 43fe9fa..b762bf8 100644 --- a/lldb/scripts/interface/SBTypeCategory.i +++ b/lldb/scripts/interface/SBTypeCategory.i @@ -124,6 +124,8 @@ namespace lldb { bool DeleteTypeSynthetic (lldb::SBTypeNameSpecifier); + STRING_EXTENSION_LEVEL(SBTypeCategory, lldb::eDescriptionLevelBrief) + #ifdef SWIGPYTHON %pythoncode %{ diff --git a/lldb/scripts/interface/SBTypeEnumMember.i b/lldb/scripts/interface/SBTypeEnumMember.i index b2d8617..006bdea 100644 --- a/lldb/scripts/interface/SBTypeEnumMember.i +++ b/lldb/scripts/interface/SBTypeEnumMember.i @@ -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.''') diff --git a/lldb/scripts/interface/SBTypeFilter.i b/lldb/scripts/interface/SBTypeFilter.i index 3759e0a2..c1d282c 100644 --- a/lldb/scripts/interface/SBTypeFilter.i +++ b/lldb/scripts/interface/SBTypeFilter.i @@ -61,6 +61,8 @@ namespace lldb { bool operator != (lldb::SBTypeFilter &rhs); + STRING_EXTENSION_LEVEL(SBTypeFilter, lldb::eDescriptionLevelBrief) + #ifdef SWIGPYTHON %pythoncode %{ options = property(GetOptions, SetOptions) diff --git a/lldb/scripts/interface/SBTypeFormat.i b/lldb/scripts/interface/SBTypeFormat.i index 5efd135b..765a2a7 100644 --- a/lldb/scripts/interface/SBTypeFormat.i +++ b/lldb/scripts/interface/SBTypeFormat.i @@ -61,6 +61,8 @@ namespace lldb { bool operator != (lldb::SBTypeFormat &rhs); + STRING_EXTENSION_LEVEL(SBTypeFormat, lldb::eDescriptionLevelBrief) + #ifdef SWIGPYTHON %pythoncode %{ format = property(GetFormat, SetFormat) diff --git a/lldb/scripts/interface/SBTypeNameSpecifier.i b/lldb/scripts/interface/SBTypeNameSpecifier.i index bb16e86..772f7c1 100644 --- a/lldb/scripts/interface/SBTypeNameSpecifier.i +++ b/lldb/scripts/interface/SBTypeNameSpecifier.i @@ -53,6 +53,8 @@ namespace lldb { bool operator != (lldb::SBTypeNameSpecifier &rhs); + STRING_EXTENSION_LEVEL(SBTypeNameSpecifier, lldb::eDescriptionLevelBrief) + #ifdef SWIGPYTHON %pythoncode %{ name = property(GetName) diff --git a/lldb/scripts/interface/SBTypeSummary.i b/lldb/scripts/interface/SBTypeSummary.i index 225a404..adcc79b 100644 --- a/lldb/scripts/interface/SBTypeSummary.i +++ b/lldb/scripts/interface/SBTypeSummary.i @@ -101,6 +101,8 @@ namespace lldb { bool operator != (lldb::SBTypeSummary &rhs); + STRING_EXTENSION_LEVEL(SBTypeSummary, lldb::eDescriptionLevelBrief) + #ifdef SWIGPYTHON %pythoncode %{ options = property(GetOptions, SetOptions) diff --git a/lldb/scripts/interface/SBTypeSynthetic.i b/lldb/scripts/interface/SBTypeSynthetic.i index d9d75e4..f57139e 100644 --- a/lldb/scripts/interface/SBTypeSynthetic.i +++ b/lldb/scripts/interface/SBTypeSynthetic.i @@ -63,6 +63,8 @@ namespace lldb { bool operator != (lldb::SBTypeSynthetic &rhs); + STRING_EXTENSION_LEVEL(SBTypeSynthetic, lldb::eDescriptionLevelBrief) + #ifdef SWIGPYTHON %pythoncode %{ options = property(GetOptions, SetOptions) diff --git a/lldb/scripts/interface/SBValue.i b/lldb/scripts/interface/SBValue.i index 8647854..fb89980 100644 --- a/lldb/scripts/interface/SBValue.i +++ b/lldb/scripts/interface/SBValue.i @@ -440,6 +440,8 @@ public: const SBExpressionOptions &options, const char *name) const; + STRING_EXTENSION(SBValue) + #ifdef SWIGPYTHON %pythoncode %{ def __get_dynamic__ (self): diff --git a/lldb/scripts/interface/SBValueList.i b/lldb/scripts/interface/SBValueList.i index 56ef1905..17ba205 100644 --- a/lldb/scripts/interface/SBValueList.i +++ b/lldb/scripts/interface/SBValueList.i @@ -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; iGetValueAtIndex(i).GetDescription(description); + } + else + { + description.Printf(" 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): diff --git a/lldb/scripts/interface/SBWatchpoint.i b/lldb/scripts/interface/SBWatchpoint.i index e11c4f2..cb0bc5f 100644 --- a/lldb/scripts/interface/SBWatchpoint.i +++ b/lldb/scripts/interface/SBWatchpoint.i @@ -90,6 +90,7 @@ public: static lldb::SBWatchpoint GetWatchpointFromEvent (const lldb::SBEvent& event); + STRING_EXTENSION_LEVEL(SBWatchpoint, lldb::eDescriptionLevelVerbose) }; } // namespace lldb -- 2.7.4