<div dir="ltr">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 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">Hello Manuel,<br>
<br>
I’m dealing with the same issue like maxs.<br>
<div class="im"><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>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> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
>> 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>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>4. rewriting text is easy - in fact, we've written a whole library as part of clang (libTooling) to make it as easy as possible; the tools you find in clang's extra tools repository are written on top of that architecture</div>
<div><br></div><div>Cheers,</div><div>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I’m looking for your answer. It seems that you an AST pro.<br>
<br>
Cheers!<br>
<br>
Joshua :)<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://clang-developers.42468.n3.nabble.com/How-to-add-a-function-to-an-AST-tp4034314p4034342.html" target="_blank">http://clang-developers.42468.n3.nabble.com/How-to-add-a-function-to-an-AST-tp4034314p4034342.html</a><br>

Sent from the Clang Developers mailing list archive at Nabble.com.<br>
<div class="HOEnZb"><div class="h5"><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>
</div></div></blockquote></div><br></div></div>