Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How to modify the AST and flush the changes back to the source code
How to modify the AST and flush the changes back to the source code [message #231395] Tue, 10 March 2009 15:50 Go to next message
Eclipse UserFriend
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
Re: How to modify the AST and flush the changes back to the source code [message #849334 is a reply to message #231395] Thu, 19 April 2012 04:01 Go to previous messageGo to next message
new2CDT . is currently offline new2CDT .Friend
Messages: 4
Registered: April 2012
Junior Member
Hi all,

I am also facing the same problem.
Did anybody find its solution?
If yes, please post the solution here.

Thanks in advance.

[Updated on: Fri, 20 April 2012 06:26]

Report message to a moderator

Re: How to modify the AST and flush the changes back to the source code [message #1015214 is a reply to message #849334] Wed, 27 February 2013 19:01 Go to previous message
Erika Redmark is currently offline Erika RedmarkFriend
Messages: 22
Registered: January 2013
Junior Member
I stumbled across this thread whilst attempting to find a solution to a related problem I am having, but I can answer your question (hopefully it is still relevant after several months)

The doc for ASTRewrite.rewriteAST states

"Converts all modifications recorded by this rewriter into the change object required by the refactoring framework."

On the returned Change object, you have to call Change.perform. This performs the change and returns back another Change object that acts as an 'Undo'.

Given the age of this topic, I hope this helps at least someone Smile
Previous Topic:SDL errors appearing only when I open source files
Next Topic:How to insert method comment block?
Goto Forum:
  


Current Time: Thu Apr 25 11:18:30 GMT 2024

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

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

Back to the top