Add memory file to clang CompilerInstance

Viewed 542

I am trying to create a tool with clang and was wondering if it is possible to inject a include file from memory to the CompilerInstance preprocessor.
My goal is to add a #include <my_globals.hpp> to my files and dynamically include this file with the appropriate content. So I have a ASTFrontendAction like this:

class MyFrontendAction : public ASTFrontendAction
{
    virtual bool BeginInvocation(CompilerInstance &ci) override{
        auto buffer = llvm::MemoryBuffer::getMemBufferCopy(...);
        ci.createFileManager();
        ci.createSourceManager(ci.getFileManager());
        ci.createPreprocessor(clang::TU_Complete);
        auto& pp = ci.getPreprocessor();
        auto& preprocessorOpts = pp.getPreprocessorOpts();
        preprocessorOpts.clearRemappedFiles();
        preprocessorOpts.addRemappedFile("my_globals.hpp", buffer.release());
        // this seams not to work
        auto& hsi = pp.getHeaderSearchInfo();
        auto& headerSearchOptions = hsi.getHeaderSearchOpts();
        headerSearchOptions.Verbose = true; // this option is used during job

    }
    std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& ci, StringRef file) override{/* do my parsing */}

};

The parsing works as long as I do not include my header file. If i do I will get a my_globals.hpp file not found.
So the addRemappedFile does not make this file visible to the preprocessor. I could add some search path but how can I indicate that this file has no path?
Can anyone give me hint how I can solve this.

1 Answers
Related