[cfe-dev] How to add a function to an AST?
Joshua T
llvm.mailing at gmail.com
Mon Sep 9 09:06:08 PDT 2013
Hello Manuel,
I’m dealing with the same issue like maxs.
>> Generally speaking, the easy way is to add it in the code and let clang
>> parse the code (seriously, that's what works best, and has a couple of
>> other upsides - you can also do that programatically).
Do you prefer this workflow:
1. Add new lines into the buffer
2. Reparse the buffer and generate a complete new tree?
That seems like a really heavy workload for adding something new.
What are the other upsides? Sorry, I’m new to the Clang-API world :(.
>> Or to turn it around, the other question would be: why do you want to add
>> a function to the AST instead of adding it to the code? :)
Easy :-) I can add every line of code via the Rewriter class, however, it is
challenging to guarantee that my insertion is valid. For instance, I could
add this function: void foo(int a, int b int c) without any problem…
Besides syntax errors semantic errors can happen. For instance, a function
with the same name and same parameter list exists already in the scope…
On the other hand, inserting directly into the tree means that we can
evaluate the expression.
After the AST safeguards that everything is fine, we only have to use pretty
print function.
Okay, another example:
Lets say we would like to change the number of arguments of a FunctionDecl
by deleting one argument.
We make the assumption that at least one argument is in the parameter list.
Text editing requires a lot of effort.
If the function has one parameter, we can use the start and end location of
the parameter. No big deal.
If the function has many arguments, it is more complicated. We have to
delete the parameter like before and have to figure out the position of the
corresponding comma.
Doing this by editing the AST is much more simpler. Delete the argument in
the AST and call pretty print, right? Comma handing is the job of pretty
print, right?
I’m looking for your answer. It seems that you an AST pro.
Cheers!
Joshua :)
--
View this message in context: http://clang-developers.42468.n3.nabble.com/How-to-add-a-function-to-an-AST-tp4034314p4034342.html
Sent from the Clang Developers mailing list archive at Nabble.com.
More information about the cfe-dev
mailing list