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

Marcel Schaible via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 5 08:35:59 PDT 2017


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";
        TheRewriter.InsertTextBefore(loc, SSBefore.str());
        ...
    }

Thanks
Marcel

Am 05.09.2017 um 13:15 schrieb Marcel Schaible via cfe-dev:
> Hi everyone,
>
> I am trying to add an user defined include file at the top level of q
> compilation unit with a RecursiveAstVisitor and a Rewriter. Is there
> something like VisitModule?
>
> Any hints are welcome!
>
>
> Thanks
>
> Marcel
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170905/4c9d0136/attachment.html>


More information about the cfe-dev mailing list