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.
|