[cfe-dev] ClangTool emit preprocessor output from modified buffers.

rphwrck via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 13 06:02:06 PDT 2019


Hello,


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");
> rewriter.getEditBuffer(sm.getMainFileID()).write(llvm::outs());
>     }


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?

Thanks,
rphwrck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190613/4b56fefc/attachment.html>


More information about the cfe-dev mailing list