Search code examples
c++cclangabstract-syntax-tree

Clang ast-dump results in infinite loop


For some reason when I try to get the ast-dump of any C/C++ code with Clang, I get an infinite loop which eventually results in clang: error: linker command failed with exit code 1136 or some similar error code.

I'm running Windows 10 with Clang version 13.0 (Also tried Clang 12.0 with same issues). I run the command clang -Xclang -ast-dump filename.c.

I've tried multiple files, but at this point I'm just trying to get a hello world ast (which compiles and runs fine via clang).

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}

I'm sure I'm missing something simple, but I'd appreciate any help, thanks!


Solution

  • It's not an infinite loop if it terminates. :-)

    The dumped AST includes stdio.h, which you included, so it's quite big (about 800 lines, when I tried it).

    The error message is because the clang compiler (cc1) does not produce an object file when you pass it the -ast-dump option, but the clang driver (clang) doesn't know that. The driver is expecting to be able to link the generated object file into an executable, but it doesn't find the object file so it complains.

    Use

    clang -Xclang -ast-dump -fsyntax-only filename.c
    

    to terminate the driver after the AST has been dumped.