【发布时间】:2020-03-21 19:38:20
【问题描述】:
我想要 AST 用于我的 C 程序并希望以 json 格式表示。为此,我使用了clang -Xclang -ast-dump=json -fSyntax-only main.c 命令。它给出了 AST。但 AST 包含 typeDecl, Value declaration 等以及 function declaration。
我只想要 JSON 格式的代码中的函数声明。如何做到这一点?
这是一个替代clang-check -ast-dump -ast-dump-filter=main main.c,但这不能以 JSON 形式给出结果。当我执行此操作时,会收到一些错误消息以及此简单代码的输出
#include <stdio.h>
int main() {
printf("Hello from C!");
return 0;
}
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "main.c"
No compilation database found in /home/..../src or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
【问题讨论】:
-
令人惊讶的是,
clang -Xclang -ast-dump=json -fSyntax-only main.cpp不提供 AST 如果我们包含了标头。对于C程序,它工作正常。
标签: c clang abstract-syntax-tree