collecting source code information

Hey!

I'd like to write a program that collects statistics of OpenCL kernels. Examples
are the number of arithmetic operations, number and types of variables, etc. In
the future I'd also like to do some static analysis, for example to try and
derive memory access patterns.

I'm new to Clang, so I'm not quite sure how to go about this... It seems like
the RecursiveASTVisitor class could be a good start, as it allows you to
traverse the AST. Overloading VisitStmt() for example would allow me to count
the number of arithmetic expressions, right?
Unfortunately I can't figure out how to start the tree traversal. What is it
that I need to pass my visitor class in order to traverse the whole file or a
certain function?
Are there any examples that show a usage of RecursiveASTVisitor?

Thanks
Dominik

Hey!

I'd like to write a program that collects statistics of OpenCL kernels. Examples
are the number of arithmetic operations, number and types of variables, etc. In
the future I'd also like to do some static analysis, for example to try and
derive memory access patterns.

I'm new to Clang, so I'm not quite sure how to go about this... It seems like
the RecursiveASTVisitor class could be a good start, as it allows you to
traverse the AST. Overloading VisitStmt() for example would allow me to count
the number of arithmetic expressions, right?

Sure, although you probably want to overload VisitBinaryOperator/VisitUnaryOperator.

Unfortunately I can't figure out how to start the tree traversal. What is it
that I need to pass my visitor class in order to traverse the whole file or a
certain function?
Are there any examples that show a usage of RecursiveASTVisitor?

Check out lib/Frontend/BoostConAction.cpp

  - Doug

Thanks for your reply. I now started using CFG rather than ASTVisitor. It allows
me to iterate over basic blocks nicely so that I can see the actual structure of
the code. Is this also possible in ASTVisitor?

Another problem:
When I reach a variable that is a parameter of my function, it is described as a
DeclRefExpr in the CFG. However, if the variable is local it is not a
DeclRefExpr and I can't figure out what type of expression it is...

Cheers
Dominik

Another problem:
...

Problem solved... sorry

Where can I find RecursiveASTVisitor.hpp ? It's referred to as lib/AST/RecursiveASTVisitor.hpp in the above BoostConAction.cpp, but I can't see any such file in http://llvm.org/svn/llvm-project/cfe/trunk/lib/AST/.

Thanks,
         Stefan

Stefan Seefeld <stefan.seefeld@...> writes:

Where can I find RecursiveASTVisitor.hpp ? It's referred to as
lib/AST/RecursiveASTVisitor.hpp in the above BoostConAction.cpp, but I
can't see any such file in
http://llvm.org/svn/llvm-project/cfe/trunk/lib/AST/.

It's part of clang, not LLVM. Check out the clang repository (see
clang.llvm.org) and you'll find the RecursiveASTVisitor.h in
include/clang/AST/RecursiveASTVisitor.h

That's exactly what I did: http://clang.llvm.org/ has two menu items for "Browse svn" and "Browse VC" and the above is where they take me.

I now see that I was looking in libs/AST, not include/AST, which is why I didn't find what I was looking for.

Thanks,
         Stefan