-
-ParseError* TheParseError = 0; /// FIXME: Not threading friendly
-
-Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) {
- std::string ErrorStr;
- MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
- &ErrorStr);
- if (F == 0) {
- if (Err)
- Err->setError(Filename, "Could not open input file '" + Filename + "'");
- return 0;
- }
-
- TheParseError = Err;
- Module *Result = RunVMAsmParser(F);
- delete F;
- return Result;
+Module *llvm::ParseAssembly(std::unique_ptr<MemoryBuffer> F, Module *M,
+ SMDiagnostic &Err, LLVMContext &Context) {
+ SourceMgr SM;
+ MemoryBuffer *Buf = F.get();
+ SM.AddNewSourceBuffer(F.release(), SMLoc());
+
+ // If we are parsing into an existing module, do it.
+ if (M)
+ return LLParser(Buf, SM, Err, M).Run() ? nullptr : M;
+
+ // Otherwise create a new module.
+ std::unique_ptr<Module> M2(new Module(Buf->getBufferIdentifier(), Context));
+ if (LLParser(Buf, SM, Err, M2.get()).Run())
+ return nullptr;
+ return M2.release();