[Lldb-commits] [lldb] r184580 - Change the SWIG wrappers to stop directly casting SB object to SWIG objects, and instead use a safer type-checked API (thanks templates)

Enrico Granata egranata at apple.com
Fri Jun 21 11:57:31 PDT 2013


Author: enrico
Date: Fri Jun 21 13:57:30 2013
New Revision: 184580

URL: http://llvm.org/viewvc/llvm-project?rev=184580&view=rev
Log:
Change the SWIG wrappers to stop directly casting SB object to SWIG objects, and instead use a safer type-checked API (thanks templates)
Any time a SWIG wrapper needs a PyObject for an SB object, it now should call into SBTypeToSWIGWrapper<SBType>(SBType*)
If you try to use it on an SBType for which there is not an implementation yet, LLDB will fail to link - just add your specialization to python-swigsafecast.swig and rebuild

This is the first step in simplifying our SWIG Wrapper layer


Added:
    lldb/trunk/scripts/Python/python-swigsafecast.swig
Modified:
    lldb/trunk/lldb.xcodeproj/project.pbxproj
    lldb/trunk/scripts/Python/build-swig-Python.sh
    lldb/trunk/scripts/Python/python-wrapper.swig
    lldb/trunk/scripts/lldb.swig

Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=184580&r1=184579&r2=184580&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Fri Jun 21 13:57:30 2013
@@ -1555,6 +1555,7 @@
 		944372DB171F6B4300E57C32 /* RegisterContextDummy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RegisterContextDummy.h; path = Utility/RegisterContextDummy.h; sourceTree = "<group>"; };
 		9443B120140C18A90013457C /* SBData.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SBData.h; path = include/lldb/API/SBData.h; sourceTree = "<group>"; };
 		9443B121140C18C10013457C /* SBData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBData.cpp; path = source/API/SBData.cpp; sourceTree = "<group>"; };
+		944DC3481774C99000D7D884 /* python-swigsafecast.swig */ = {isa = PBXFileReference; lastKnownFileType = text; path = "python-swigsafecast.swig"; sourceTree = "<group>"; };
 		9452573616262CD000325455 /* SBDeclaration.i */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c.preprocessed; path = SBDeclaration.i; sourceTree = "<group>"; };
 		9452573816262CEF00325455 /* SBDeclaration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SBDeclaration.h; path = include/lldb/API/SBDeclaration.h; sourceTree = "<group>"; };
 		9452573916262D0200325455 /* SBDeclaration.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBDeclaration.cpp; path = source/API/SBDeclaration.cpp; sourceTree = "<group>"; };
@@ -2356,6 +2357,7 @@
 				94FE476613FC1DA8001F8475 /* finish-swig-Python-LLDB.sh */,
 				94E367CC140C4EC4001C7A5A /* modify-python-lldb.py */,
 				9A48A3A7124AAA5A00922451 /* python-extensions.swig */,
+				944DC3481774C99000D7D884 /* python-swigsafecast.swig */,
 				94E367CE140C4EEA001C7A5A /* python-typemaps.swig */,
 				94005E0313F438DF001EF42D /* python-wrapper.swig */,
 			);

Modified: lldb/trunk/scripts/Python/build-swig-Python.sh
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/build-swig-Python.sh?rev=184580&r1=184579&r2=184580&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/build-swig-Python.sh (original)
+++ lldb/trunk/scripts/Python/build-swig-Python.sh Fri Jun 21 13:57:30 2013
@@ -46,6 +46,7 @@ swig_input_file=${SRC_ROOT}/scripts/lldb
 swig_python_extensions=${SRC_ROOT}/scripts/Python/python-extensions.swig
 swig_python_wrapper=${SRC_ROOT}/scripts/Python/python-wrapper.swig
 swig_python_typemaps=${SRC_ROOT}/scripts/Python/python-typemaps.swig
+swig_python_swigsafecast=${SRC_ROOT}/scripts/Python/python-swigsafecast.swig
 
 if [ "$LLDB_DISABLE_PYTHON" = "1" ] ; then
     # We don't want Python for this build, but touch the output file so we don't have to
@@ -276,6 +277,19 @@ then
             echo "swig file will need to be re-built."
         fi
     fi
+fi
+
+if [ $NeedToUpdate -eq 0 ]
+then
+    if [ ${swig_python_swigsafecast} -nt ${swig_output_file} ]
+    then
+        NeedToUpdate=1
+        if [ $Debug -eq 1 ]
+        then
+            echo "${swig_python_swigsafecast} is newer than ${swig_output_file}"
+            echo "swig file will need to be re-built."
+        fi
+    fi
 fi
 
 python_version=`/usr/bin/env python --version 2>&1 | sed -e 's,Python ,,' -e 's,[.][0-9],,2' -e 's,[a-z][a-z][0-9],,'`

Added: lldb/trunk/scripts/Python/python-swigsafecast.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-swigsafecast.swig?rev=184580&view=auto
==============================================================================
--- lldb/trunk/scripts/Python/python-swigsafecast.swig (added)
+++ lldb/trunk/scripts/Python/python-swigsafecast.swig Fri Jun 21 13:57:30 2013
@@ -0,0 +1,93 @@
+#ifndef __cplusplus
+#error needs C++ to build these
+#endif
+
+// leaving this undefined ensures we will get a linker error if we try to use SBTypeToSWIGWrapper()
+// for a type for which we did not specialze this function
+template <typename SBClass>
+PyObject*
+SBTypeToSWIGWrapper (SBClass* sb_object);
+
+template <typename SBClass>
+PyObject*
+SBTypeToSWIGWrapper (SBClass& sb_object)
+{
+    return SBTypeToSWIGWrapper(&sb_object);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBProcess* process_sb)
+{
+    return SWIG_NewPointerObj((void *) process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBThread* thread_sb)
+{
+    return SWIG_NewPointerObj((void *) thread_sb, SWIGTYPE_p_lldb__SBThread, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBTarget* target_sb)
+{
+    return SWIG_NewPointerObj((void *) target_sb, SWIGTYPE_p_lldb__SBTarget, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBFrame* frame_sb)
+{
+    return SWIG_NewPointerObj((void *) frame_sb, SWIGTYPE_p_lldb__SBFrame, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBDebugger* debugger_sb)
+{
+    return SWIG_NewPointerObj((void *) debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBBreakpoint* breakpoint_sb)
+{
+    return SWIG_NewPointerObj((void *) breakpoint_sb, SWIGTYPE_p_lldb__SBBreakpoint, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBWatchpoint* watchpoint_sb)
+{
+    return SWIG_NewPointerObj((void *) watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBBreakpointLocation* breakpoint_location_sb)
+{
+    return SWIG_NewPointerObj((void *) breakpoint_location_sb, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBValue* value_sb)
+{
+    return SWIG_NewPointerObj((void *) value_sb, SWIGTYPE_p_lldb__SBValue, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBCommandReturnObject* cmd_ret_obj_sb)
+{
+    return SWIG_NewPointerObj((void *) cmd_ret_obj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (lldb::SBInputReader* input_reader_sb)
+{
+    return SWIG_NewPointerObj((void *) input_reader_sb, SWIGTYPE_p_lldb__SBInputReader, 0);
+}

Modified: lldb/trunk/scripts/Python/python-wrapper.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-wrapper.swig?rev=184580&r1=184579&r2=184580&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-wrapper.swig (original)
+++ lldb/trunk/scripts/Python/python-wrapper.swig Fri Jun 21 13:57:30 2013
@@ -119,8 +119,8 @@ LLDBSwigPythonBreakpointCallbackFunction
     lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
 
     bool stop_at_breakpoint = true;
-    PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
-    PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
+    PyObject *Frame_PyObj = SBTypeToSWIGWrapper(sb_frame);
+    PyObject *Bp_Loc_PyObj = SBTypeToSWIGWrapper(sb_bp_loc);
     
     if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
         return stop_at_breakpoint;
@@ -202,8 +202,8 @@ LLDBSwigPythonWatchpointCallbackFunction
     lldb::SBWatchpoint sb_wp(wp_sp);
 
     bool stop_at_watchpoint = true;
-    PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
-    PyObject *Wp_PyObj = SWIG_NewPointerObj ((void *) &sb_wp, SWIGTYPE_p_lldb__SBWatchpoint, 0);
+    PyObject *Frame_PyObj = SBTypeToSWIGWrapper(sb_frame);
+    PyObject *Wp_PyObj = SBTypeToSWIGWrapper(sb_wp);
     
     if (Frame_PyObj == NULL || Wp_PyObj == NULL)
         return stop_at_watchpoint;
@@ -279,7 +279,7 @@ LLDBSwigPythonCallTypeScript
 
     retval.clear();
 
-    PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0);
+    PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
     
     if (ValObj_PyObj == NULL)
         return false;
@@ -357,10 +357,10 @@ LLDBSwigPythonCreateSyntheticProvider
 
     // I do not want the SBValue to be deallocated when going out of scope because python
     // has ownership of it and will manage memory for this object by itself
-    lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp);
-    valobj_sb->SetPreferSyntheticValue(false);
+    lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
+    sb_value->SetPreferSyntheticValue(false);
 
-    PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0);
+    PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
 
     if (ValObj_PyObj == NULL)
         Py_RETURN_NONE;
@@ -714,8 +714,8 @@ LLDBSwigPythonCallCommand
 
     bool retval = false;
 
-    PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
-    PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
+    PyObject *DebuggerObj_PyObj = SBTypeToSWIGWrapper(debugger_sb);
+    PyObject *CmdRetObj_PyObj = SBTypeToSWIGWrapper(cmd_retobj_sb);
 
     if (DebuggerObj_PyObj == NULL)
         return retval;
@@ -810,11 +810,11 @@ LLDBSWIGPythonCreateOSPlugin
     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
         Py_RETURN_NONE;
 
-    // I do not want the SBValue to be deallocated when going out of scope because python
+    // I do not want the SBProcess to be deallocated when going out of scope because python
     // has ownership of it and will manage memory for this object by itself
     lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
 
-    PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
+    PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
 
     if (SBProc_PyObj == NULL)
         Py_RETURN_NONE;
@@ -908,7 +908,7 @@ std::string& output)
         return retval;
 
     lldb::SBProcess process_sb(process);
-    PyObject *ProcessObj_PyObj = SWIG_NewPointerObj((void *) &process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
+    PyObject *ProcessObj_PyObj = SBTypeToSWIGWrapper(process_sb);
 
     if (ProcessObj_PyObj == NULL)
         return retval;
@@ -994,7 +994,7 @@ std::string& output)
         return retval;
 
     lldb::SBThread thread_sb(thread);
-    PyObject *ThreadObj_PyObj = SWIG_NewPointerObj((void *) &thread_sb, SWIGTYPE_p_lldb__SBThread, 0);
+    PyObject *ThreadObj_PyObj = SBTypeToSWIGWrapper(thread_sb);
 
     if (ThreadObj_PyObj == NULL)
         return retval;
@@ -1080,7 +1080,7 @@ std::string& output)
         return retval;
 
     lldb::SBTarget target_sb(target);
-    PyObject *TargetObj_PyObj = SWIG_NewPointerObj((void *) &target_sb, SWIGTYPE_p_lldb__SBTarget, 0);
+    PyObject *TargetObj_PyObj = SBTypeToSWIGWrapper(target_sb);
 
     if (TargetObj_PyObj == NULL)
         return retval;
@@ -1166,7 +1166,7 @@ std::string& output)
         return retval;
 
     lldb::SBFrame frame_sb(frame);
-    PyObject *FrameObj_PyObj = SWIG_NewPointerObj((void *) &frame_sb, SWIGTYPE_p_lldb__SBFrame, 0);
+    PyObject *FrameObj_PyObj = SBTypeToSWIGWrapper(frame_sb);
 
     if (FrameObj_PyObj == NULL)
         return retval;
@@ -1251,7 +1251,7 @@ LLDBSwigPythonCallModuleInit
 
     bool retval = false;
 
-    PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
+    PyObject *DebuggerObj_PyObj = SBTypeToSWIGWrapper(debugger_sb);
 
     if (DebuggerObj_PyObj == NULL)
         return retval;
@@ -1360,7 +1360,7 @@ LLDBSwigPythonCallSBInputReaderCallback(
     if (baton != Py_None) {
         SWIG_PYTHON_THREAD_BEGIN_BLOCK;
     
-        PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false);
+        PyObject *py_InputReader = SBTypeToSWIGWrapper(reader);
         PyObject *py_Notification = PyInt_FromLong(notification);
         PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
     

Modified: lldb/trunk/scripts/lldb.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/lldb.swig?rev=184580&r1=184579&r2=184580&view=diff
==============================================================================
--- lldb/trunk/scripts/lldb.swig (original)
+++ lldb/trunk/scripts/lldb.swig Fri Jun 21 13:57:30 2013
@@ -98,6 +98,9 @@ import os
 #include "lldb/API/SBValue.h"
 #include "lldb/API/SBValueList.h"
 #include "lldb/API/SBWatchpoint.h"
+
+#include "../scripts/Python/python-swigsafecast.swig"
+
 %}
 
 /* Various liblldb typedefs that SWIG needs to know about.  */





More information about the lldb-commits mailing list