<html><head></head><body>







<p class="p1">Hi all,</p>
<p class="p2"><br></p>
<p class="p1">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.</p>
<p class="p2"><br></p>
<p class="p1">What I have tried:</p>
<p class="p1">- initialising the source manager with my buffer:</p>
<p class="p1">  // the args used by the driver have “-“ as the input file</p>
<p class="p1">  // and ‘Clang’ is a clang::CompilerInstance</p>
<p class="p1">  Clang.setInvocation(CI.take());</p>
<p class="p1">  Clang.createDiagnostics();</p>
<p class="p1">  if (!Clang.hasDiagnostics())</p>
<p class="p1">  {</p>
<p class="p1">      if (error)</p>
<p class="p1">      {</p>
<p class="p1">          *error = [self compilerErrorWithCode:IKBCompilerErrorCouldNotReportUnderlyingErrors compilerOutput:diagnostic_output];</p>
<p class="p1">      }</p>
<p class="p1">      return nil;</p>
<p class="p1">  }</p>
<p class="p2"><br></p>
<p class="p1">  Clang.createFileManager();</p>
<p class="p1">  Clang.createSourceManager(Clang.getFileManager());</p>
<p class="p1">  llvm::StringRef sourceString([source UTF8String]);</p>
<p class="p1">  llvm::MemoryBuffer* mainFile = llvm::MemoryBuffer::getMemBuffer(sourceString);</p>
<p class="p1">  FrontendInputFile inputFile(mainFile, IK_ObjC);</p>
<p class="p1">  Clang.InitializeSourceManager(inputFile);</p>
<p class="p2"><br></p>
<p class="p3"><span class="s1">- creating a main file ID with the buffer. I used the approach taken by cling here: <a href="https://github.com/cxx-hep/root-cern/blob/be3c582075364538f14b65ba5f528f730ef8f2ba/interpreter/cling/lib/Interpreter/CIFactory.cpp#L239-L263"><span class="s2">https://github.com/cxx-hep/root-cern/blob/be3c582075364538f14b65ba5f528f730ef8f2ba/interpreter/cling/lib/Interpreter/CIFactory.cpp#L239-L263</span></a></span></p>
<p class="p1">- following the other branch in cling’s example above, I tried setting the main file’s content buffer to be my buffer.</p>
<p class="p2"><br></p>
<p class="p1">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:</p>
<p class="p2"><br></p>
<p class="p4">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.</p>
<p class="p2"><br></p>
<p class="p1">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?</p>
<p class="p2"><br></p>
<p class="p3"><span class="s1">For more context, the full application is at <a href="https://bitbucket.org/iamleeg/ikbclassbrowser"><span class="s2">https://bitbucket.org/iamleeg/ikbclassbrowser</span></a> and the (current, working) way to invoke clang is at <a href="https://bitbucket.org/iamleeg/ikbclassbrowser/src/2ea5643705fdeccd3c2bea08c516e3289f9a9fd8/ClassBrowser/Code%20Execution/IKBClangCompiler.mm?at=master#cl-80"><span class="s2">https://bitbucket.org/iamleeg/ikbclassbrowser/src/2ea5643705fdeccd3c2bea08c516e3289f9a9fd8/ClassBrowser/Code%20Execution/IKBClangCompiler.mm?at=master#cl-80</span></a>.</span></p>
<p class="p2"><br></p>
<p class="p1">Thanks,</p>
<p class="p1">Graham.</p></body></html>