【发布时间】:2012-09-21 16:02:42
【问题描述】:
考虑以下简单的头文件 demo.h:
#define PERSIST
struct Serialised
{
int someTransientValue ;
PERSIST int aNumberToPersist ;
};
我使用以下代码和 Clang 的 python API 来迭代标头:
import sys, clang.cindex
def callexpr_visitor(node, parent, userdata):
if node.location.file: print node.location.file, node.displayname, node.kind
return 2
tu = clang.cindex.Index.create().parse(sys.argv[1], args=['-x', 'c++'])
clang.cindex.Cursor_visit(tu.cursor, clang.cindex.Cursor_visit_callback(callexpr_visitor), None)
这会打印出 Clang 的 AST 的元素,产生以下输出:
demo.h 序列化 CursorKind.STRUCT_DECL
demo.h someTransientValue CursorKind.FIELD_DECL
demo.h aNumberToPersist CursorKind.FIELD_DECL
有谁知道我如何提取与名为“aNumberToPersist”的成员变量关联的预处理器声明?有没有更好的方法以在解析树中清晰显示的方式“标记”变量?
Xubuntu 12.04,clang 版本 3.1(tags/RELEASE_31/final),目标:x86_64-unknown-linux-gnu 线程模型:posix。
【问题讨论】:
-
以防万一您没有收到关于答案更改的通知(我记不得了),从今天开始,由于Michael Han 的提交,Clang 的主干版本将保留所有已解析的属性。
-
太棒了!谢谢你的记忆:)
标签: c++ parsing serialization clang c-preprocessor