jay*_*ven 5 clang abstract-syntax-tree libclang
我写了一个测试程序(parse_ast.c)来解析ac源文件(tt.c)来看看libclang是如何工作的,输出是AST的层次结构:
这是测试文件:
/* tt.c */ // line 1
#include <unistd.h>
#include <stdio.h>
typedef ssize_t (*write_fn_t)(int, const void *, size_t);
void indirect_write(write_fn_t write_fn) { // line 7
(*write_fn)(1, "indirect call\n", 14);
}
void direct_write() { // line 11
write(1, "direct call\n", 12); // line 12 mising in the ast?
}
int main() { // line 15
direct_write();
indirect_write(write); // line 17 missing in the ast?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出显示如下:
...
...
inclusion directive at tt.c (2, 1) to (2, 20)
inclusion directive at tt.c (3, 1) to (3, 19)
TypedefDecl at tt.c (5, 1) to (5, 57)
TypeRef at tt.c (5, 9) to (5, 16)
ParmDecl at tt.c (5, 31) to (5, 35)
ParmDecl at tt.c (5, 36) to (5, 49)
ParmDecl at tt.c (5, 50) to (5, 56)
FunctionDecl at tt.c (7, 1) to (9, 2)
ParmDecl at tt.c (7, 21) to (7, 40)
TypeRef at tt.c (7, 21) to (7, 31)
CompoundStmt at tt.c (7, 42) to (9, 2)
CallExpr at tt.c (8, 5) to (8, 42)
UnexposedExpr at tt.c (8, 5) to (8, 16)
ParenExpr at tt.c (8, 5) to (8, 16)
UnaryOperator at tt.c (8, 6) to (8, 15)
UnexposedExpr at tt.c (8, 7) to (8, 15)
DeclRefExpr at tt.c (8, 7) to (8, 15)
IntegerLiteral at tt.c (8, 17) to (8, 18)
UnexposedExpr at tt.c (8, 20) to (8, 37)
UnexposedExpr at tt.c (8, 20) to (8, 37)
StringLiteral at tt.c (8, 20) to (8, 37)
IntegerLiteral at tt.c (8, 39) to (8, 41)
FunctionDecl at tt.c (11, 1) to (13, 2)
CompoundStmt at tt.c (11, 21) to (13, 2) <- XXX no line 12?
FunctionDecl at tt.c (15, 1) to (20, 2)
CompoundStmt at tt.c (15, 12) to (20, 2)
CallExpr at tt.c (16, 5) to (16, 19)
UnexposedExpr at tt.c (16, 5) to (16, 17)
DeclRefExpr at tt.c (16, 5) to (16, 17) <- XXX no line 17?
ReturnStmt at tt.c (19, 5) to (19, 13)
IntegerLiteral at tt.c (19, 12) to (19, 13)
Run Code Online (Sandbox Code Playgroud)
我们可以看到三个函数(第 7 行的 direct_write/第 11 行的 indirect_write/第 15 行的 main)在那里,大多数语句都可以在 AST 中找到,但我找不到任何代表第 12 行中的语句的东西,第 17 行。有人知道原因吗?
我在 debian 2.6.32 上,在 clang 3.1 和 3.2(从源代码编译)上进行了测试。
这是程序 parse_ast.c:
/* tt.c */ // line 1
#include <unistd.h>
#include <stdio.h>
typedef ssize_t (*write_fn_t)(int, const void *, size_t);
void indirect_write(write_fn_t write_fn) { // line 7
(*write_fn)(1, "indirect call\n", 14);
}
void direct_write() { // line 11
write(1, "direct call\n", 12); // line 12 mising in the ast?
}
int main() { // line 15
direct_write();
indirect_write(write); // line 17 missing in the ast?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更新:
问题是由于缺少头文件 stddef.h,它在 libclang 的邮件列表中得到了回答http://clang-developers.42468.n3.nabble.com/libclang-missing-some-statements-in-the-AST-td4029641 .html
检查生成的诊断clang_parseTranslationUnit()- 即使遇到错误,也会生成 AST,但显然不能保证它有意义。
我发现注释掉这些#include行会导致编译错误,但生成的 AST 与您的类似(具体来说,缺少第 17 行)。
将这些#include行替换为size_tand的 typedef ssize_t(如int)会导致有关 的隐式声明的编译警告write(),但 AST 包含第 17 行。
因此,我假设您的头文件中存在问题,诊断应该会揭示该问题。可以通过以下方式检索诊断信息:
for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I) {
CXDiagnostic Diag = clang_getDiagnostic(TU, I);
CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions());
fprintf(stderr, "%s\n", clang_getCString(String));
clang_disposeString(String);
}
Run Code Online (Sandbox Code Playgroud)