How to visit a IASTTranslationUnit? [message #168661] |
Mon, 24 April 2006 08:57  |
Eclipse User |
|
|
|
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 #168708 is a reply to message #168671] |
Tue, 25 April 2006 10:43   |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
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)
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.03253 seconds