J'essaie d'utiliser libclang bindings python pour analyser mes fichiers source C++. Je suis incapable d'obtenir la valeur de macro ou développer une macro.
Voici mon exemple C++ CodeDévelopper des macros et récupérer des valeurs de macro
#define FOO 6001
#define EXPAND_MACR \
int \
foo = 61
int main()
{
EXPAND_MACR;
cout << foo;
return 0;
}
Ceci est mon script python
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)
Ceci est d'info Je reviens de 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]
Si vous observez mon script python, node.data donne
DATA <clang.cindex.c_void_p_Array_3 object at 0x10b86d950>
Je peux lire étendue données renvoyées par clang & analyser le fichier de début à fin positions pour obtenir la valeur. Je veux savoir si, il existe un meilleur moyen d'obtenir des valeurs macro?
Je veux obtenir la valeur de la macro (dans l'exemple) directement (sans utiliser l'étendue). Comment puis-je l'obtenir?
De plus, pour EXPAND_MACR est veulent obtenir int foo = 61
Je l'ai déjà vu ces post: Link-1 & Link-2.
Toute aide sera très appréciée