How to get only function declaration from clang AST?

我的未来我决定 提交于 2019-12-21 17:44:10

问题


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.

I want only a function declaration form my code in JSON form. How can achieve this?

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.

回答1:


It is a well-known issue connected to the differences between clang frontend and clang driver. It is even covered in docs.

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!



来源:https://stackoverflow.com/questions/59050018/how-to-get-only-function-declaration-from-clang-ast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!