<div dir="ltr">On Mon, Sep 9, 2013 at 1:30 PM, Manuel Klimek <span dir="ltr"><<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>></span> wrote:<br><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"><div dir="ltr"><div class="im">On Mon, Sep 9, 2013 at 6:06 PM, Joshua T <span dir="ltr"><<a href="mailto:llvm.mailing@gmail.com" target="_blank">llvm.mailing@gmail.com</a>></span> wrote:<br>
</div><div class="gmail_extra"><div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Manuel,<br>
<br>
I’m dealing with the same issue like maxs.<br>
<div><br>
>> Generally speaking, the easy way is to add it in the code and let clang<br>
>> parse the code (seriously, that's what works best, and has a couple of<br>
>> other upsides - you can also do that programatically).<br>
<br>
</div>Do you prefer this workflow:<br>
<br>
1. Add new lines into the buffer<br>
2. Reparse the buffer and generate a complete new tree?<br>
<br>
That seems like a really heavy workload for adding something new.<br>
What are the other upsides? Sorry, I’m new to the Clang-API world :(.<br></blockquote><div><br></div></div><div>It's not actually a "heavy" workflow - clang has some libraries that help making that workflow amazingly little code. You can look at clang-modernize, which uses exactly this procedure to migrate code to c++11 (and beyond ;)</div>
<div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
>> Or to turn it around, the other question would be: why do you want to add<br>
>> a function to the AST instead of adding it to the code? :)<br>
<br>
</div>Easy :-) I can add every line of code via the Rewriter class, however, it is<br>
challenging to guarantee that my insertion is valid. For instance, I could<br>
add this function: void foo(int a, int b int c) without any problem…<br>
Besides syntax errors semantic errors can happen. For instance, a function<br>
with the same name and same parameter list exists already in the scope…<br>
<br>
On the other hand, inserting directly into the tree means that we can<br>
evaluate the expression.<br>
After the AST safeguards that everything is fine, we only have to use pretty<br>
print function.<br>
<br>
Okay, another example:<br>
<br>
Lets say we would like to change the number of arguments of a FunctionDecl<br>
by deleting one argument.<br>
We make the assumption that at least one argument is in the parameter list.<br>
<br>
Text editing requires a lot of effort.<br>
If the function has one parameter, we can use the start and end location of<br>
the parameter. No big deal.<br>
If the function has many arguments, it is more complicated. We have to<br>
delete the parameter like before and have to figure out the position of the<br>
corresponding comma.<br>
<br>
Doing this by editing the AST is much more simpler. Delete the argument in<br>
the AST and call pretty print, right? Comma handing is the job of pretty<br>
print, right?<br></blockquote><div><br></div></div><div>Unfortunately this is not true.</div><div>1. inserting a node in the AST will not automatically "validate" it - afaik clang relies on its C++ analysis in Sema to only run into building correct ASTs; you can still add nodes to the AST, if you're really careful</div>
<div>2. the AST has undocumented invariants, so you might run into problems later that you don't even know of now</div><div>3. clang doesn't come with a pretty-printer that actually outputs "real code" (at least if I'm not completely missing something) - the pretty printers I know are made for debugging or diagnostics </div>
<div><br></div></div></div></div></blockquote><div>Actually, we do have such a pretty-printer... it just isn't good enough for production use. :) Try "clang -cc1 -ast-print".</div><div><br></div><div>-Eli</div>
</div><br></div></div>