Do not require a Context to extract the FunctionIndex from Bitcode (NFC)
[oota-llvm.git] / lib / Object / FunctionIndexObjectFile.cpp
index c5f88fc2a2bd9f13987e12fd7c29c6dc6107c6cf..f4b2b5562a39397aa487bb47120fd9ac821490b1 100644 (file)
@@ -1,4 +1,4 @@
-//===- FunctionIndexObjectFile.cpp - Function index file implementation ----===//
+//===- FunctionIndexObjectFile.cpp - Function index file implementation ---===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -32,14 +32,16 @@ std::unique_ptr<FunctionInfoIndex> FunctionIndexObjectFile::takeIndex() {
   return std::move(Index);
 }
 
-ErrorOr<MemoryBufferRef> FunctionIndexObjectFile::findBitcodeInObject(
-    const ObjectFile &Obj) {
+ErrorOr<MemoryBufferRef>
+FunctionIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
   for (const SectionRef &Sec : Obj.sections()) {
     StringRef SecName;
-    if (std::error_code EC = Sec.getName(SecName)) return EC;
+    if (std::error_code EC = Sec.getName(SecName))
+      return EC;
     if (SecName == ".llvmbc") {
       StringRef SecContents;
-      if (std::error_code EC = Sec.getContents(SecContents)) return EC;
+      if (std::error_code EC = Sec.getContents(SecContents))
+        return EC;
       return MemoryBufferRef(SecContents, Obj.getFileName());
     }
   }
@@ -47,50 +49,55 @@ ErrorOr<MemoryBufferRef> FunctionIndexObjectFile::findBitcodeInObject(
   return object_error::bitcode_section_not_found;
 }
 
-ErrorOr<MemoryBufferRef> FunctionIndexObjectFile::findBitcodeInMemBuffer(
-    MemoryBufferRef Object) {
+ErrorOr<MemoryBufferRef>
+FunctionIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
   sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
   switch (Type) {
-    case sys::fs::file_magic::bitcode:
-      return Object;
-    case sys::fs::file_magic::elf_relocatable:
-    case sys::fs::file_magic::macho_object:
-    case sys::fs::file_magic::coff_object: {
-      ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
-          ObjectFile::createObjectFile(Object, Type);
-      if (!ObjFile) return ObjFile.getError();
-      return findBitcodeInObject(*ObjFile->get());
-    }
-    default:
-      return object_error::invalid_file_type;
+  case sys::fs::file_magic::bitcode:
+    return Object;
+  case sys::fs::file_magic::elf_relocatable:
+  case sys::fs::file_magic::macho_object:
+  case sys::fs::file_magic::coff_object: {
+    ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
+        ObjectFile::createObjectFile(Object, Type);
+    if (!ObjFile)
+      return ObjFile.getError();
+    return findBitcodeInObject(*ObjFile->get());
+  }
+  default:
+    return object_error::invalid_file_type;
   }
 }
 
 // Looks for function index in the given memory buffer.
 // returns true if found, else false.
 bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
-    MemoryBufferRef Object, LLVMContext &Context) {
+    MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) {
   ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
-  if (!BCOrErr) return false;
+  if (!BCOrErr)
+    return false;
 
-  return hasFunctionSummary(BCOrErr.get(), Context, nullptr);
+  return hasFunctionSummary(BCOrErr.get(), DiagnosticHandler);
 }
 
 // Parse function index in the given memory buffer.
 // Return new FunctionIndexObjectFile instance containing parsed
 // function summary/index.
 ErrorOr<std::unique_ptr<FunctionIndexObjectFile>>
-FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
-                                bool IsLazy) {
+FunctionIndexObjectFile::create(MemoryBufferRef Object,
+                                DiagnosticHandlerFunction DiagnosticHandler,
+                                const Module *ExportingModule, bool IsLazy) {
   std::unique_ptr<FunctionInfoIndex> Index;
 
   ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
-  if (!BCOrErr) return BCOrErr.getError();
+  if (!BCOrErr)
+    return BCOrErr.getError();
 
-  ErrorOr<std::unique_ptr<FunctionInfoIndex>> IOrErr =
-      getFunctionInfoIndex(BCOrErr.get(), Context, nullptr, IsLazy);
+  ErrorOr<std::unique_ptr<FunctionInfoIndex>> IOrErr = getFunctionInfoIndex(
+      BCOrErr.get(), DiagnosticHandler, ExportingModule, IsLazy);
 
-  if (std::error_code EC = IOrErr.getError()) return EC;
+  if (std::error_code EC = IOrErr.getError())
+    return EC;
 
   Index = std::move(IOrErr.get());
 
@@ -101,14 +108,15 @@ FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
 // given name out of the given buffer. Parsed information is
 // stored on the index object saved in this object.
 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
-    MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) {
+    MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
+    StringRef FunctionName) {
   sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
   switch (Type) {
-    case sys::fs::file_magic::bitcode: {
-      return readFunctionSummary(Object, Context, nullptr, FunctionName,
-                                 std::move(Index));
-    }
-    default:
-      return object_error::invalid_file_type;
+  case sys::fs::file_magic::bitcode: {
+    return readFunctionSummary(Object, DiagnosticHandler, FunctionName,
+                               std::move(Index));
+  }
+  default:
+    return object_error::invalid_file_type;
   }
 }