Is it possible to print Clang's ast in a minimal presentation?

155 Views Asked by At

When I have

int main()
{
;;
    {return 0;}

the command clang -cc1 -ast-print test.c will print

int main() {
    ;
    ;
    {
        return 0;
    }
}

Is it possible to have Clang parse the file into an AST, and then print it with minimum scoping and ;s? So

int main()
{
    return 0;
}

If a different tool can do it, I am also interested.

1

There are 1 best solutions below

0
Emily-TTG On

(Neither of these methods are perfect for their respective cases but in a general sense they achieve what you want)

As the answer here shows - reconstructing the source from a consumed TU with libclang will strip out anything that doesn't make it into the AST: Can I print all lines of c++ code using clang.cindex parsing in Python?

If you want the AST - this Github Gist uses Python with the libclang CIndex interface to print out the AST: https://gist.github.com/ldzm/53b6d386ebbbed799b32e238fa052548