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

Benjamin Orgogozo benjamin.orgogozo at normalesup.org
Thu Jul 21 08:50:36 PDT 2011


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 




More information about the cfe-dev mailing list