How to modify the AST and flush the changes back to the source code [message #231395] |
Tue, 10 March 2009 15:50 |
Eclipse User |
|
|
|
Originally posted by: qkevin.chen.gmail.com
Hi,
I am wondering is there any way to modify the AST and make changes back
to the source. The ASTRewrite seems to be not working at all.
I did a bit of search and found out this link.
> [news.eclipse.tools.cdt] Re: ASTRewrite - Editing cpp Files with cdt
>
> * From: Andrew Niefer <aniefer@xxxxxxxxxx>
> * Date: Tue, 06 Jun 2006 13:12:24 -0400
> * Newsgroups: eclipse.tools.cdt
> * Organization: EclipseCorner
> * User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)
>
> Currently the CDT DOM is not "rewritable". I think the best you can do now is to use the location information on the IASTNodes to make your changes directly back to the file.
>
> You might want to try on the cdt-dev mailing list, there may be others investigating the rewrite problem.
>
> -Andrew
>
>
> Sebastian wrote:
>
> Hi,
> I'm looking for a class similar to the jdt "ASTRewrite" but for cpp to be
> able to
> edit cpp files.
>
>
> this is the jdt ASTRewrite class:
> org.eclipse.jdt.core.dom.rewrite.ASTRewrite
> see:
> http://help.eclipse.org/help31/topic/org.eclipse.jdt.doc.isv /reference/api/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.h tml
>
>
> I need to manipulate the tree of a cpp file for editing. Is there such a
> class for cdt? (otherwise, what should I use?)
>
>
> Thanks,
> Sebastian
Right now it seems that we already have a ASTRewrite from
org.eclipse.cdt.core.dom.rewrite. I did a small test on it and it is not
working.
Suppose we want to add a statement inside a method body such that
int function(){
return 1;
}
would become
int function(){
functionEnter();
return 1;
}
In my code, I would do something like this,
public void instrumentMethod(IASTFunctionDefinition function) {
IASTStatement methodBody = function.getBody();
IASTCompoundStatement oldMethodBody =(IASTCompoundStatement) methodBody;
IASTStatement[] compoundChildren = oldMethodBody.getStatements();
IASTNode enterNode = astRewrite.createLiteralNode(
"\" functionEnter() \" ");
astRewrite.insertBefore(oldMethodBody, compoundChildren[0], enterNode,
null);
}
And I would call astRewrite.rewriteAST() later in my code.
However, this simply has no effect on the source.
Your help will be immensely appreciated.
Qichang
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03167 seconds