Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add memory file to clang CompilerInstance

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.

like image 404
mkaes Avatar asked Sep 06 '25 17:09

mkaes


1 Answers

I will answer my own question maybe it will be helpful. While the PPCallbacks are called whenever the preprocessor touches a file I found no way to change the content. So my solution is to provide my own FileSystem

class MyFs : public FileSystem{
    ... openFileForRead(const Twine& Path) overwrite
    {
    // in here I create my own File that will contain the data
    // all other functions and when the path is not what I expect I forward it to a FileSystrem
    }
};
class MyFrontendAction : public ASTFrontendAction
{
    virtual bool BeginInvocation(CompilerInstance &ci) override{
        ci.createFileManager(); // now I have a default FS
        llvm::IntrusiveRefCntPtr<vfs::FileSystem> fs(new MyFs(ci.getFileManager().getVirtualFileSystem()));
        ci.setVirtualFileSystem(fs);
        ci.setFileManager(new clang::FileManager(clang::FileSystemOptions{}, fs));
        auto& fm = ci.getFileManager();
        ci.createSourceManager(fm);
        return true;
    }
    std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& ci, StringRef file) override{/* do my parsing */}

};
like image 187
mkaes Avatar answered Sep 09 '25 03:09

mkaes