如何仅从cast AST获得函数声明? [英] How to get only function declaration from clang AST?
问题描述
我的C程序需要AST,并希望以json格式表示。为此,我使用了 clang -Xclang -ast-dump = json -f仅语法main.c
命令。它给了AST。但是AST包含 typeDecl,值声明
等,以及函数声明
。
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前端
和 clang驱动程序
之间。甚至在文档中也有介绍。
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希望这些信息对您有所帮助!
I hope this information will be helpful!
这篇关于如何仅从cast AST获得函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!