Thank you for your fast answer.
Today I was reading the clang code and I'm now trying to implement an ASTConsumer, as I'll have to do a little more analysis that just checking function calls. I also need to know when a particular variable used in the function call is initialized or not as well as if it is not initialized by the function call (through a pointer) whether it is used afterwards or not. I still didn't know if this will be simple enough to do, but I'll try..

For now, I have just more two questions:
- is it possible to disable clang warnings and output just the warnings I'll be generating? (I added a -parse-ast-sirs opt for me) - can clang ignore parsing errors? e.g. if it can't find an header.h file can it continue even if a function is called without being declared. This is especially useful in my case because I'll be parsing the PHP sources (in C) and I don't have all the external libraries that some PHP extensions rely on, but I still would like to be able to analyse those extensions, though.

BTW, in attach you'll find a backtrace I got when doing executing 'clang -parse-ast-*'. It happens when I parse a PHP source file and when not all include (-I) paths are specified. I took a quick look at those functions, but I couldn't fix the bug myself.

Thanks,
Nuno


----- Original Message ----- From: "Chris Lattner" <[EMAIL PROTECTED]>
To: "Nuno Lopes" <[EMAIL PROTECTED]>
Cc: <[email protected]>
Sent: Saturday, October 06, 2007 7:27 PM
Subject: Re: [cfe-dev] usage of clang in an university project


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
clang: SemaDecl.cpp:772: virtual void* clang::Sema::ParseStartOfFunctionDef(clang::Scope*, 
clang::Declarator&): Assertion `CurFunctionDecl == 0 && "Function parsing 
confused"' failed.
./clang((anonymous namespace)::PrintStackTrace()+0x22)[0x83acfbc]
./clang((anonymous namespace)::SignalHandler(int)+0x110)[0x83ad280]
[0xb7f5d420]
/lib/libc.so.6(abort+0x108)[0xb7cffe58]
/lib/libc.so.6(__assert_fail+0xfc)[0xb7cf7f7c]
./clang(clang::Sema::ParseStartOfFunctionDef(clang::Scope*, 
clang::Declarator&)+0x38)[0x824fb50]
./clang(clang::Parser::ParseFunctionDefinition(clang::Declarator&)+0xba)[0x8296e3e]
./clang(clang::Parser::ParseDeclarationOrFunctionDefinition()+0x2ce)[0x8297236]
./clang(clang::Parser::ParseExternalDeclaration()+0x27f)[0x829786b]
./clang(clang::Parser::ParseExternalDeclaration()+0xf8)[0x82976e4]
./clang(clang::Parser::ParseTopLevelDecl(void*&)+0x3b)[0x82978af]
./clang((anonymous namespace)::ASTStreamer::ReadTopLevelDecl()+0x5f)[0x824538b]
./clang(clang::ParseAST(clang::Preprocessor&, unsigned int, 
clang::ASTConsumer&, bool)+0xdd)[0x8245533]
./clang[0x82119fe]
./clang(main+0x3bc)[0x8211e6e]
/lib/libc.so.6(__libc_start_main+0xd8)[0xb7ceb838]
./clang[0x81fed31]
Aborted
_______________________________________________
cfe-dev mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

Reply via email to