[cfe-dev] usage of clang in an university project

Chris Lattner clattner at apple.com
Sat Oct 6 11:27:37 PDT 2007


On Oct 6, 2007, at 11:06 AM, Nuno Lopes wrote:
> I was assigned a project for the security class in the university that
> consists in doing some analisys of varargs function calls (C only).
> Basically I need some form of an AST of a C file and a way to  
> transverse it.

Sure, clang can do that very easily.

> Do you think that clang is the best tool for the job? (btw, it has  
> to be
> delivered in mid-December). I already took a look to other  
> projects, like
> ANTLR, GCCXML, a flex+bison grammar I found on the web, and even  
> the dump of
> gcc -fdump-translation-unit, but noone seems to be appropriate  
> (they have a
> big learning curve or they don't work well enough).
>
> So if you think that clang might do the job, can you please give me  
> some
> pointers to how to get started?

The easiest thing to do is to get clang and build it.  Then run the - 
parse-ast-print option.  The code for this is implemented in the  
clang/AST/StmtPrinter.cpp file.  That will give you an overview of  
using the AST for something simple.

For you, you don't want to process all nodes, just calls.  Given a  
Stmt for a function body, to walk the AST, you should be able to do  
something like this:

void WalkAST(Stmt *S) {
   if (S == 0) return;

   if (CallExpr *Call = dyn_cast<CallExpr>(S)) {
     // Look at this call.
   }

   for (Stmt::child_iterator I = S->child_begin(), E = S->child_end 
(); I != E; ++I)
     WalkAST(*I);
}

-Chris



More information about the cfe-dev mailing list