Clang plugin reload AST with in memory changes

Hello

There was an email on this list about my question earlier in January:

http://lists.llvm.org/pipermail/cfe-dev/2015-January/040760.html

But it contained no specific information about how to do it, and I couldn’t figure it out based on the docs.

My problem is:

I have a source-to-source transformation tool, built using AstMatchers/Rewriter. It’s perfect when I want to compile something with gcc (I can transform the source with it, and run gcc on the modified source), and also works with clang, but I thought it could be integrated with it even better: a clang plugin, which rewrites the source, but instead of saving it to a file, clang just could reparse the modifications from the memory buffer and compile it…

So I have a PluginASTAction, and a Rewriter member, containing everything I modified.

  • I tried the solution used by tracer (link found in the same conversation), saving the modification to the DefaultOutputFile. That overwrites the original file on the disk, and doesn’t reload it - the modification will be only visible during the next execution
  • I also found ASTUnit::Reparse, which is accessible from any FrontendAction, but I only succeeded in causing segmentation faults with it - the function itself isn’t used by clang itself, and I found no plugins / projects using it with google
  • I could write a standalone program which transforms the source, saves it to a temporary file, runs clang with it, and deletes the temporary file… but I hope it can be solved in a better way.

(I read that the recommended way is just adding attributes to the AST and changing the IR, but that would require creating and maintaining two version of my tool)

My questions:

  • is this possible at all, with a plugin?
  • If it is, the solution is Reparse and I’m just doing something wrong, or something entirely different?

Hello

There was an email on this list about my question earlier in January:

http://lists.llvm.org/pipermail/cfe-dev/2015-January/040760.html

But it contained no specific information about how to do it, and I couldn’t figure it out based on the docs.

My problem is:

I have a source-to-source transformation tool, built using AstMatchers/Rewriter. It’s perfect when I want to compile something with gcc (I can transform the source with it, and run gcc on the modified source), and also works with clang, but I thought it could be integrated with it even better: a clang plugin, which rewrites the source, but instead of saving it to a file, clang just could reparse the modifications from the memory buffer and compile it…

So I have a PluginASTAction, and a Rewriter member, containing everything I modified.

  • I tried the solution used by tracer (link found in the same conversation), saving the modification to the DefaultOutputFile. That overwrites the original file on the disk, and doesn’t reload it - the modification will be only visible during the next execution
  • I also found ASTUnit::Reparse, which is accessible from any FrontendAction, but I only succeeded in causing segmentation faults with it - the function itself isn’t used by clang itself, and I found no plugins / projects using it with google
  • I could write a standalone program which transforms the source, saves it to a temporary file, runs clang with it, and deletes the temporary file… but I hope it can be solved in a better way.

You could use two ClangTools / ToolInvocations. You’ll definitely need to create a new instance, as you’ll want a new SourceManager.
Generally, this is possible, but not really a common use case; most of the time, people want to rewrite things in headers, where this approach quickly leads to ODR violations