[cfe-dev] Adding C++ code with a plugin

François-Xavier Bourlet bombela at gmail.com
Sun Jul 31 23:30:36 PDT 2011


Hi,

I am trying to do the same kind of things! And I ended up at the same
point as you. Glad that I am not alone trying to rewrite the AST
trough a plugin ;)

In my case what I want to do is add new custom attribute and modify
the AST in consequence.

C++0x propose a standard way to define compiler specific attribute using [[ ]]:

[[some_attr_namespace::attr]]
void myfunc(){};

etc.

I want to change the AST nodes decorated with some custom attribute.
As example, I would like to be able to transform something like that:

[[coroutine::generator]]
int range(int min, int max) {
  while (min != max) {
    yield(min++);
  }
}

to something like:

void range_(coroutine::Yielder<int> yield, int min, int max) {
  while (min != max) {
    yield(min++);
  }
}
coroutine::generator_builder<int (coroutine::Yielder<int> yield, int,
int)> range(&range_);

If it's necessary to patch clang to add a new kind of plugin/new
plugin feature to allow AST modification in the middle of the
compilation process, I am willing to try doing it!

Thanks a lots for any answers.

Thanks also for this wonderful C++ compiler guys. Really.

On Thu, Jul 21, 2011 at 8:50 AM, Benjamin Orgogozo
<benjamin.orgogozo at normalesup.org> wrote:
> Hello,
>
> I'm new to this mailing-list and to CLANG in general so please forgive me if I
> ask a quite stupid question...
>
> I've been reading a few mailing lists and webpages on CLANG now but I can't find
> a clear answer to my question so I hope someone here could at least point me to
> the correct resource.
>
> My goal is simple: I want to compile a C++ code but after automatically adding a
> few lines of code. Here are my question:
>
> - can (or want) I do that with a plugin? At the beginning I wanted to write a
> plugin to the front-end and change the AST but it seems (but I hope I'm wrong)
> that when I use the "load" and "plugin" command line option, I can't generate
> code at the end. Is it true or not?
>
> - How should I do this transformation? The code I want to add is really simple:
> I just want to add function calls in the constructor of classes with specific
> class members as parameters.
> For example, I want to transform:
>
> class A {
>  float f;
> };
> class foo {
>  int bar;
>
>  foo() {}
> };
>
> into
>
> class A {
>  float f;
> };
> class foo {
>  int bar;
>
>  foo() {baz(bar);}
> };
>
> It seems that I have two options:
>  * use a rewriter to add nodes within the AST, but it seems that it's only for
> source to source transformation (and I would like to avoid that if possible).
>  * use a TreeTransform which looks fine but I think I read somewhere that it
> modifiying the AST for compiling it later wasn't always a good idea.
>
>
>
> >From what I understood, it seems that I want to use treetransform, but I wonder
> if I can use it within a plugin *and* generate code with only one call to clang.
>
>
> Any comment and pointer is more than welcome!
>
> Thanks,
>
> --
>  Benjamin Orgogozo
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>



-- 
François-Xavier Bourlet




More information about the cfe-dev mailing list