Exports an LLVM control flow graph (CFG) to JSON. This pass is different from LLVM's standard CFG printer in that it captures both intra and inter procedural edges (i.e., function calls).
git clone https://github.com/adrianherrera/llvm-cfg-to-json.git
cd llvm-cfg-to-json
mkdir build
cd build
# If you have multiple LLVM versions installed, specify the one you want by
# setting LLVM_DIR; e.g., -DLLVM_DIR=`llvm-config-12 --cmakedir`
#
# This probably also requires setting CC/CXX
CC=clang CXX=clang++ cmake ..
clang -fplugin=/path/to/build/libLLVMCFGToJSON.so /path/to/src.c
If using autotools/make/etc., do
CFLAGS="-fplugin=/path/to/build/libLLVMCFGToJSON.so" CXXFLAGS="-fplugin=/path/to/build/libLLVMCFGToJSON.so" ./configure
make
Or CMake:
cmake -DCMAKE_C_FLAGS="-fplugin=/path/to/build/libLLVMCFGToJSON.so" -DCMAKE_CXX_FLAGS="-fplugin=/path/to/build/libLLVMCFGToJSON.so" ...
make
Using the results produced by the LLVM pass, calculate some interesting graph statistics (e.g., number of basic blocks, number of edges, and the graph eccentricity from the CFG's entry point). The script can also (optionally) produce a DOT graph of the CFG.
clang -fplugin=/path/to/build/libLLVMCFGToJSON.so /path/to/src.c
python cfg_stats.py `pwd`/cfg.*.json