[LLVM-Clang CFG] - generating CFG of a C program and llvm iterators


I want to generate Control Flow Graph (in memory CFG object) of any
given C program and then iterate over this CFG (all function, basic
blocks and instructions) using llvm iterators.

I can see text output of CFG on screen, using following command:
`clang -cc1 -Idummyh -analyze -analyzer-checker=debug.DumpCFG hello/hello.c'
where dummyh has en empty stdio.h to remove this error:
`fatal error: 'stdio.h' file not found'

I see CFG.h and CFG.cpp that clang uses and have seen some docs on
ModulePass, FunctionPass, BasicBlockPass, etc. But I have not figured
out how to put together things to generate CFG and then write iterators.

All help is much appreciated.

Thank you.


Hi Rajendra, you want to get hold of the LLVM IR produced by clang (use the
-flto option to have clang produce LLVM IR), and pipe it to the "opt" command
line tool, into which you will have loaded your logic as a plugin pass. So I
suggest you read about writing plugins for opt.

Ciao, Duncan.