Skip to content

Commit 8d6b60c

Browse files
committedFeb 15, 2019
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 llvm-svn: 354104
1 parent 8c02e77 commit 8d6b60c

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Test that we embed the swig version into the lldb module
3+
"""
4+
5+
from __future__ import print_function
6+
7+
"""
8+
import os
9+
import time
10+
import re
11+
import lldb
12+
from lldbsuite.test.decorators import *
13+
from lldbsuite.test import lldbutil
14+
"""
15+
from lldbsuite.test.lldbtest import *
16+
17+
class SwigVersionTestCase(TestBase):
18+
19+
mydir = TestBase.compute_mydir(__file__)
20+
NO_DEBUG_INFO_TESTCASE = True
21+
22+
def test(self):
23+
self.assertTrue(getattr(lldb, "swig_version"))
24+
self.assertIsInstance(lldb.swig_version, tuple)
25+
self.assertEqual(len(lldb.swig_version), 3)
26+
self.assertGreaterEqual(lldb.swig_version[0], 1)
27+
for v in lldb.swig_version:
28+
self.assertGreaterEqual(v, 0)

‎lldb/scripts/Python/modify-python-lldb.py

+1-22
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444

4545
# print "output_name is '" + output_name + "'"
4646

47-
#
48-
# Version string
49-
#
50-
version_line = "swig_version = %s"
51-
5247
#
5348
# Residues to be removed.
5449
#
@@ -338,7 +333,6 @@ def finish(self):
338333
isvalid_pattern = re.compile("^ def IsValid\(")
339334

340335
# These define the states of our finite state machine.
341-
EXPECTING_VERSION = 0
342336
NORMAL = 1
343337
DEFINING_ITERATOR = 2
344338
DEFINING_EQUALITY = 4
@@ -364,9 +358,8 @@ def finish(self):
364358
# The FSM, in all possible states, also checks the current input for IsValid()
365359
# definition, and inserts a __nonzero__() method definition to implement truth
366360
# value testing and the built-in operation bool().
367-
state = EXPECTING_VERSION
361+
state = NORMAL
368362

369-
swig_version_tuple = None
370363
for line in content.splitlines():
371364
# Handle the state transition into CLEANUP_DOCSTRING state as it is possible
372365
# to enter this state from either NORMAL or DEFINING_ITERATOR/EQUALITY.
@@ -383,20 +376,6 @@ def finish(self):
383376
else:
384377
state |= CLEANUP_DOCSTRING
385378

386-
if state == EXPECTING_VERSION:
387-
# We haven't read the version yet, read it now.
388-
if swig_version_tuple is None:
389-
match = version_pattern.search(line)
390-
if match:
391-
v = match.group(1)
392-
swig_version_tuple = tuple(map(int, (v.split("."))))
393-
elif not line.startswith('#'):
394-
# This is the first non-comment line after the header. Inject the
395-
# version
396-
new_line = version_line % str(swig_version_tuple)
397-
new_content.add_line(new_line)
398-
state = NORMAL
399-
400379
if state == NORMAL:
401380
match = class_pattern.search(line)
402381
# Inserts lldb_helpers and the lldb_iter() definition before the first

‎lldb/scripts/lldb.swig

+15
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ import os
6666

6767
import six
6868
%}
69+
70+
// Include the version of swig that was used to generate this interface.
71+
%define EMBED_VERSION(VERSION)
72+
%pythoncode%{
73+
# SWIG_VERSION is written as a single hex number, but the components of it are
74+
# meant to be interpreted in decimal. So, 0x030012 is swig 3.0.12, and not
75+
# 3.0.18.
76+
def _to_int(hex):
77+
return hex // 0x10 % 0x10 * 10 + hex % 0x10
78+
swig_version = (_to_int(VERSION // 0x10000), _to_int(VERSION // 0x100), _to_int(VERSION))
79+
del _to_int
80+
%}
81+
%enddef
82+
EMBED_VERSION(SWIG_VERSION)
83+
6984
%include "./Python/python-typemaps.swig"
7085

7186
/* C++ headers to be included. */

0 commit comments

Comments
 (0)
Please sign in to comment.