问题
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