[LLVMdev] libcang python bindings and working with macros question

Sam Price thesamprice at gmail.com
Tue Dec 10 14:09:53 PST 2013


How do I obtain the data that a macro points to, both expanded and
unexpanded.

in example I have a file named "blah.c" with the following.
"""
#define TESTA 1
#define TESTB 2 + TESTA
"""

I modified util.py's get_cursor to look at the displayname also, as
spelling won't match the macroname.
"""
    for cursor in children:
        if cursor.spelling == spelling:
            return cursor
        if cursor.displayname == spelling:
            return cursor
"""

import scripts.clang.cindex as cindex
from scripts.util import get_cursor

index = cindex.Index.create()

t = cindex.TranslationUnit
tu = index.parse('blah.c',[ ] ,options=t.PARSE_DETAILED_PROCESSING_RECORD |
t.PARSE_PRECOMPILED_PREAMBLE)

cursor = tu.cursor
z = get_cursor(cursor,'TESTB')

fid = open('blah.c')
raw = fid.read()
fid.close()
print raw[z.extent.start.offset:z.extent.end.offset]

this outputs


TESTB 2 + TESTA



How should I go about obtaining the expanded macro of 3.




-- 
Thank you,

Sam Price
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131210/4d56d8e8/attachment.html>


More information about the llvm-dev mailing list