我使用以下@L_403_0@脚本(cindex.py和libclang)
import codecsfrom clang.cindex import *class Sourcefile(object): def __init__(self,path): with codecs.open(path,'r','utf-8') as file: self.file_content = file.read() index = Index.create() root_node = index.parse(path) for included in root_node.get_includes(): print included.include self.print_declerations(root_node.cursor) def print_declerations(self,root,recurse=True): print root.kind.name,root.spelling if root.kind.is_declaration(): node_def = root.get_deFinition() if node_def is not None: start_offset = node_def.extent.start.offset end_offset = node_def.extent.end.offset + 1 print self.file_content[start_offset:end_offset],'\n' if recurse: for child in root.get_children(): self.print_declerations(child,False)if __name__ == '__main__': path = 'Sample.cpp' print 'Translation unit:',path source = Sourcefile(path)
哪些输出
Translation unit: Sample.cpp/mingw/include\stdio.h/mingw/include\_mingw.h/mingw/include\sys/types.hTRANSLATION_UNIT NoneTYPEDEF_DECL __builtin_va_ListSTRUCT_DECL _iobufTYPEDEF_DECL fileVAR_DECL _iobUNEXPOSED_DECL FUNCTION_DECL mainint main(){ printf(HELLO_WORLD); return 0;}
对于以下C代码:
#include <stdio.h>#define HELLO_WORLD "HELLO!"int main(){ printf(HELLO_WORLD); return 0;}
我想要的是在代码中为我的#define获取define_DECL HELLO_WORLD(目前我什么都没有).当然也可以得到类似的我的#include的声明.这可能吗?
编辑:基本上,我想解析文件,而不扩展预处理指令.
解决方法 前几天我在#llvm freenode irc频道上问了同一个问题.答案是“宏不是AST的一部分,所以你不能”,但最可能的是“-fSyntax-only”选项和clang插件而不是libclang可能会帮助你.编辑:看起来现在实际上是可以的,请参阅bradtgmurray的回答
总结以上是内存溢出为你收集整理的c – 检索有关预处理器指令的信息全部内容,希望文章能够帮你解决c – 检索有关预处理器指令的信息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)