Skip to content

Commit

Permalink
Use std::unique_ptr to make the ownership explicit.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214377 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Jul 31, 2014
1 parent 0a8cc45 commit 79002da
Show file tree
Hide file tree
Showing 25 changed files with 74 additions and 60 deletions.
7 changes: 4 additions & 3 deletions include/llvm/Object/Binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ class Binary {
/// @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);
}
}

Expand Down
18 changes: 12 additions & 6 deletions include/llvm/Object/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace llvm {
namespace object {

class ObjectFile;
class COFFObjectFile;
class MachOObjectFile;

class SymbolRef;
class symbol_iterator;
Expand Down Expand Up @@ -343,11 +345,13 @@ class ObjectFile : public SymbolicFile {
/// @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);
}
Expand All @@ -358,11 +362,13 @@ class ObjectFile : public SymbolicFile {
}

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);
};

Expand Down
7 changes: 4 additions & 3 deletions include/llvm/Object/SymbolicFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,16 @@ class SymbolicFile : public Binary {
}

// 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();
Expand Down
4 changes: 2 additions & 2 deletions lib/DebugInfo/DWARFUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ObjectImageCommon : public ObjectImage {
// 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)) {}
Expand Down
7 changes: 4 additions & 3 deletions lib/Object/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions lib/Object/COFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions lib/Object/ELFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1739,6 +1739,6 @@ ObjectFile::createMachOObjectFile(std::unique_ptr<MemoryBuffer> &Buffer) {

if (EC)
return EC;
return Ret.release();
return std::move(Ret);
}

5 changes: 3 additions & 2 deletions lib/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 5 additions & 2 deletions lib/Object/ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion lib/Object/SymbolicFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ProfileData/CoverageMappingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
if (!File)
error(File.getError());
else
Object.reset(File.get());
Object = std::move(File.get());
}

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

namespace {
Expand Down
4 changes: 2 additions & 2 deletions tools/lli/lli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-ar/llvm-ar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions tools/llvm-dwarfdump/llvm-dwarfdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions tools/llvm-objdump/MachODump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-readobj/llvm-readobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-size/llvm-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 79002da

Please sign in to comment.