kastiglione created this revision.
kastiglione added a reviewer: JDevlieghere.
Herald added a project: All.
kastiglione requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

When attempting to use SWIG's `-builtin` flag, there were a few compile
failures caused by a mismatch between return type and return value. In those
cases, the return type was `int` but many of the type maps assume returning
`NULL`/`nullptr` (only the latter caused compile failures).

This fix abstracts failure paths to use the `SWIG_fail` macro, which performs
`goto fail;`. Each of the generated functions contain a `fail` label, which
performs any resource cleanup and returns the appropriate failure value.

This change isn't strictly necessary at this point, but seems like the right
thing to do, and for anyone who tries `-builtin` later, it resolves those
issues.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133961

Files:
  lldb/bindings/python/python-typemaps.swig

Index: lldb/bindings/python/python-typemaps.swig
===================================================================
--- lldb/bindings/python/python-typemaps.swig
+++ lldb/bindings/python/python-typemaps.swig
@@ -18,7 +18,7 @@
       if (!py_str.IsAllocated()) {
         PyErr_SetString(PyExc_TypeError, "list must contain strings");
         free($1);
-        return nullptr;
+        SWIG_fail;
       }
 
       $1[i] = const_cast<char *>(py_str.GetString().data());
@@ -28,7 +28,7 @@
     $1 = NULL;
   } else {
     PyErr_SetString(PyExc_TypeError, "not a list");
-    return NULL;
+    SWIG_fail;
   }
 }
 
@@ -70,7 +70,7 @@
   PythonObject obj = Retain<PythonObject>($input);
   lldb::tid_t value = unwrapOrSetPythonException(As<unsigned long long>(obj));
   if (PyErr_Occurred())
-    return nullptr;
+    SWIG_fail;
   $1 = value;
 }
 
@@ -79,10 +79,10 @@
   unsigned long long state_type_value =
       unwrapOrSetPythonException(As<unsigned long long>(obj));
   if (PyErr_Occurred())
-    return nullptr;
+    SWIG_fail;
   if (state_type_value > lldb::StateType::kLastStateType) {
     PyErr_SetString(PyExc_ValueError, "Not a valid StateType value");
-    return nullptr;
+    SWIG_fail;
   }
   $1 = static_cast<lldb::StateType>(state_type_value);
 }
@@ -93,12 +93,12 @@
 %typemap(in) (char *dst, size_t dst_len) {
   if (!PyInt_Check($input)) {
     PyErr_SetString(PyExc_ValueError, "Expecting an integer");
-    return NULL;
+    SWIG_fail;
   }
   $2 = PyInt_AsLong($input);
   if ($2 <= 0) {
     PyErr_SetString(PyExc_ValueError, "Positive integer expected");
-    return NULL;
+    SWIG_fail;
   }
   $1 = (char *)malloc($2);
 }
@@ -129,12 +129,12 @@
 %typemap(in) (char *dst_or_null, size_t dst_len) {
   if (!PyInt_Check($input)) {
     PyErr_SetString(PyExc_ValueError, "Expecting an integer");
-    return NULL;
+    SWIG_fail;
   }
   $2 = PyInt_AsLong($input);
   if ($2 <= 0) {
     PyErr_SetString(PyExc_ValueError, "Positive integer expected");
-    return NULL;
+    SWIG_fail;
   }
   $1 = (char *)malloc($2);
 }
@@ -166,7 +166,7 @@
     $2 = bytes.GetSize();
   } else {
     PyErr_SetString(PyExc_ValueError, "Expecting a string");
-    return NULL;
+    SWIG_fail;
   }
 }
 // For SBProcess::WriteMemory, SBTarget::GetInstructions and SBDebugger::DispatchInput.
@@ -186,7 +186,7 @@
     $2 = bytes.GetSize();
   } else {
     PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
-    return NULL;
+    SWIG_fail;
   }
 }
 
@@ -199,11 +199,11 @@
     $2 = PyLong_AsLong($input);
   } else {
     PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
-    return NULL;
+    SWIG_fail;
   }
   if ($2 <= 0) {
     PyErr_SetString(PyExc_ValueError, "Positive integer expected");
-    return NULL;
+    SWIG_fail;
   }
   $1 = (void *)malloc($2);
 }
@@ -287,12 +287,12 @@
       if (!SetNumberFromPyObject($1[i], o)) {
         PyErr_SetString(PyExc_TypeError, "list must contain numbers");
         free($1);
-        return NULL;
+        SWIG_fail;
       }
 
       if (PyErr_Occurred()) {
         free($1);
-        return NULL;
+        SWIG_fail;
       }
     }
   } else if ($input == Py_None) {
@@ -300,7 +300,7 @@
     $2 = 0;
   } else {
     PyErr_SetString(PyExc_TypeError, "not a list");
-    return NULL;
+    SWIG_fail;
   }
 }
 
@@ -353,7 +353,7 @@
   if (!($input == Py_None ||
         PyCallable_Check(reinterpret_cast<PyObject *>($input)))) {
     PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
-    return NULL;
+    SWIG_fail;
   }
 
   // FIXME (filcab): We can't currently check if our callback is already
@@ -377,11 +377,11 @@
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp = unwrapOrSetPythonException(py_file.ConvertToFile());
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -389,12 +389,12 @@
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp = unwrapOrSetPythonException(
       py_file.ConvertToFileForcingUseOfScriptingIOMethods());
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -402,12 +402,12 @@
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp =
       unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true));
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -415,12 +415,12 @@
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp = unwrapOrSetPythonException(
       py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true));
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -439,7 +439,7 @@
   if (sp) {
     PythonFile pyfile = unwrapOrSetPythonException(PythonFile::FromFile(*sp));
     if (!pyfile.IsValid())
-      return nullptr;
+      SWIG_fail;
     $result = pyfile.release();
   }
   if (!$result) {
@@ -468,7 +468,7 @@
     py_str.release();
   } else {
     PyErr_SetString(PyExc_TypeError, "not a string-like object");
-    return NULL;
+    SWIG_fail;
   }
 }
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to