[Lldb-commits] [lldb] r354104 - Embed swig version into lldb.py in a different way

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 14 23:41:13 PST 2019


Author: labath
Date: Thu Feb 14 23:41:12 2019
New Revision: 354104

URL: http://llvm.org/viewvc/llvm-project?rev=354104&view=rev
Log:
Embed swig version into lldb.py in a different way

Summary:
Instead of doing string chopping on the resulting python file, get swig
to output the version for us. The two things which make slightly
non-trivial are:
- in order to get swig to expand SWIG_VERSION for us, we cannot use
  %pythoncode directly, but we have to go through an intermediate macro.
- SWIG_VERSION is a hex number, but it's components are supposed to be
  interpreted decimally, so there is a bit of integer magic needed to
  get the right number to come out.

I've tested that this approach works both with the latest (3.0.12) and
oldest (1.3.40) supported swig.

Reviewers: zturner, jingham, serge-sans-paille

Subscribers: jdoerfert, lldb-commits

Differential Revision: https://reviews.llvm.org/D58172

Added:
    lldb/trunk/packages/Python/lldbsuite/test/python_api/lldbutil/TestSwigVersion.py
Modified:
    lldb/trunk/scripts/Python/modify-python-lldb.py
    lldb/trunk/scripts/lldb.swig

Added: lldb/trunk/packages/Python/lldbsuite/test/python_api/lldbutil/TestSwigVersion.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/lldbutil/TestSwigVersion.py?rev=354104&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/python_api/lldbutil/TestSwigVersion.py (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/python_api/lldbutil/TestSwigVersion.py Thu Feb 14 23:41:12 2019
@@ -0,0 +1,28 @@
+"""
+Test that we embed the swig version into the lldb module
+"""
+
+from __future__ import print_function
+
+"""
+import os
+import time
+import re
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test import lldbutil
+"""
+from lldbsuite.test.lldbtest import *
+
+class SwigVersionTestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def test(self):
+        self.assertTrue(getattr(lldb, "swig_version"))
+        self.assertIsInstance(lldb.swig_version, tuple)
+        self.assertEqual(len(lldb.swig_version), 3)
+        self.assertGreaterEqual(lldb.swig_version[0], 1)
+        for v in lldb.swig_version:
+            self.assertGreaterEqual(v, 0)

Modified: lldb/trunk/scripts/Python/modify-python-lldb.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/modify-python-lldb.py?rev=354104&r1=354103&r2=354104&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/modify-python-lldb.py (original)
+++ lldb/trunk/scripts/Python/modify-python-lldb.py Thu Feb 14 23:41:12 2019
@@ -45,11 +45,6 @@ else:
 # print "output_name is '" + output_name + "'"
 
 #
-# Version string
-#
-version_line = "swig_version = %s"
-
-#
 # Residues to be removed.
 #
 c_endif_swig = "#endif"
@@ -338,7 +333,6 @@ init_pattern = re.compile("^    def __in
 isvalid_pattern = re.compile("^    def IsValid\(")
 
 # These define the states of our finite state machine.
-EXPECTING_VERSION = 0
 NORMAL = 1
 DEFINING_ITERATOR = 2
 DEFINING_EQUALITY = 4
@@ -364,9 +358,8 @@ lldb_iter_defined = False
 # The FSM, in all possible states, also checks the current input for IsValid()
 # definition, and inserts a __nonzero__() method definition to implement truth
 # value testing and the built-in operation bool().
-state = EXPECTING_VERSION
+state = NORMAL
 
-swig_version_tuple = None
 for line in content.splitlines():
     # Handle the state transition into CLEANUP_DOCSTRING state as it is possible
     # to enter this state from either NORMAL or DEFINING_ITERATOR/EQUALITY.
@@ -383,20 +376,6 @@ for line in content.splitlines():
         else:
             state |= CLEANUP_DOCSTRING
 
-    if state == EXPECTING_VERSION:
-        # We haven't read the version yet, read it now.
-        if swig_version_tuple is None:
-            match = version_pattern.search(line)
-            if match:
-                v = match.group(1)
-                swig_version_tuple = tuple(map(int, (v.split("."))))
-        elif not line.startswith('#'):
-            # This is the first non-comment line after the header.  Inject the
-            # version
-            new_line = version_line % str(swig_version_tuple)
-            new_content.add_line(new_line)
-            state = NORMAL
-
     if state == NORMAL:
         match = class_pattern.search(line)
         # Inserts lldb_helpers and the lldb_iter() definition before the first

Modified: lldb/trunk/scripts/lldb.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/lldb.swig?rev=354104&r1=354103&r2=354104&view=diff
==============================================================================
--- lldb/trunk/scripts/lldb.swig (original)
+++ lldb/trunk/scripts/lldb.swig Thu Feb 14 23:41:12 2019
@@ -66,6 +66,21 @@ import os
 
 import six
 %}
+
+// Include the version of swig that was used to generate this interface.
+%define EMBED_VERSION(VERSION)
+%pythoncode%{
+# SWIG_VERSION is written as a single hex number, but the components of it are
+# meant to be interpreted in decimal. So, 0x030012 is swig 3.0.12, and not
+# 3.0.18.
+def _to_int(hex):
+    return hex // 0x10 % 0x10 * 10 + hex % 0x10
+swig_version = (_to_int(VERSION // 0x10000), _to_int(VERSION // 0x100), _to_int(VERSION))
+del _to_int
+%}
+%enddef
+EMBED_VERSION(SWIG_VERSION)
+
 %include "./Python/python-typemaps.swig"
 
 /* C++ headers to be included. */




More information about the lldb-commits mailing list