ClangTool emit preprocessor output from modified buffers.


I am doing source-to-source transformation using Clang tools. I use a custom ASTFrontendAction to refactor a few things such as the variable name by implementing visitors like VisitVarDecl. It is fairly simple to save the modified buffers as new files. I can do this by calling the proper rewriter functions at the end of the ASTFrontendAction::EndSourceFileAction. More or less like this (this one prints to the standard output):

void EndSourceFileAction() override {
SourceManager &sm = rewriter.getSourceMgr();
rewriter.InsertTextBefore(sm.getLocForStartOfFile(sm.getMainFileID()), “// This file has been modified”);

My question is, how can I emit the preprocessor output (similar behavior as clang++ -E main.cpp) at the end of my refactoring with the modified files? Basically, I am trying to find a code that does roughly the following:

// For every modified file add file to CI.getPreprocessorOpts().addRemappedFile()
// Call CI.getPreprocessor().emitPreprocessorOutput();

I am struggling to implement this. Does anybody know the right approach to re-invoke the preprocessor to emit the preprocessor output at the end of my FrontEndActions? Any suggestions of tutorials?