[cfe-users] Using Clang for CFG creation

Viktor Kaszian kaszian at cs.uni-bonn.de
Mon Mar 24 04:04:22 PDT 2014


Hi Jordan,

thanks for your advice. It took me a while to get through the 
LibTooling. By mostly copying the code from your link, i wrote up a few 
lines of code. However since i am just setting up for testing, I have 
not gotten around to test this yet.

However i have a few questions: buildCFG takes four parameters:
- a Decl
- a Stmt
- an ASTContext
- buildOptions

To be honest i am not exactly clear on what is what here.

I called it like this:

myCFG = CFG::buildCFG(inFile, getBody(), *Context, cfgBuildOptions);

which i mostly copied from

http://clang.llvm.org/doxygen/AnalysisDeclContext_8cpp_source.html#l00179

I will append my current code. You will notice that i am fairly novice 
in terms of coding. Please excuse any blatant mistakes.

Best regards,
Viktor


Am 19.03.2014 17:47, schrieb Jordan Rose:
> Both DumpCFG and ViewCFG are meant as debug tools for working on the
> Clang static analyzer; in particular, the way the CFG is built for the
> analyzer may not match your needs.
>
> If you are willing to write your own tool based on Clang's C++
> interface, you can get a parsed AST
> <http://clang.llvm.org/docs/RAVFrontendAction.html> and then ask for the
> CFG of any particular function using CFG::buildCFG. Note, though that
> the C++ API is not stable from release to release.
>
> Jordan
-------------- next part --------------
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"


using namespace clang;

class FindCFGConsumer : public ASTConsumer {
public:
    explicit FindCFGConsumer(ASTContext *Context)
		{ }

    virtual void HandleTranslationUnit(ASTContext &Context) {
        myCFG = CFG::buildCFG(inFile, getBody(), *Context, cfgBuildOptions);
    }
}


class FindCFGFrontendAction : public clang::ASTFrontendAction {
public:
  virtual clang::ASTConsumer *CreateASTConsumer(
    clang::CompilerInstance &Compiler, llvm::StringRef InFile) {
    return new FindCFGConsumer(&Compiler.getASTContext());
  }
};

int main(int argc, const char **argv) {
    
    clang::tooling::runToolOnCode(new FindCFGFrontendAction, argv[1]);
}


More information about the cfe-users mailing list