Skip to content
systemcoding.dev
Toolchains

Dump the Clang AST with -Xclang -ast-dump

Print Clang's abstract syntax tree for a C file with clang -Xclang -ast-dump -fsyntax-only, no object file needed, using Clang 19.

2 min read
Summarize in AI

The AST is what Clang actually understood before it generated any code, and it shows implicit casts the source never spells. -ast-dump is a frontend option, so the driver needs -Xclang to forward it, and -fsyntax-only stops after Sema so nothing is written to disk.

add.cc
int add(int a, int b) { return a + b; }
Shell
clang -Xclang -ast-dump -fsyntax-only add.c | tail -n 10
Text
`-FunctionDecl 0x55d0c8a4b6c8 <add.c:1:1, col:39> col:5 add 'int (int, int)'
  |-ParmVarDecl 0x55d0c8a4b570 <col:9, col:13> col:13 used a 'int'
  |-ParmVarDecl 0x55d0c8a4b5f0 <col:16, col:20> col:20 used b 'int'
  `-CompoundStmt 0x55d0c8a4b868 <col:23, col:39>
    `-ReturnStmt 0x55d0c8a4b858 <col:25, col:36>
      `-BinaryOperator 0x55d0c8a4b838 <col:32, col:36> 'int' '+'
        |-ImplicitCastExpr 0x55d0c8a4b808 <col:32> 'int' <LValueToRValue>
        | `-DeclRefExpr 0x55d0c8a4b7c8 <col:32> 'int' lvalue ParmVar 0x55d0c8a4b570 'a' 'int'
        `-ImplicitCastExpr 0x55d0c8a4b820 <col:36> 'int' <LValueToRValue>
          `-DeclRefExpr 0x55d0c8a4b7e8 <col:36> 'int' lvalue ParmVar 0x55d0c8a4b5f0 'b' 'int'

The 0x... addresses change on every run. The two ImplicitCastExpr nodes are the point: each operand is an lvalue that is converted to an rvalue before the addition, a step the C standard describes and the source never shows.

The tail is there because the dump starts with the implicit typedefs for __int128_t and friends, about a dozen lines of noise before your code. For anything bigger than a toy, use the filter in the next snippet instead of tail.

Taggedclangllvm
Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.