Fix a pair of use after free. Should bring the bots back.
[oota-llvm.git] / lib / AsmParser / Parser.cpp
index a613a8346abfe72b894f7c27c6ad801704935b39..7c6598106ece07fdb9ebfc12b6e0d4b00676a379 100644 (file)
@@ -7,57 +7,53 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This library implements the functionality defined in llvm/Assembly/Parser.h
+// This library implements the functionality defined in llvm/AsmParser/Parser.h
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Assembly/Parser.h"
+#include "llvm/AsmParser/Parser.h"
 #include "LLParser.h"
-#include "llvm/Module.h"
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/Support/SourceMgr.h"
+#include "llvm/IR/Module.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Support/system_error.h"
 #include <cstring>
+#include <system_error>
 using namespace llvm;
 
-Module *llvm::ParseAssembly(MemoryBuffer *F,
-                            Module *M,
-                            SMDiagnostic &Err,
-                            LLVMContext &Context) {
+std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F,
+                                            SMDiagnostic &Err,
+                                            LLVMContext &Context) {
   SourceMgr SM;
-  SM.AddNewSourceBuffer(F, SMLoc());
-
-  // If we are parsing into an existing module, do it.
-  if (M)
-    return LLParser(F, SM, Err, M).Run() ? 0 : M;
-
-  // Otherwise create a new module.
-  OwningPtr<Module> M2(new Module(F->getBufferIdentifier(), Context));
-  if (LLParser(F, SM, Err, M2.get()).Run())
-    return 0;
-  return M2.take();
+  MemoryBuffer *Buf = F.get();
+  SM.AddNewSourceBuffer(F.release(), SMLoc());
+
+  std::unique_ptr<Module> M =
+      make_unique<Module>(Buf->getBufferIdentifier(), Context);
+  if (LLParser(Buf->getBuffer(), SM, Err, M.get()).Run())
+    return nullptr;
+  return std::move(M);
 }
 
-Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
-                                LLVMContext &Context) {
-  error_code ec;
-  MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
-  if (F == 0) {
-    Err = SMDiagnostic(Filename,
-                       "Could not open input file: " + ec.message());
-    return 0;
+std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
+                                                SMDiagnostic &Err,
+                                                LLVMContext &Context) {
+  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
+      MemoryBuffer::getFileOrSTDIN(Filename);
+  if (std::error_code EC = FileOrErr.getError()) {
+    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
+                       "Could not open input file: " + EC.message());
+    return nullptr;
   }
 
-  return ParseAssembly(F, 0, Err, Context);
+  return parseAssembly(std::move(FileOrErr.get()), Err, Context);
 }
 
-Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
-                                  SMDiagnostic &Err, LLVMContext &Context) {
-  MemoryBuffer *F =
-    MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)),
-                               "<string>");
+std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
+                                                  SMDiagnostic &Err,
+                                                  LLVMContext &Context) {
+  std::unique_ptr<MemoryBuffer> F(
+      MemoryBuffer::getMemBuffer(AsmString, "<string>"));
 
-  return ParseAssembly(F, M, Err, Context);
+  return parseAssembly(std::move(F), Err, Context);
 }