I am trying to use libclang python bindings to parse my c++ source files. I am unable to get the value of macro or expand a macro.
Here is my sample c++ code
#define FOO 6001
#define EXPAND_MACR \
int \
foo = 61
int main()
{
EXPAND_MACR;
cout << foo;
return 0;
}
This is my python script
import sys
import clang.cindex
def visit(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)
if __name__ == '__main__':
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1], options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)
print 'Translation unit:', tu.spelling
visit(tu.cursor)
This is info I get back from clang:
Found FOO Type CursorKind.MACRO_DEFINITION DATA <clang.cindex.c_void_p_Array_3 object at 0x10b86d950> Extent <SourceRange start <SourceLocation file 'sample.cpp', line 4, column 9>, end <SourceLocation file 'sample.cpp', line 4, column 17>> [line=4, col=9]
Found EXPAND_MACR Type CursorKind.MACRO_DEFINITION DATA <clang.cindex.c_void_p_Array_3 object at 0x10b86d950> Extent <SourceRange start <SourceLocation file 'sample.cpp', line 6, column 9>, end <SourceLocation file 'sample.cpp', line 8, column 11>> [line=6, col=9]
Found EXPAND_MACR Type CursorKind.MACRO_INSTANTIATION DATA <clang.cindex.c_void_p_Array_3 object at 0x10b86d950> Extent <SourceRange start <SourceLocation file 'sample.cpp', line 12, column 2>, end <SourceLocation file 'sample.cpp', line 12, column 13>> [line=12, col=2]
If you observe my python script, node.data gives out
DATA <clang.cindex.c_void_p_Array_3 object at 0x10b86d950>
I can read Extent data returned by clang & then parse the file from start to end positions to get the value. I want to know if, there exists a better way to get macro values?
I want to get the value of macro(6001 in the sample) directly(without using Extent). How can I get that?
Additionally for EXPAND_MACR is want to get int foo = 61
I have already seen these post: Link-1 & Link-2.
Any help will be highly appreciated