[cfe-dev] Circumventing ExecuteAction's initialization of SourceManager instance

Garrison Venn gvenn.cfe.dev at gmail.com
Tue Nov 9 17:58:48 PST 2010


Is there anyway around FrontendAction::Execute(...)'s use of CompilerInstance::
InitializeSourceManager(...)?

I'm trying to switch from using ParseAST to CompilerInstance::ExecuteAction when
operating on memory buffers initialized with a code string. The eventually called
FrontendAction::Execute implementation hangs on a lower MemoryBuffer::getSTDIN(...)
read, but would subsequently fail anyway when trying to reinitialized the
the SourceManager instance. I know the doc for CompilerInstance::ExecuteAction
specifically states that the supplied SourceManager should not be pre-initialized, but
I know of no other way to use memory buffers directly.

In detail:

I setup my source manager via:

    compInst.createFileManager();
    compInst.createSourceManager(compInst.getFileManager(),
                                 compInst.getFileSystemOpts());
    compInst.getSourceManager().createMainFileIDForMemBuffer(buffer);

compInst is a CompilerInstance, and buffer is previously setup with:

    const char * src = codeString.data();
    llvm::StringRef input_data(src);
    llvm::StringRef buffer_name("src");
    llvm::MemoryBuffer *buffer =
        llvm::MemoryBuffer::getMemBufferCopy(input_data, buffer_name);

I then invoke the action code path with:

    llvm::OwningPtr<clang::SyntaxOnlyAction> act(new MyASTAction);

    if (compInst.ExecuteAction(*act))
    {
    ....

I looked for overwriting opportunities, but CompilerInstance::InitializeSourceManager
is static, and FrontendAction::Execute is non-virtual. If there is not another way to use
the CompilerInstance::ExecuteAction code path with memory buffers, would it be ok
to patch CompilerInstance::InitializeSourceManager to only do its SourceManager
initializations if its MainFileID is invalid? Beyond violating the contract as indicated 
by the CompilerInstance::ExecuteAction doc, I don't know what other effects this
would have. 

... and there always is "I don't understand the code correctly" so feel free to jump in. :-)

Thanks in advance

Garrison



More information about the cfe-dev mailing list