1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

Use std::unique_ptr to make the ownership explicit.

llvm-svn: 214377
This commit is contained in:
Rafael Espindola 2014-07-31 03:12:45 +00:00
parent 0141b69686
commit 191faa331e
25 changed files with 74 additions and 60 deletions

View File

@ -126,10 +126,11 @@ public:
/// @brief Create a Binary from Source, autodetecting the file type.
///
/// @param Source The data to create the Binary from.
ErrorOr<Binary *> createBinary(std::unique_ptr<MemoryBuffer> Source,
LLVMContext *Context = nullptr);
ErrorOr<std::unique_ptr<Binary>>
createBinary(std::unique_ptr<MemoryBuffer> Source,
LLVMContext *Context = nullptr);
ErrorOr<Binary *> createBinary(StringRef Path);
ErrorOr<std::unique_ptr<Binary>> createBinary(StringRef Path);
}
}

View File

@ -27,6 +27,8 @@ namespace llvm {
namespace object {
class ObjectFile;
class COFFObjectFile;
class MachOObjectFile;
class SymbolRef;
class symbol_iterator;
@ -343,11 +345,13 @@ public:
/// @param ObjectPath The path to the object file. ObjectPath.isObject must
/// return true.
/// @brief Create ObjectFile from path.
static ErrorOr<ObjectFile *> createObjectFile(StringRef ObjectPath);
static ErrorOr<ObjectFile *>
static ErrorOr<std::unique_ptr<ObjectFile>>
createObjectFile(StringRef ObjectPath);
static ErrorOr<std::unique_ptr<ObjectFile>>
createObjectFile(std::unique_ptr<MemoryBuffer> &Object,
sys::fs::file_magic Type);
static ErrorOr<ObjectFile *>
static ErrorOr<std::unique_ptr<ObjectFile>>
createObjectFile(std::unique_ptr<MemoryBuffer> &Object) {
return createObjectFile(Object, sys::fs::file_magic::unknown);
}
@ -358,11 +362,13 @@ public:
}
public:
static ErrorOr<ObjectFile *>
static ErrorOr<std::unique_ptr<COFFObjectFile>>
createCOFFObjectFile(std::unique_ptr<MemoryBuffer> Object);
static ErrorOr<ObjectFile *>
static ErrorOr<std::unique_ptr<ObjectFile>>
createELFObjectFile(std::unique_ptr<MemoryBuffer> &Object);
static ErrorOr<ObjectFile *>
static ErrorOr<std::unique_ptr<MachOObjectFile>>
createMachOObjectFile(std::unique_ptr<MemoryBuffer> &Object);
};

View File

@ -142,15 +142,16 @@ public:
}
// construction aux.
static ErrorOr<SymbolicFile *>
static ErrorOr<std::unique_ptr<SymbolicFile>>
createSymbolicFile(std::unique_ptr<MemoryBuffer> &Object,
sys::fs::file_magic Type, LLVMContext *Context);
static ErrorOr<SymbolicFile *>
static ErrorOr<std::unique_ptr<SymbolicFile>>
createSymbolicFile(std::unique_ptr<MemoryBuffer> &Object) {
return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
}
static ErrorOr<SymbolicFile *> createSymbolicFile(StringRef ObjectPath);
static ErrorOr<std::unique_ptr<SymbolicFile>>
createSymbolicFile(StringRef ObjectPath);
static inline bool classof(const Binary *v) {
return v->isSymbolic();

View File

@ -260,12 +260,12 @@ bool DWARFUnit::parseDWO() {
sys::path::append(AbsolutePath, CompilationDir);
}
sys::path::append(AbsolutePath, DWOFileName);
ErrorOr<object::ObjectFile *> DWOFile =
ErrorOr<std::unique_ptr<object::ObjectFile>> DWOFile =
object::ObjectFile::createObjectFile(AbsolutePath);
if (!DWOFile)
return false;
// Reset DWOHolder.
DWO.reset(new DWOHolder(DWOFile.get()));
DWO.reset(new DWOHolder(DWOFile.get().get()));
DWARFUnit *DWOCU = DWO->getUnit();
// Verify that compile unit in .dwo file is valid.
if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {

View File

@ -49,7 +49,7 @@ public:
// FIXME: error checking? createObjectFile returns an ErrorOr<ObjectFile*>
// and should probably be checked for failure.
std::unique_ptr<MemoryBuffer> Buf(Buffer->getMemBuffer());
ObjFile.reset(object::ObjectFile::createObjectFile(Buf).get());
ObjFile = std::move(object::ObjectFile::createObjectFile(Buf).get());
}
ObjectImageCommon(std::unique_ptr<object::ObjectFile> Input)
: ObjectImage(nullptr), ObjFile(std::move(Input)) {}

View File

@ -38,8 +38,9 @@ StringRef Binary::getFileName() const {
return Data->getBufferIdentifier();
}
ErrorOr<Binary *> object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
LLVMContext *Context) {
ErrorOr<std::unique_ptr<Binary>>
object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
LLVMContext *Context) {
sys::fs::file_magic Type = sys::fs::identify_magic(Buffer->getBuffer());
switch (Type) {
@ -74,7 +75,7 @@ ErrorOr<Binary *> object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
llvm_unreachable("Unexpected Binary File Type");
}
ErrorOr<Binary *> object::createBinary(StringRef Path) {
ErrorOr<std::unique_ptr<Binary>> object::createBinary(StringRef Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = FileOrErr.getError())

View File

@ -1112,12 +1112,12 @@ ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
return object_error::success;
}
ErrorOr<ObjectFile *>
ErrorOr<std::unique_ptr<COFFObjectFile>>
ObjectFile::createCOFFObjectFile(std::unique_ptr<MemoryBuffer> Object) {
std::error_code EC;
std::unique_ptr<COFFObjectFile> Ret(
new COFFObjectFile(std::move(Object), EC));
if (EC)
return EC;
return Ret.release();
return std::move(Ret);
}

View File

@ -17,7 +17,7 @@
namespace llvm {
using namespace object;
ErrorOr<ObjectFile *>
ErrorOr<std::unique_ptr<ObjectFile>>
ObjectFile::createELFObjectFile(std::unique_ptr<MemoryBuffer> &Obj) {
std::pair<unsigned char, unsigned char> Ident =
getElfArchType(Obj->getBuffer());
@ -80,7 +80,7 @@ ObjectFile::createELFObjectFile(std::unique_ptr<MemoryBuffer> &Obj) {
if (EC)
return EC;
return R.release();
return std::move(R);
}
} // end namespace llvm

View File

@ -1721,7 +1721,7 @@ void MachOObjectFile::ReadULEB128s(uint64_t Index,
}
}
ErrorOr<ObjectFile *>
ErrorOr<std::unique_ptr<MachOObjectFile>>
ObjectFile::createMachOObjectFile(std::unique_ptr<MemoryBuffer> &Buffer) {
StringRef Magic = Buffer->getBuffer().slice(0, 4);
std::error_code EC;
@ -1739,6 +1739,6 @@ ObjectFile::createMachOObjectFile(std::unique_ptr<MemoryBuffer> &Buffer) {
if (EC)
return EC;
return Ret.release();
return std::move(Ret);
}

View File

@ -60,9 +60,10 @@ wrap(const relocation_iterator *SI) {
// ObjectFile creation
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
ErrorOr<ObjectFile *> ObjOrErr(ObjectFile::createObjectFile(Buf));
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
ObjectFile::createObjectFile(Buf));
Buf.release();
ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : nullptr;
ObjectFile *Obj = ObjOrErr ? ObjOrErr.get().release() : nullptr;
return wrap(Obj);
}

View File

@ -11,6 +11,8 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/COFF.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
@ -45,7 +47,7 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
return section_iterator(SectionRef(Sec, this));
}
ErrorOr<ObjectFile *>
ErrorOr<std::unique_ptr<ObjectFile>>
ObjectFile::createObjectFile(std::unique_ptr<MemoryBuffer> &Object,
sys::fs::file_magic Type) {
if (Type == sys::fs::file_magic::unknown)
@ -82,7 +84,8 @@ ObjectFile::createObjectFile(std::unique_ptr<MemoryBuffer> &Object,
llvm_unreachable("Unexpected Object File Type");
}
ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
ErrorOr<std::unique_ptr<ObjectFile>>
ObjectFile::createObjectFile(StringRef ObjectPath) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFile(ObjectPath);
if (std::error_code EC = FileOrErr.getError())

View File

@ -25,7 +25,7 @@ SymbolicFile::SymbolicFile(unsigned int Type,
SymbolicFile::~SymbolicFile() {}
ErrorOr<SymbolicFile *>
ErrorOr<std::unique_ptr<SymbolicFile>>
SymbolicFile::createSymbolicFile(std::unique_ptr<MemoryBuffer> &Object,
sys::fs::file_magic Type,
LLVMContext *Context) {

View File

@ -282,7 +282,7 @@ ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
if (!File)
error(File.getError());
else
Object.reset(File.get());
Object = std::move(File.get());
}
ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
@ -292,7 +292,7 @@ ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
if (!File)
error(File.getError());
else
Object.reset(File.get());
Object = std::move(File.get());
}
namespace {

View File

@ -528,13 +528,13 @@ int main(int argc, char **argv, char * const *envp) {
}
for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
ErrorOr<object::ObjectFile *> Obj =
ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
object::ObjectFile::createObjectFile(ExtraObjects[i]);
if (!Obj) {
Err.print(argv[0], errs());
return 1;
}
EE->addObjectFile(std::unique_ptr<object::ObjectFile>(Obj.get()));
EE->addObjectFile(std::move(Obj.get()));
}
for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {

View File

@ -698,12 +698,12 @@ writeSymbolTable(raw_fd_ostream &Out, ArrayRef<NewArchiveIterator> Members,
E = Members.end();
I != E; ++I, ++MemberNum) {
std::unique_ptr<MemoryBuffer> &MemberBuffer = Buffers[MemberNum];
ErrorOr<object::SymbolicFile *> ObjOrErr =
ErrorOr<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
object::SymbolicFile::createSymbolicFile(
MemberBuffer, sys::fs::file_magic::unknown, &Context);
if (!ObjOrErr)
continue; // FIXME: check only for "not an object file" errors.
std::unique_ptr<object::SymbolicFile> Obj(ObjOrErr.get());
std::unique_ptr<object::SymbolicFile> Obj = std::move(ObjOrErr.get());
if (!StartOffset) {
printMemberHeader(Out, "", sys::TimeValue::now(), 0, 0, 0, 0);

View File

@ -74,12 +74,13 @@ static void DumpInput(const StringRef &Filename) {
return;
}
ErrorOr<ObjectFile *> ObjOrErr(ObjectFile::createObjectFile(Buff.get()));
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
ObjectFile::createObjectFile(Buff.get());
if (std::error_code EC = ObjOrErr.getError()) {
errs() << Filename << ": " << EC.message() << '\n';
return;
}
std::unique_ptr<ObjectFile> Obj(ObjOrErr.get());
std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj.get()));

View File

@ -1009,11 +1009,11 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
return;
LLVMContext &Context = getGlobalContext();
ErrorOr<Binary *> BinaryOrErr =
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr =
createBinary(std::move(*BufferOrErr), &Context);
if (error(BinaryOrErr.getError(), Filename))
return;
std::unique_ptr<Binary> Bin(BinaryOrErr.get());
std::unique_ptr<Binary> Bin = std::move(BinaryOrErr.get());
if (Archive *A = dyn_cast<Archive>(Bin.get())) {
if (ArchiveMap) {

View File

@ -202,8 +202,8 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
return;
}
std::unique_ptr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile *>(
ObjectFile::createMachOObjectFile(Buff.get()).get()));
std::unique_ptr<MachOObjectFile> MachOOF =
std::move(ObjectFile::createMachOObjectFile(Buff.get()).get());
DisassembleInputMachO2(Filename, MachOOF.get());
}
@ -294,7 +294,7 @@ static void DisassembleInputMachO2(StringRef Filename,
errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
return;
}
DbgObj = ObjectFile::createMachOObjectFile(Buf.get()).get();
DbgObj = ObjectFile::createMachOObjectFile(Buf.get()).get().release();
}
// Setup the DIContext

View File

@ -884,12 +884,12 @@ static void DumpInput(StringRef file) {
}
// Attempt to open the binary.
ErrorOr<Binary *> BinaryOrErr = createBinary(file);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(file);
if (std::error_code EC = BinaryOrErr.getError()) {
errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
return;
}
std::unique_ptr<Binary> binary(BinaryOrErr.get());
std::unique_ptr<Binary> binary = std::move(BinaryOrErr.get());
if (Archive *a = dyn_cast<Archive>(binary.get()))
DumpArchive(a);

View File

@ -287,12 +287,12 @@ static void dumpInput(StringRef File) {
}
// Attempt to open the binary.
ErrorOr<Binary *> BinaryOrErr = createBinary(File);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(File);
if (std::error_code EC = BinaryOrErr.getError()) {
reportError(File, EC);
return;
}
std::unique_ptr<Binary> Binary(BinaryOrErr.get());
std::unique_ptr<Binary> Binary = std::move(BinaryOrErr.get());
if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
dumpArchive(Arc);

View File

@ -453,12 +453,12 @@ static void PrintFileSectionSizes(StringRef file) {
}
// Attempt to open the binary.
ErrorOr<Binary *> BinaryOrErr = createBinary(file);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(file);
if (std::error_code EC = BinaryOrErr.getError()) {
errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
return;
}
std::unique_ptr<Binary> binary(BinaryOrErr.get());
std::unique_ptr<Binary> binary = std::move(BinaryOrErr.get());
if (Archive *a = dyn_cast<Archive>(binary.get())) {
// This is an archive. Iterate over each member and display its sizes.

View File

@ -300,9 +300,9 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
return I->second;
Binary *Bin = nullptr;
Binary *DbgBin = nullptr;
ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(Path);
if (!error(BinaryOrErr.getError())) {
std::unique_ptr<Binary> ParsedBinary(BinaryOrErr.get());
std::unique_ptr<Binary> ParsedBinary = std::move(BinaryOrErr.get());
// Check if it's a universal binary.
Bin = ParsedBinary.get();
ParsedBinariesAndObjects.push_back(std::move(ParsedBinary));
@ -314,8 +314,8 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
BinaryOrErr = createBinary(ResourcePath);
std::error_code EC = BinaryOrErr.getError();
if (EC != errc::no_such_file_or_directory && !error(EC)) {
DbgBin = BinaryOrErr.get();
ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin));
DbgBin = BinaryOrErr.get().get();
ParsedBinariesAndObjects.push_back(std::move(BinaryOrErr.get()));
}
}
// Try to locate the debug binary using .gnu_debuglink section.
@ -327,8 +327,8 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
BinaryOrErr = createBinary(DebugBinaryPath);
if (!error(BinaryOrErr.getError())) {
DbgBin = BinaryOrErr.get();
ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin));
DbgBin = BinaryOrErr.get().get();
ParsedBinariesAndObjects.push_back(std::move(BinaryOrErr.get()));
}
}
}

View File

@ -164,12 +164,12 @@ static void dumpInput(StringRef File) {
}
// Attempt to open the binary.
ErrorOr<Binary *> BinaryOrErr = createBinary(File);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(File);
if (std::error_code EC = BinaryOrErr.getError()) {
reportError(File, EC);
return;
}
std::unique_ptr<Binary> Binary(BinaryOrErr.get());
std::unique_ptr<Binary> Binary = std::move(BinaryOrErr.get());
if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
dumpArchive(Arc);

View File

@ -403,10 +403,10 @@ int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(InputFile);
if (std::error_code EC = BinaryOrErr.getError())
return Error("unable to read input: '" + EC.message() + "'");
std::unique_ptr<Binary> Binary(BinaryOrErr.get());
std::unique_ptr<Binary> Binary = std::move((BinaryOrErr.get()));
const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
if (!InputObject)

View File

@ -32,11 +32,11 @@ static std::error_code dumpInput(StringRef File) {
if (File != "-" && !sys::fs::exists(File))
return obj2yaml_error::file_not_found;
ErrorOr<Binary *> BinaryOrErr = createBinary(File);
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(File);
if (std::error_code EC = BinaryOrErr.getError())
return EC;
std::unique_ptr<Binary> Binary(BinaryOrErr.get());
std::unique_ptr<Binary> Binary = std::move(BinaryOrErr.get());
// TODO: If this is an archive, then burst it and dump each entry
if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
return dumpObject(*Obj);