[cfe-dev] RecursiveAstVisitor&Rewriter: How to add an include file?

Csaba Raduly via cfe-dev cfe-dev at lists.llvm.org
Sun Sep 10 10:01:12 PDT 2017


On Tue, Sep 5, 2017 at 5:35 PM, Marcel Schaible wrote:
> After digging around I came to the following solution:
>
> class MyFrontendAction : public ASTFrontendAction {
> public:
>     MyFrontendAction() {}
>     void EndSourceFileAction() override {
>         SourceManager &SM = TheRewriter.getSourceMgr();
>         TheRewriter.getEditBuffer(SM.getMainFileID()).write(llvm::errs());
>
>         clang::SourceLocation loc =
> SM.getLocForStartOfFile(SM.getMainFileID());
>         std::stringstream SSBefore;
>         SSBefore << "#include \"myinclude.h\"\n";

Why do you use a stream? InsertTextBefore takes a StringRef.

const char * before = R"(#include "myinclude.h")"   "\n" ;
TheRewriter.InsertTextBefore(loc, before); // StringRef temporary constructed

>         TheRewriter.InsertTextBefore(loc, SSBefore.str());
>         ...
>     }
>


Csaba
-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds



More information about the cfe-dev mailing list