[cfe-dev] How to make clang compile the data from the RewriteBuffer

Vassil Vassilev vvasilev at cern.ch
Sat Aug 9 15:22:21 PDT 2014


Hi Bogdan,
   After applying the attached patch, I can do:

void MyPlugin::Initialize(ASTContext& Context) {
// We need to reorder the consumers in the MultiplexConsumer.
MultiplexConsumer& multiplex
= static_cast<MultiplexConsumer&>(m_CI.getASTConsumer());
std::vector<ASTConsumer*>& consumers = multiplex.getConsumers();
ASTConsumer* lastConsumer = consumers.back();
consumers.pop_back();
consumers.insert(consumers.begin(), lastConsumer);
}

This allows me to hook MyPlugin before clang's codegen.

Vassil
On 09/08/14 21:24, Bogdan Tabacaru wrote:
> Hi Vassil,
>
> Is the patch available for the 3.4.2 version of LLVM or must I migrate 
> to the "current" version ?
>
> Cheers,
> Bogdan
>
>
> On Friday, August 8, 2014 6:16 PM, Vassil Vassilev <vvasilev at cern.ch> 
> wrote:
>
>
> Hi,
>   There is a pending patch on cfe-commits about this "[PATCH] 
> clang/Frontend/MultiplexConsumer.h". With it you could reorder the 
> ASTConsumers in clang and get yours before codegen.
> Vassil
> On 08/08/14 16:48, Bogdan Tabacaru wrote:
> Hi everyone,
>
> I want to create a plugin that makes some changes on the parsed AST 
> and that directly compiles the changes made on the AST. It is 
> important that I do not generate a new file with the changes on the AST.
> So, in other words, I want to change the AST in place, and then 
> compile it in the same run, using a Clang Plugin.
>
> I am using llvm 3.4.2.
>
> By following the RecursiveASTVisitor example, I have written a clang 
> plugin which modifies the parsed AST.
>
> I am using the Rewriter class to ReplaceText inside the buffer.
>
> I am also using the -add-plugin command(instead the "standard" -plugin 
> command) when calling the plugin.
>
> The rewriting part works excellently. However, when I continue the 
> compilation, clang only compiles the original file, instead of the 
> original file + changes.
> Is there a way to tell clang/llvm to compile what I have written in 
> the AST (using the Rewriter) without creating an intermediate file?
>
> Thanks a lot for your help,
> Bogdan
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu  <mailto:cfe-dev at cs.uiuc.edu>
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140810/be5c8e8b/attachment.html>
-------------- next part --------------
Index: tools/clang/include/clang/Frontend/MultiplexConsumer.h
===================================================================
--- tools/clang/include/clang/Frontend/MultiplexConsumer.h	(revision 209839)
+++ tools/clang/include/clang/Frontend/MultiplexConsumer.h	(working copy)
@@ -52,6 +52,8 @@
   void InitializeSema(Sema &S) override;
   void ForgetSema() override;
 
+  std::vector<ASTConsumer*>& getConsumers() { return Consumers; }
+
 private:
   std::vector<ASTConsumer*> Consumers;  // Owns these.
   std::unique_ptr<MultiplexASTMutationListener> MutationListener;


More information about the cfe-dev mailing list