LLVM  4.0.0
ModuleSummaryIndexObjectFile.cpp
Go to the documentation of this file.
1 //===- ModuleSummaryIndexObjectFile.cpp - Summary index file implementation ==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the ModuleSummaryIndexObjectFile class implementation.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Object/ObjectFile.h"
22 using namespace llvm;
23 using namespace object;
24 
26  "ignore-empty-index-file", llvm::cl::ZeroOrMore,
28  "Ignore an empty index file and perform non-ThinLTO compilation"),
29  llvm::cl::init(false));
30 
32  MemoryBufferRef Object, std::unique_ptr<ModuleSummaryIndex> I)
33  : SymbolicFile(Binary::ID_ModuleSummaryIndex, Object), Index(std::move(I)) {
34 }
35 
37 
38 std::unique_ptr<ModuleSummaryIndex> ModuleSummaryIndexObjectFile::takeIndex() {
39  return std::move(Index);
40 }
41 
44  for (const SectionRef &Sec : Obj.sections()) {
45  if (Sec.isBitcode()) {
46  StringRef SecContents;
47  if (std::error_code EC = Sec.getContents(SecContents))
48  return EC;
49  return MemoryBufferRef(SecContents, Obj.getFileName());
50  }
51  }
52 
54 }
55 
59  switch (Type) {
61  return Object;
66  ObjectFile::createObjectFile(Object, Type);
67  if (!ObjFile)
68  return errorToErrorCode(ObjFile.takeError());
69  return findBitcodeInObject(*ObjFile->get());
70  }
71  default:
73  }
74 }
75 
76 // Parse module summary index in the given memory buffer.
77 // Return new ModuleSummaryIndexObjectFile instance containing parsed
78 // module summary/index.
82  if (!BCOrErr)
83  return errorCodeToError(BCOrErr.getError());
84 
86  getModuleSummaryIndex(BCOrErr.get());
87 
88  if (!IOrErr)
89  return IOrErr.takeError();
90 
91  std::unique_ptr<ModuleSummaryIndex> Index = std::move(IOrErr.get());
92  return llvm::make_unique<ModuleSummaryIndexObjectFile>(Object,
93  std::move(Index));
94 }
95 
96 // Parse the module summary index out of an IR file and return the summary
97 // index object if found, or nullptr if not.
102  std::error_code EC = FileOrErr.getError();
103  if (EC)
104  return errorCodeToError(EC);
105  MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
106  if (IgnoreEmptyThinLTOIndexFile && !BufferRef.getBufferSize())
107  return nullptr;
110  if (!ObjOrErr)
111  return ObjOrErr.takeError();
112 
113  object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
114  return Obj.takeIndex();
115 }
static ErrorOr< MemoryBufferRef > findBitcodeInMemBuffer(MemoryBufferRef Object)
Finds and returns bitcode in the given memory buffer (which may be either a bitcode file or a native ...
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:116
std::error_code getError() const
Definition: ErrorOr.h:169
Represents either an error or a value T.
Definition: ErrorOr.h:68
static Expected< std::unique_ptr< ModuleSummaryIndexObjectFile > > create(MemoryBufferRef Object)
Parse module summary index in the given memory buffer.
This class is the base class for all object file types.
Definition: ObjectFile.h:178
ELF Relocatable object file.
Definition: FileSystem.h:246
Error takeError()
Take ownership of the stored error.
static ErrorOr< MemoryBufferRef > findBitcodeInObject(const ObjectFile &Obj)
Finds and returns bitcode embedded in the given object file, or an error code if not found...
Tagged union holding either a T or a Error.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:395
ModuleSummaryIndexObjectFile(MemoryBufferRef Object, std::unique_ptr< ModuleSummaryIndex > I)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
static llvm::cl::opt< bool > IgnoreEmptyThinLTOIndexFile("ignore-empty-index-file", llvm::cl::ZeroOrMore, llvm::cl::desc("Ignore an empty index file and perform non-ThinLTO compilation"), llvm::cl::init(false))
section_iterator_range sections() const
Definition: ObjectFile.h:257
StringRef getFileName() const
Definition: Binary.cpp:35
size_t getBufferSize() const
Definition: MemoryBuffer.h:175
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndexForFile(StringRef Path)
Parse the module summary index out of an IR file and return the module summary index object if found...
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Path.cpp:999
std::unique_ptr< ModuleSummaryIndex > takeIndex()
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
reference get()
Returns a reference to the stored T value.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:240
#define I(x, y, z)
Definition: MD5.cpp:54
This class is used to read just the module summary index related sections out of the given object (wh...
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndex(MemoryBufferRef Buffer)
Parse the specified bitcode buffer, returning the module summary index.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
reference get()
Definition: ErrorOr.h:166
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:70