Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » How to visit a IASTTranslationUnit?
How to visit a IASTTranslationUnit? [message #168661] Mon, 24 April 2006 08:57 Go to next message
Eclipse UserFriend
Originally posted by: zhdf.os.pku.edu.cn

Hi,
I'm writing a plugin for CDT which will find a certain if-else statement
pattern. First I need to visit a IASTTranslationUnit to collect all the
if-else statements. So I write some code like this:

First, I write an ASTVisitor which extends ASTVisitor:

public class ASTTranslationUnitVisitor
extends ASTVisitor {
ArrayList ifStatements = new ArrayList();

public int visit(IASTStatement statement) {
System.out.println ( "in here");
if ( statement instanceof CASTIfStatement ) {
ifStatements.add( statement);
}
return PROCESS_CONTINUE;
}

public ArrayList getIfStatements ( ) {
return ifStatements;
}
}

Then I get the IASTTranslationUnit, and accept the visitor like this:

ITranslationUnit unit = (ITranslationUnit) unitIte.next();
// get ast for the translation unit
IASTTranslationUnit ast = null;
System.out.println ( unit.getResource().getName());
try {
ast = CDOM.getInstance().getTranslationUnit( (IFile)unit.getResource());
} catch ( UnsupportedDialectException e){
}
// sounds like this, but this interface has not implemented by CDT now, it
will only return null
// IASTTranslationUnit ast = unit.getASTTranslationUnit();

// to collect all if statement
ASTTranslationUnitVisitor translationUnitVisitor = new
ASTTranslationUnitVisitor( );

// visit the ast
ast.accept( translationUnitVisitor);

When I run this program, nothing happens. The "System.out.println ( "in
here");" has not been showed. I don't know what's wrong with my program.
Thanks very much.

Best regards,
Stephen Zhang
Re: How to visit a IASTTranslationUnit? [message #168671 is a reply to message #168661] Mon, 24 April 2006 15:18 Go to previous messageGo to next message
Eclipse UserFriend
Stephen Zhang wrote:
> Hi,
> I'm writing a plugin for CDT which will find a certain if-else statement
> pattern. First I need to visit a IASTTranslationUnit to collect all the
> if-else statements. So I write some code like this:

Stephen,

I can't help you with the internals of CDT parsing... I'll leave that to
the actual CDT guys. I'm curious about your actual intended use of
trying to "find a certain if-else statement pattern". I'm not wanting to
pry if this is proprietary product work, but just wanted to make you
aware of some work that MIGHT be of use to you: Check out the Eclipse
TPTP "C/C++ Code Review Provider" static code analysis work of Steve
Gutz (go to < http://www.eclipse.org/tptp > => Downloads => some recent
build => down towards the bottom in the Technology Preview area. If you
download "the feature", you'll find the
org.eclipse.tptp.platform.analysis.codereview.cpp framework and the
....cpp.rules package which has two rules built on that framework. This
past weekend I tried to build using those rules a third rule which would
look for statements along the lines of "if (lhs = rhs)" to catch the
common error of using = when == was meant. It took me about 30 minutes
using Steve's example rules to put together something which I think will
do what I want it to... but I got hung up in the "scaffolding", and his
tutorial (see the "Static Analysis Tutorial" parts 1 and 2 from the TPTP
=> Documentation => User Guides and Introductory Articles) is geared to
his (more extensive) Java static analysis than the "proof of concept"
C/C++ static analysis.

If this is of any interest to you, let me know and we'll get our heads
together with Steve and see how to proceed.
--
RDS

Randy D. Smith randy (dot) d (dot) smith (at) intel (dot) com
Eclipse TPTP Committer, Platform Proj (data collection/agent controller)
Re: How to visit a IASTTranslationUnit? [message #168708 is a reply to message #168671] Tue, 25 April 2006 10:43 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: zhdf.os.pku.edu.cn

Hi
Thanks for your reply.
I've solved my problem already. After debugging my plugin, I found that
to extend a ASTVisitor, you must set the member named "shouldVisit****", for
example, if I want to visit all Statements in a TranslationUnit, I have to
write my constructor like this:

public ASTTranslationUnitVisitor () {
super();
shouldVisitTranslationUnit = true;
shouldVisitStatements = true;
}

then, in the visit function for IASTStatement, I can collect all if-else
statement like this:

public int visit(IASTStatement statement) {
if ( statement instanceof CASTIfStatement ) {
ifStatements.add( statement);
}
return PROCESS_CONTINUE;
}
where ifStatement is an ArrayList.

I'm busy writing my own plugin, so I don't think I'll have time to help
you recently. But I do think when you collected all the if-else statements
like the codes above, It will be easy to resolve your problem of "look for
statements along the lines of "if (lhs = rhs)" to catch the common error of
using = when == was meant." without the help of TPTP.

Best regards,
Stephen
Zhang


"Randy D. Smith" <randy.d.smith@intel.com> wrote:
> I can't help you with the internals of CDT parsing... I'll leave that to
> the actual CDT guys. I'm curious about your actual intended use of trying
> to "find a certain if-else statement pattern". I'm not wanting to pry if
> this is proprietary product work, but just wanted to make you aware of
> some work that MIGHT be of use to you: Check out the Eclipse TPTP "C/C++
> Code Review Provider" static code analysis work of Steve Gutz (go to <
> http://www.eclipse.org/tptp > => Downloads => some recent build => down
> towards the bottom in the Technology Preview area. If you download "the
> feature", you'll find the

> RDS
>
> Randy D. Smith randy (dot) d (dot) smith (at) intel (dot) com
> Eclipse TPTP Committer, Platform Proj (data collection/agent controller)
Re: How to visit a IASTTranslationUnit? [message #168981 is a reply to message #168708] Sun, 30 April 2006 23:09 Go to previous message
Eclipse UserFriend
Hi Stephen,
I would just add one thing: You are checking that statement is
instanceof CASTIfStatement. You should check against IASTIfStatement
instead. The CASTIfStatement is the implementing class not the
interface, if you were parsing C++ code you would have a
CPPASTIfStatement instead.

-Andrew

Stephen Zhang wrote:
> Hi
> Thanks for your reply.
> I've solved my problem already. After debugging my plugin, I found that
> to extend a ASTVisitor, you must set the member named "shouldVisit****", for
> example, if I want to visit all Statements in a TranslationUnit, I have to
> write my constructor like this:
>
> public ASTTranslationUnitVisitor () {
> super();
> shouldVisitTranslationUnit = true;
> shouldVisitStatements = true;
> }
>
> then, in the visit function for IASTStatement, I can collect all if-else
> statement like this:
>
> public int visit(IASTStatement statement) {
> if ( statement instanceof CASTIfStatement ) {
> ifStatements.add( statement);
> }
> return PROCESS_CONTINUE;
> }
> where ifStatement is an ArrayList.
>
> I'm busy writing my own plugin, so I don't think I'll have time to help
> you recently. But I do think when you collected all the if-else statements
> like the codes above, It will be easy to resolve your problem of "look for
> statements along the lines of "if (lhs = rhs)" to catch the common error of
> using = when == was meant." without the help of TPTP.
>
> Best regards,
> Stephen
> Zhang
>
>
> "Randy D. Smith" <randy.d.smith@intel.com> wrote:
>
>>I can't help you with the internals of CDT parsing... I'll leave that to
>>the actual CDT guys. I'm curious about your actual intended use of trying
>>to "find a certain if-else statement pattern". I'm not wanting to pry if
>>this is proprietary product work, but just wanted to make you aware of
>>some work that MIGHT be of use to you: Check out the Eclipse TPTP "C/C++
>>Code Review Provider" static code analysis work of Steve Gutz (go to <
>>http://www.eclipse.org/tptp > => Downloads => some recent build => down
>>towards the bottom in the Technology Preview area. If you download "the
>>feature", you'll find the
>
>
>> RDS
>>
>>Randy D. Smith randy (dot) d (dot) smith (at) intel (dot) com
>>Eclipse TPTP Committer, Platform Proj (data collection/agent controller)
>
>
>
Previous Topic:Different build commands for different working sets
Next Topic:Getting started compiling an aerospace application with CDT on Linux
Goto Forum:
  


Current Time: Sat Jun 07 05:24:19 EDT 2025

Powered by FUDForum. Page generated in 0.03253 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top