basic block traversal for conditions and loops

Hello,

I am working for static analysis of C programs and I have CFG generated from Clang.

I want to know how to traverse basic blocks for if-else and while loop - a common method that will work for both given a CFG.

Is there any piece of Clang code I can make use of? or any pointers?

Thank you.

- Rajendra

Any pointers on how to traverse CFG for loops? or How to find entry and exit
point of loop?
Any clang source code where it does so?

CFG is a graph and llvm GraphTraits for it exist. This should allow you to use llvm graph traversal algorithms.

You could also traverse it directly by relying on the iterators of CFG and CFGBlock. For example, UninitializedValues.cpp uses such iterators:
    for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
      unsigned BlockID = Block->getBlockID();
      ...
        for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
             E = Block->succ_end(); I != E; ++I) {

Unlike LLVM, we do not have any direct representation of loops in clang. However, we do have support for dominators, see Dominators.h.

You can visualize the CFG with DebugChecker, which should be mentioned in clang/docs/analyzer/DebugChecks.txt.

Anna.