Replacing the main input file with a buffer

Hi all,

I’ve got an application that uses the clang frontend to generate a bit code module that’s run by LLVM’s JIT. The current way it works is to take the user-supplied source, write it to a temporary file and construct a clang invocation to compile that source file, based on the code in clang’s examples/clang-interpreter/. That works, but I want to use a memory buffer instead of the main file to avoid making all these temporary files.

What I have tried:

  • initialising the source manager with my buffer:

// the args used by the driver have “-“ as the input file

// and ‘Clang’ is a clang::CompilerInstance



if (!Clang.hasDiagnostics())


if (error)


*error = [self compilerErrorWithCode:IKBCompilerErrorCouldNotReportUnderlyingErrors compilerOutput:diagnostic_output];


return nil;




llvm::StringRef sourceString([source UTF8String]);

llvm::MemoryBuffer* mainFile = llvm::MemoryBuffer::getMemBuffer(sourceString);

FrontendInputFile inputFile(mainFile, IK_ObjC);


In each case, I try to execute an EmitLLVMOnlyAction(). This waits without completing, and I find that the application has paused in ::read(), waiting for data from stdin. Indeed if I launch my application from the terminal and press Ctrl-D when I get to that point, this happens:

Assertion failed: ((!RequiresNullTerminator || BufEnd[0] == 0) && “Buffer is not null terminated!”), function init, file /Users/leeg/Documents/OtherProjects/llvm/lib/Support/MemoryBuffer.cpp, line 57.

So it seems that none of the three things I have tried above has actually convinced the compiler instance that it should read the main file from my buffer, instead of stdin. Clearly I’ve missed some step in the problem, but can someone please tell me what that step is?

For more context, the full application is at and the (current, working) way to invoke clang is at



cling uses this approach so it can add more source lines to the input file as they come in from the user.

You may have an easier solution:

1 Use SourceManager.getFileManager().getVirtualFile() to create a virtual source file.
2 Allocate a MemoryBuffer with your sources.
3 Use overrideFileContents to replace the file contents with the MemoryBuffer.
4 Pass the virtual file file name to clang::CompilerInstance.


Thanks very much, Yaron, that does the job nicely.


Yaron Keren , 1/22/2014 7:56 PM: