Skip to content Skip to sidebar Skip to footer

How Can I Parse Macros In C++ Code, Using Clang As The Parser And Python As The Scripting Language?

If I have the following macro in some C++ code: _Foo(arg1, arg2) I would like to use Python to find me all the instances and extents of that macro using Clang and the Python bindi

Solution 1:

You need to pass the appropriate options flag to Index.parse:

tu = index.parse(sys.argv[1], options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)

The rest of the cursor visitor could look like this:

defvisit(node):
    if node.kind in (clang.cindex.CursorKind.MACRO_INSTANTIATION, clang.cindex.CursorKind.MACRO_DEFINITION):
        print'Found %s Type %s DATA %s Extent %s [line=%s, col=%s]' % (node.displayname, node.kind, node.data, node.extent, node.location.line, node.location.column)
    for c in node.get_children():
        visit(c)

Solution 2:

I once wrote a script to prettyprint the whole AST you get from libclang, in order to see where to find which information.

Here it is: https://gist.github.com/2503232

Post a Comment for "How Can I Parse Macros In C++ Code, Using Clang As The Parser And Python As The Scripting Language?"