<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>Le 2013/01/16 à 18:13, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div class="gmail_default" style="">On Wed, Jan 16, 2013 at 7:39 AM, Antoine Trouve <span dir="ltr"><<a href="mailto:trouve@isit.or.jp" target="_blank">trouve@isit.or.jp</a>></span> wrote:<br></div><div class="gmail_extra">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear,<br>
<br>
Regarding the below topic, I am now trying to convert my tool using the RefactoringTool.<br>
<br>
I am using a RecursiveASTVisitor, but I can't find any way (other than using a global variable) to pass the replacement list to my ASTVisitor.<br>
Indeed, when I create my RecursiveASTVisitor object (inside ASTFrontendAction.CreateASTConsumer), I only have access to the Compiler object, not the tool object.<br>
However, the only way I could find to get the replacement list was through RefactoringTool->getReplacements().<br>
<br>
Did I miss something ?<br>
Is there any way to make my RecursiveASTVisitor aware of the Replacement list ?<br>
<br>
All the examples I could find on the repository use a MatchFinder with a custom callback that is aware of the replacement list.<br>
Was the RefactoringTool API designed with this very specific use in mind ?<br></blockquote><div><br></div><div style="">The Tool/RefactoringTool interface takes a tooling::FrontendActionFactory, so you have two options (we have some convenience methods in Tooling.h around line 66-100):</div>
<div style=""><br></div><div style="">struct MyFrontendActionFactory : public FrontendActionFactory {</div><div style="">  MyFrontendActionFactory(tooling::Replacements* Replaces) : Replaces(Replaces) {}</div><div style="">  virtual clang::FrontendAction *create() {</div>
<div style="">    // Give the replacements to your frontend action and from there to your ASTConsumer</div><div style="">  }</div><div style="">  tooling::Replacements* Replaces;</div><div style="">};</div><div style=""><br></div><div style="">
Now with convenience methods you can also just do (note that you'll need the MyASTConsumer part anyway):</div><div style="">struct MyASTConsumer : public ASTConsumer {</div><div style="">  MyASTConsumer(tooling::Replacements* Replaces); // otherwise like above</div>
<div style="">};</div><div style="">// This doesn't need to be an interface, simply having the method newASTConsumer will be enough...</div><div style="">struct MyConsumerFactory {</div><div style="">  MyConsumerFactory(tooling::Replacements* Replaces);</div>
<div style="">  clang::ASTConsumer *newASTConsumer() { return new MyASTConsumer(Replaces); }</div><div style="">  tooling::Replacements* Replaces;</div><div style="">};</div><div style="">and where you create the RefactoringTool:</div>
<div style="">RefactoringTool Tool(...);</div><div style="">MyConsumerFactory ConsumerFactory(Tool.getReplacements());</div><div style="">return Tool.runAndSave(newFrontendActionFactory(&ConsumerFactory));</div><div style=""><br>
</div><div style="">Note that I didn't write out everything for the first version, as it's more code :) Also, I wrote all that without trying to compile it, so there will be compile errors, but the principle should work…</div></div></div></div></blockquote><div><br></div><div>Thank you so much for your comprehensive answer !</div><div>I got a bit lost with all these factories of factories, but it finally works !</div><div><br></div><div>- Antoine</div><br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">
<div style=""><br></div><div style="">Cheers,<br></div><div style="">/Manuel</div><div style=""><br></div><div style=""><br></div><div style=""> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br>
Regards,<br>
<br>
- Antoine<br>
<br>
Le H.25/01/10 à 18:33, Antoine Trouve <<a href="mailto:trouve@isit.or.jp">trouve@isit.or.jp</a>> a écrit :<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> Le H.25/01/10 à 18:25, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> a écrit :<br>
><br>
>> On Thu, Jan 10, 2013 at 10:22 AM, Antoine Trouve <<a href="mailto:trouve@isit.or.jp">trouve@isit.or.jp</a>> wrote:<br>
>><br>
>> Le H.25/01/10 à 18:03, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> a écrit :<br>
>><br>
>>>> On Thu, Jan 10, 2013 at 8:44 AM, Antoine Trouve <<a href="mailto:trouve@isit.or.jp">trouve@isit.or.jp</a>> wrote:<br>
>>>> Hello everybody,<br>
>>>><br>
>>>> I have successfully written a source-to-source translator using LibTooling, and I am now willing to go further and do some source-to-source translation.<br>
>>>> However, I can't figure out how to output modified code with LibTooling.<br>
>>>><br>
>>>> I have used in the past the class "Rewriter", coupled with a "RewriterBuffer" to do this job, but without the LibTooling (I was manipulating "CompilerInstance" object directly).<br>

>>>> However, a "RewriterBuffer" requires a file ID (that I used to get from a "SourceManager") to be created, and I don't have access to such information anymore, because I'm calling my tool using "Tool.run" from a custom "FrontEndAction".<br>

>>>><br>
>>>> I've spotted the example called "loop-convert" in the clang-tools-extra repository, that is using class derived from "RefactoringTool" to do source-to-source transformation. I kind of made sense of this code, but the way it works would require me to change my code deeply in order to fill the so-called replacement list.<br>

>>>><br>
>>>> Does someone has any experience on this matter in order to give some advices on the best approach I should adopt ?<br>
>>><br>
>>> I'm not 100% sure what you're looking for - as you said, the RefactoringTool is what's meant to be used to write out changed code - I'm open to changes to that interface if it helps your use case, but I don't know your code, so it's hard for me to tell exactly why that would be a deep change, or how to fit it into your project.<br>

>>><br>
>>> My intuition would be that at the point at which you would make changes vie the Rewriter, you would instead fill the Replacements...<br>
>>><br>
>>> Cheers,<br>
>>> /Manuel<br>
>><br>
>> Thank you for your answer. Basically I would like to know what is the *standard* way to do source-to-source translation in Libtooling.<br>
>> Should I understand that this is by using "RefactoringTool" ? Updating my code would require some effort for me to understand how the "Replacements" work, so I want to be sure that this is not to find out that there is a better way to do so.<br>

>><br>
>> To answer your question, I detect "for" statements; analyse the body of the loop to extract some metrics; and add a comment after the for with the values of the previously measured metrics. I used to use the "Rewriting" class to add this comment.<br>

>><br>
>> Ah, yes, RefactoringTool is the intended "standard" way to do source-to-source with libtooling. I think if you already use the Rewriter, I'll expect it to actually be not hard to change to using the Replacements :)<br>

>><br>
>> Cheers,<br>
>> /Manuel<br>
><br>
> OK. Thank you for you fast answers.<br>
> I'll convert my code then, and kindly come back to you if I have any issue.<br>
><br>
> Best regards,<br>
><br>
> - Antoine<br>
><br>
>><br>
>><br>
>> Regards,<br>
>><br>
>> - Antoine<br>
>><br>
>>>> Best regards,<br>
>>>><br>
>>>> - Antoine<br>
>>>> _______________________________________________<br>
>>> cfe-dev mailing list<br>
>>> <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
>>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
>>><br>
>><br>
>><br>
><br>
<br>
</div></div></blockquote></div><br></div></div>
</blockquote></div><br></body></html>