[llvm-bugs] [Bug 26394] New: libclang python bindings discard index when creating a TranslationUnit

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jan 30 09:56:05 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26394

            Bug ID: 26394
           Summary: libclang python bindings discard index when creating a
                    TranslationUnit
           Product: clang
           Version: 3.7
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: andrej.lajovic at ad-vega.si
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

I was testing the python bindings for libclang and I noticed that the following
example crashes:

test.py:
----------
import clang.cindex
tu = clang.cindex.TranslationUnit.from_source('test.c')
tu.reparse()
print tu.cursor()
----------

The file 'test.c' must exist, but can be empty. The expected result is that
clang would create a translation unit and then reparse it immediately. But the
actual result is this:

----------
$ python2 test.py 
LIBCLANG FATAL ERROR: unknown module format
libclang: crash detected during reparsing
Segmentation fault
----------

I believe that I know what the problem is. When the classmethod
clang.cindex.TranslationUnit.from_source() is called, it creates an index and a
little while later passes it to the constructor:

> if index is None:
>     index = Index.create()
> [...]
> return cls(ptr, index=index)

However, the constructor only checks that the index is indeed a valid Index
object, but does not store it in any way (I am omitting the documentation
string and empty lines for brevity):

> def __init__(self, ptr, index):
>     assert isinstance(index, Index)
>     ClangObject.__init__(self, ptr)

As a consequence, after TranslationUnit.from_source() terminates, the index has
a reference count of zero and it is destroyed by python's garbage collector. I
believe that this is not something that should happen. Indeed, if I insert

self.index = index

just after the assert clause, my little snippet in test.py starts to work as
expected.

Can somebody involved in python bindings take a look at this, please? I have
only started to dabble with clang about a day ago and I am not confident enough
to propose a definite solution.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160130/dbd05e95/attachment.html>


More information about the llvm-bugs mailing list