Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Using ASTRewriter to reflect intermediate changes to the AST and perform further rewrites

That depends on the specific problem. If you perform all changes on the AST of the translation unit you can just apply the modifications on its ASTRewrite in a loop. If you have changes to subtrees that have been an argument of a previous change (insert/replace), you better use recursion and perform further modifications on the resulting ASTRewrite of the previous change. This might be done with an AST visitor.




Von: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> im Auftrag von Krishna Narasimhan <krishna.nm86@xxxxxxxxx>
Gesendet: Mittwoch, 8. Juni 2016 09:53
An: CDT General developers list.
Betreff: Re: [cdt-dev] Using ASTRewriter to reflect intermediate changes to the AST and perform further rewrites
 
Okay, but this logic isnt extendible to a generic version of incremental unknown number of updates. I am looking for something like this :

changedAST = PerformChange to AST, 
changedAST = performchange2 to changedAST
...
...
...

changedAST = performChangen to changedAST



On Wed, Jun 8, 2016 at 9:51 AM, Corbat Thomas <thomas.corbat@xxxxxx> wrote:

Well, you always need to have the tree structure in mind. To add a parameter I guess you need to add it to the function declarator. I'm not sure whether this is supported by the framework as is, but for the explanation it does not really matter. Conceptually it would be as follows:


ASTRewrite rewrite = /*rewrite for translation unit*/;

ICPPASTFunctionDeclarator declaratorNode = ...;

ICPPASTParameterDeclaration newParameter1 = ...;

ASTRewrite parameterRewrite = rewrite.insertBefore(declaratorNode, null, newParameter1);


This will (should) add the newParameter1 to the parameter list. Now you have two ASTRewrites (rewrite and parameterRewrite). "rewrite" is responsible for all further changes to the whole translation unit EXCEPT all changes made to the inserted subtree, of which newParameter1 is the root node. For all further changes to the subtree rooted by newParameter1 you need to use "parameterRewrite". So if you want to add another parameter you need to use "rewrite". An ASTRewrite can take any number of changes.


ICPPASTParameterDeclaration newParameter2 = ...;

rewrite.insertBefore(declaratorNode, null, newParameter2);


On the other hand if you wanted to modify the name of the inserted newParameter1 node, you would have to go with "parameterRewrite".


IASTName oldParameter2Name = ...;

IASTName newName = ...;

parameterRewrite.replace(oldParameter2Name, newName);



Regards

Thomas




Von: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> im Auftrag von Krishna Narasimhan <krishna.nm86@xxxxxxxxx>
Gesendet: Mittwoch, 8. Juni 2016 09:03

An: CDT General developers list.
Betreff: Re: [cdt-dev] Using ASTRewriter to reflect intermediate changes to the AST and perform further rewrites
 
I still am unclear. Will this allow me to make changes to the "changed" parent AST. Lets say I have a particular functionCall. And I want to add two transforms to it, one after the other that would each add one parameter (or simply just one character to the name of the function being called). The requirement sounds silly, as why not just add the two parameters at one go. But assume this requirement. Is there way to do it in CDT.... And if there is, is there a more generic way to incrementally make changes to the ROOT ast, where the changes to the root AST are reflected as and when we do the rewrite.



On Wed, Jun 8, 2016 at 8:58 AM, Corbat Thomas <thomas.corbat@xxxxxx> wrote:

Oh, I might have been unclear about the relation of the individual ASTRewrites. When you receive a new ASTRewrite from an insert/replace call the returned ASTRewrite is a "child" of the original ASTRewrite. When you create the changes of the parent ASTRewrite​ all changes to its child ASTRewrites are present.


ASTRewrite parent = ...;

ASTRewrite child1 = parent.insertBefore(..., node1);

ASTRewrite child2 = parent.insertBefore(..., node2);

ASTRewrite child3 = child1.replace(node3, node4);


Change change = parent.rewriteAST();


The change object contains all modifications above.


Hope this helps!

Regards

Thomas



Von: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> im Auftrag von Krishna Narasimhan <krishna.nm86@xxxxxxxxx>
Gesendet: Mittwoch, 8. Juni 2016 08:25

An: CDT General developers list.
Betreff: Re: [cdt-dev] Using ASTRewriter to reflect intermediate changes to the AST and perform further rewrites
 

Mm. It makes the challenges clear. But are there simple steps i could follow to make sure every rewrite operation updates the existinf rewrite with the new rewrite?

For example

Astrewrite rewrite = getrewrterfor ast ()

rewrite = rewrite.insertbefore...

rewrite = rewrite.replace....

Wont work as it returns the rewrite of the subtree and not the main tree. Any way to work around this?

On Jun 8, 2016 8:01 AM, "Corbat Thomas" <thomas.corbat@xxxxxx> wrote:

The capabilities of the ASTRewrite basically work as follows: You can remove, replace and insert nodes. The methods for replacing and inserting nodes return a new ASTRewrite object specifically for the subtree that has been inserted (or that is the replacement for another node respectively). This API is required as the arguments of those methods can either be new AST nodes or nodes that already exist in the same AST. In order to avoid modifications of an inserted subtree in two locations you get an ASTRewrite for every occurrence of the subtree.


An example to make it more clear:

- You have a function definition, known by its AST node...


ICPPASTFunctionDefinition fun = ...;


- ...and you want to create a clone of this function with a different name, ...


ASTRewrite rewrite = ...;

ASTRewrite insertionRewrite = rewrite.insertBefore(fun.getParent(), fun, fun);


- ... the insertBefore method call duplicates the function definition (inserts it again before the already existing definition). Now if you would like to make a change to that newly inserted function definition (and only to that definition) you need to use the insertionRewrite object to make these changes. Othwerwise, if there was only one rewrite object it would either affect both definitions of the function or only the original definition.


However, the infrastructure provided by CDT does not support working on a modification, description represented by an ASTRewrite, in the same way as if it was parsed from a source file. It lacks information about name bindings.


I hope this helps.

Regards

Thomas





Von: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx> im Auftrag von Krishna Narasimhan <krishna.nm86@xxxxxxxxx>
Gesendet: Dienstag, 7. Juni 2016 00:02
An: CDT General developers list.
Betreff: Re: [cdt-dev] Using ASTRewriter to reflect intermediate changes to the AST and perform further rewrites
 

Would be really nice if you could send an example if u bump into it.

Not obligatory. But thanks a lot anyway

On Jun 6, 2016 11:55 PM, "Sergey Prigogin" <eclipse.sprigogin@xxxxxxxxx> wrote:
This is done by creating hew rewriters based on the previously created rewriters. Don't remember examples from the top of my head, but should be able to find a few in the refactoring code.

-sergey

On Mon, Jun 6, 2016 at 2:11 PM, Krishna Narasimhan <krishna.nm86@xxxxxxxxx> wrote:
Hi,
   Is there a possibility for me to incrementally transform an AST in Eclipse CDT. It seems like ASTRewriter functions donot reflect the actual changes till the complete refactoring is done. Can I change the AST in the between, so that the following changes happen on the changed AST and not on the original AST,

Regards
Krishna

_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev


_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev

_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev

_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev


_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev


Back to the top