-Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException)
- FILE *F = stdin;
-
- if (Filename != "-") {
- F = fopen(Filename.c_str(), "r");
-
- if (F == 0)
- throw ParseException(Filename, "Could not open file '" + Filename + "'");
- }
-
- Module *Result;
- try {
- Result = RunVMAsmParser(Filename, F);
- } catch (...) {
- if (F != stdin) fclose(F); // Make sure to close file descriptor if an
- throw; // exception is thrown
- }
-
- if (F != stdin)
- fclose(F);
-
- if (Result) { // Check to see that it is valid...
- if (verifyModule(*Result)) {
- delete Result;
- throw ParseException(Filename, "Source file is not well formed LLVM!");
- }
- }
- return Result;
+//===----------------------------------------------------------------------===//
+//
+// This library implements the functionality defined in llvm/AsmParser/Parser.h
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/AsmParser/Parser.h"
+#include "LLParser.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
+#include <cstring>
+#include <system_error>
+using namespace llvm;
+
+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->getBuffer(), SM, Err, M).Run() ? nullptr : M;
+
+ // Otherwise create a new module.
+ std::unique_ptr<Module> M2(new Module(Buf->getBufferIdentifier(), Context));
+ if (LLParser(Buf->getBuffer(), SM, Err, M2.get()).Run())
+ return nullptr;
+ return M2.release();