Adding C++ code with a plugin

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,

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 :wink:

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.

Hi,
   If I understand correctly perhaps you are looking for clang::ASTConsumer, clang::SemaConsumer.
   If I were you I would create clang::MultiplexConsumer and attach my custom consumer before any other consumer. I'd transform the body of the constructor of the declaration (It should be appear as HandleTagDeclDefinition) that interests me in there. In cling we do that kind of thing. If you want example you might want to have a look at: http://root.cern.ch/viewvc/branches/dev/cling/lib/Interpreter/DeclExtractor.cpp?view=markup

Vassil

Hi,
   I found out that http://llvm.org/viewvc/llvm-project?view=rev&revision=124217. It adds custom attribute.
Vassil