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

Vassil Vassilev vasil.georgiev.vasilev at cern.ch
Sat Oct 15 05:32:20 PDT 2011


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
On 07/21/2011 05:50 PM, Benjamin Orgogozo 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,
>




More information about the cfe-dev mailing list