How to represent clang AST in JSON format?
Asked Answered
P

1

10

clang-check -ast-dump -ast-dump-filter=<function_name> main.c gives a AST (only a function declaration) of the particular code.

How can we represent generated AST in JSON format?

PS: I Want AST for function declaration only.

Pikeman answered 29/11, 2019 at 10:21 Comment(0)
C
11

Call clang with the -ast-dump=json argument.

This was implemented only recently (May 2019) so you need an up-to-date version of Clang.

See https://reviews.llvm.org/D60910 for details.

There's also a library to export more lower-level information available via libTooling at https://github.com/facebook/facebook-clang-plugins

Update in 2022: The full command line is now clang -Xclang -ast-dump=json -fsyntax-only <file> since the clang command is now a compilation driver, not just the compiler itself.

Curtilage answered 19/12, 2019 at 23:0 Comment(5)
this -ast-dump=json is supported with clang but not with clang-check And this is working fine for C program not C++Pikeman
Using clang from 2021 and it doesn't work for me: clang.exe: warning: argument unused during compilation: '-ast-dump=json' [-Wunused-command-line-argument]Mclendon
@Mclendon Answer updated for the latest version of clang.Curtilage
@Curtilage The updated answer works perfectly, thank you! Its funny that it generates ~400 lines from a 3 line function.Mclendon
with filter: clang -fsyntax-only -Xclang -ast-dump=json -Xclang -ast-dump-filter=some_function some_source.ccPugilist

© 2022 - 2024 — McMap. All rights reserved.