Fix use-after-free error.
[oota-llvm.git] / include / llvm / Support / IRReader.h
index 56700117d2201f163011234bb12c125123d735c7..3da6e85e25c3f46f612e94bdf360e2bc32225c34 100644 (file)
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
-#include "llvm/ModuleProvider.h"
 
 namespace llvm {
 
-  /// If the given MemoryBuffer holds a bitcode image, return a ModuleProvider
-  /// for it which does lazy deserialization of function bodies.  Otherwise,
-  /// attempt to parse it as LLVM Assembly and return a fully populated
-  /// ModuleProvider. This function *always* takes ownership of the given
-  /// MemoryBuffer.
-  inline ModuleProvider *getIRModuleProvider(MemoryBuffer *Buffer,
-                                             const std::string &Filename,
-                                             SMDiagnostic &Err,
-                                             LLVMContext &Context) {
+  /// If the given MemoryBuffer holds a bitcode image, return a Module for it
+  /// which does lazy deserialization of function bodies.  Otherwise, attempt to
+  /// parse it as LLVM Assembly and return a fully populated Module. This
+  /// function *always* takes ownership of the given MemoryBuffer.
+  inline Module *getLazyIRModule(MemoryBuffer *Buffer,
+                                 SMDiagnostic &Err,
+                                 LLVMContext &Context) {
     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
                   (const unsigned char *)Buffer->getBufferEnd())) {
       std::string ErrMsg;
-      ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, &ErrMsg);
-      if (MP == 0) {
-        Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
+      Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
+      if (M == 0) {
+        Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
         // ParseBitcodeFile does not take ownership of the Buffer in the
         // case of an error.
         delete Buffer;
       }
-      return MP;
+      return M;
     }
 
-    Module *M = ParseAssembly(Buffer, Filename, 0, Err, Context);
-    if (M == 0)
-      return 0;
-    return new ExistingModuleProvider(M);
+    return ParseAssembly(Buffer, 0, Err, Context);
   }
 
-  /// If the given file holds a bitcode image, return a ModuleProvider
+  /// If the given file holds a bitcode image, return a Module
   /// for it which does lazy deserialization of function bodies.  Otherwise,
   /// attempt to parse it as LLVM Assembly and return a fully populated
-  /// ModuleProvider.
-  inline ModuleProvider *getIRFileModuleProvider(const std::string &Filename,
-                                                 SMDiagnostic &Err,
-                                                 LLVMContext &Context) {
+  /// Module.
+  inline Module *getLazyIRFileModule(const std::string &Filename,
+                                     SMDiagnostic &Err,
+                                     LLVMContext &Context) {
     std::string ErrMsg;
     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
     if (F == 0) {
-      Err = SMDiagnostic(Filename, -1, -1,
-                         "Could not open input file '" + Filename + "'", "");
+      Err = SMDiagnostic(Filename, 
+                         "Could not open input file "
+                         "'" + Filename + "': " + ErrMsg);
       return 0;
     }
 
-    return getIRModuleProvider(F, Filename, Err, Context);
+    return getLazyIRModule(F, Err, Context);
   }
 
   /// If the given MemoryBuffer holds a bitcode image, return a Module
@@ -78,21 +73,20 @@ namespace llvm {
   /// a Module for it. This function *always* takes ownership of the given
   /// MemoryBuffer.
   inline Module *ParseIR(MemoryBuffer *Buffer,
-                         const std::string &Filename,
                          SMDiagnostic &Err,
                          LLVMContext &Context) {
     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
                   (const unsigned char *)Buffer->getBufferEnd())) {
       std::string ErrMsg;
       Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
+      if (M == 0)
+        Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
       // ParseBitcodeFile does not take ownership of the Buffer.
       delete Buffer;
-      if (M == 0)
-        Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
       return M;
     }
 
-    return ParseAssembly(Buffer, Filename, 0, Err, Context);
+    return ParseAssembly(Buffer, 0, Err, Context);
   }
 
   /// If the given file holds a bitcode image, return a Module for it.
@@ -104,12 +98,13 @@ namespace llvm {
     std::string ErrMsg;
     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
     if (F == 0) {
-      Err = SMDiagnostic(Filename, -1, -1,
-                         "Could not open input file '" + Filename + "'", "");
+      Err = SMDiagnostic(Filename, 
+                         "Could not open input file "
+                         "'" + Filename + "': " + ErrMsg);
       return 0;
     }
 
-    return ParseIR(F, Filename, Err, Context);
+    return ParseIR(F, Err, Context);
   }
 
 }