[cfe-dev] How to add a function to an AST?
Eli Friedman
eli.friedman at gmail.com
Mon Sep 9 13:58:12 PDT 2013
On Mon, Sep 9, 2013 at 1:30 PM, Manuel Klimek <klimek at google.com> wrote:
> On Mon, Sep 9, 2013 at 6:06 PM, Joshua T <llvm.mailing at gmail.com> wrote:
>
>> 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 :(.
>>
>
> 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 ;)
>
>
>> >> 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?
>>
>
> Unfortunately this is not true.
> 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
> 2. the AST has undocumented invariants, so you might run into problems
> later that you don't even know of now
> 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
>
> Actually, we do have such a pretty-printer... it just isn't good enough
for production use. :) Try "clang -cc1 -ast-print".
-Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130909/a824fb9e/attachment.html>
More information about the cfe-dev
mailing list