当我使用libclang的Python绑定解析C ++代码文件时,发现跨文件引用的游标丢失了。
示例C ++代码:
#include "controller/LocalController.h"
int addition(int a, int b)
{
return a + b;
}
auto c = addition(1, 2);
auto d = LocalController::getTextByKey("100000");
Python脚本:
index = clang.cindex.Index.create()
tu = index.parse("MazeController.cpp", clang_args, None, 1)
for child in tu.cursor.walk_preorder():
if child.location.file != None:
if child.location.file.name == tu.cursor.displayname:
print child.displayname, child.kind, child.location.line, child.location.column
变量clang_args包含“ controller / LocalController.h”的基本路径。下面是输出:
controller/LocalController.h CursorKind.INCLUSION_DIRECTIVE 1 4
addition(int, int) CursorKind.FUNCTION_DECL 3 5
a CursorKind.PARM_DECL 3 18
b CursorKind.PARM_DECL 3 25
CursorKind.COMPOUND_STMT 4 1
CursorKind.RETURN_STMT 5 5
CursorKind.BINARY_OPERATOR 5 12
a CursorKind.UNEXPOSED_EXPR 5 12
a CursorKind.DECL_REF_EXPR 5 12
b CursorKind.UNEXPOSED_EXPR 5 16
b CursorKind.DECL_REF_EXPR 5 16
c CursorKind.VAR_DECL 8 6
addition CursorKind.CALL_EXPR 8 10
addition CursorKind.UNEXPOSED_EXPR 8 10
addition CursorKind.DECL_REF_EXPR 8 10
CursorKind.INTEGER_LITERAL 8 19
CursorKind.INTEGER_LITERAL 8 22
d CursorKind.VAR_DECL 10 6
libclang可以完美地解析加法的本地函数调用,但是缺少对LocalController :: getTextByKey的跨文件函数调用的解析,因为输出的最后一行是d的光标。
有人可以帮助我吗?非常感谢!