Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only function declaration from clang AST?

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.
like image 640
achala Avatar asked Nov 07 '22 11:11

achala


1 Answers

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!

like image 81
Valeriy Savchenko Avatar answered Nov 14 '22 09:11

Valeriy Savchenko