如何仅从 clang AST 获取函数声明? [英] How to get only function declaration from clang AST?
问题描述
我想为我的 C 程序使用 AST,并希望以 json 格式表示.为此,我使用了 clang -Xclang -ast-dump=json -fSyntax-only main.c
命令.它给了一个AST.但是 AST 包含 typeDecl、Value 声明
等以及 function 声明
.
I want AST for my C program and want represent in json format. To do so I used clang -Xclang -ast-dump=json -fSyntax-only main.c
command. It gave a AST. but the AST contains typeDecl, Value declaration
etc. along with function declaration
.
我只需要一个函数声明,以 JSON 形式形成我的代码.怎样才能做到这一点?
I want only a function declaration form my code in JSON form. How can achieve this?
这是一个替代的 clang-check -ast-dump -ast-dump-filter=main main.c
但这不能以 JSON 形式给出结果.当我执行这个时得到了一些错误消息以及这个简单代码的输出
Here is an alternative clang-check -ast-dump -ast-dump-filter=main main.c
but this cant give the result in JSON form. and when I execute this got some error messages along with output for this simple code
#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 frontend
和 clang driver
之间的差异有关.它甚至在 docs 中都有介绍.
It is a well-known issue connected to the differences between clang frontend
and clang driver
. It is even covered in docs.
所以,运行 clang -### main.c
,复制所有 -internal-isystem
和 -internal-externc-isystem
选项并将它们添加到您运行以获取 AST 的命令中.
So, run clang -### main.c
, copy all -internal-isystem
and -internal-externc-isystem
options and add them to the command that you run to get AST.
希望这些信息对您有所帮助!
I hope this information will be helpful!
这篇关于如何仅从 clang AST 获取函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!