[cfe-dev] access violation parsing functions from C code

Dmitry Beransky dmitry.maven at gmail.com
Fri Apr 6 14:31:49 PDT 2012


If this isn't the right place for my question I'd appreciate a pointer
to a more appropriate venue.

I've observed this problem in both older clang/llvm code (as of
several months ago) as well as in the most recent trunk snapshot.  In
all cases running under Python 2.6 on both Windows XP and Ubuntu
10.04.

So, I'm trying to use clang via python bindings to get a list of
declared functions and their line locations in C code.  This is the
code I use:

def filter_nodes(node, node_type):
    if not node:
        return []

    children = []
    for c in node.get_children():
        children.extend(filter_nodes(c, node_type))

    return [node] + children if node.kind == node_type else children

def functions(file_path):
    index = clang.cindex.Index.create()
    tu = index.parse(file_path)

    functions = filter_nodes(tu.cursor, CursorKind.FUNCTION_DECL)

    for f in functions:
        try:
            print f.spelling+"()",
            print " in %s(%s:%s)" % (f.extent.file,
f.extent.start.line, f.extent.end.line)
        except Exception as e:
            print "ERROR: ", str(e)

On some files this works just fine and prints out all the data.  On
other files, sometimes f.spelling would print just fine, but the next
print statement would trigger an exception ("access violation reading
0xXXXXXXX").  And there are files that would generate the same
exception for every function declaration node returned.  Here's a
sample output:

ERROR:  exception: access violation reading 0x02F73C90
() ERROR:  exception: access violation reading 0x02F73FF0
strlwr() ERROR:  exception: access violation reading 0x02D030D0
strnicmp() ERROR:  exception: access violation reading 0x02D030D0
strnset() ERROR:  exception: access violation reading 0x02D030D0
strrev() ERROR:  exception: access violation reading 0x02D030D0
ERROR:  exception: access violation reading 0x02F808F0
ERROR:  exception: access violation reading 0x02F80B20
ERROR:  exception: access violation reading 0x02F80D10
ERROR:  exception: access violation reading 0x02F80EF0

My questions are: 1) am I using the API improperly, thus causing the
error and if so, how?; 2) If not, what can I do to correct the issue?

Thanks
Dmitry



More information about the cfe-dev mailing list