[llvm] r264469 - [Object] Make MachOObjectFile's constructor private, provide a static create
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 25 14:59:14 PDT 2016
Author: lhames
Date: Fri Mar 25 16:59:14 2016
New Revision: 264469
URL: http://llvm.org/viewvc/llvm-project?rev=264469&view=rev
Log:
[Object] Make MachOObjectFile's constructor private, provide a static create
method instead.
This is not quite a named constructor: Construction may fail, and
MachOObjectFiles are usually passed by unique_ptr anyway, so create
returns an Expected<std::unique_ptr<MachOObjectFile>>.
Modified:
llvm/trunk/include/llvm/Object/MachO.h
llvm/trunk/lib/Object/MachOObjectFile.cpp
Modified: llvm/trunk/include/llvm/Object/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachO.h?rev=264469&r1=264468&r2=264469&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (original)
+++ llvm/trunk/include/llvm/Object/MachO.h Fri Mar 25 16:59:14 2016
@@ -193,8 +193,8 @@ public:
typedef SmallVector<LoadCommandInfo, 4> LoadCommandList;
typedef LoadCommandList::const_iterator load_command_iterator;
- MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
- Error &Err);
+ static Expected<std::unique_ptr<MachOObjectFile>>
+ create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits);
void moveSymbolNext(DataRefImpl &Symb) const override;
@@ -442,6 +442,10 @@ public:
}
private:
+
+ MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
+ Error &Err);
+
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
union {
Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=264469&r1=264468&r2=264469&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Fri Mar 25 16:59:14 2016
@@ -243,6 +243,18 @@ static Error parseSegmentLoadCommand(
return Error::success();
}
+Expected<std::unique_ptr<MachOObjectFile>>
+MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian,
+ bool Is64Bits) {
+ Error Err = Error::errorForOutParameter();
+ std::unique_ptr<MachOObjectFile> Obj(
+ new MachOObjectFile(std::move(Object), IsLittleEndian,
+ Is64Bits, Err));
+ if (Err)
+ return std::move(Err);
+ return std::move(Obj);
+}
+
MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
bool Is64bits, Error &Err)
: ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
@@ -251,10 +263,6 @@ MachOObjectFile::MachOObjectFile(MemoryB
DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr),
HasPageZeroSegment(false) {
- // We have to check Err before it's assigned to.
- if (Err)
- llvm_unreachable("Err should be in success state at entry to constructor.");
-
if (is64Bit())
parseHeader(this, Header64, Err);
else
@@ -2416,29 +2424,18 @@ bool MachOObjectFile::isRelocatableObjec
ErrorOr<std::unique_ptr<MachOObjectFile>>
ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
StringRef Magic = Buffer.getBuffer().slice(0, 4);
- std::unique_ptr<MachOObjectFile> Ret;
- if (Magic == "\xFE\xED\xFA\xCE") {
- Error Err;
- Ret.reset(new MachOObjectFile(Buffer, false, false, Err));
- if (Err)
- return errorToErrorCode(std::move(Err));
- } else if (Magic == "\xCE\xFA\xED\xFE") {
- Error Err;
- Ret.reset(new MachOObjectFile(Buffer, true, false, Err));
- if (Err)
- return errorToErrorCode(std::move(Err));
- } else if (Magic == "\xFE\xED\xFA\xCF") {
- Error Err;
- Ret.reset(new MachOObjectFile(Buffer, false, true, Err));
- if (Err)
- return errorToErrorCode(std::move(Err));
- } else if (Magic == "\xCF\xFA\xED\xFE") {
- Error Err;
- Ret.reset(new MachOObjectFile(Buffer, true, true, Err));
- if (Err)
- return errorToErrorCode(std::move(Err));
- } else
- return object_error::parse_failed;
-
- return std::move(Ret);
+ if (Magic == "\xFE\xED\xFA\xCE")
+ return expectedToErrorOr(
+ MachOObjectFile::create(Buffer, false, false));
+ else if (Magic == "\xCE\xFA\xED\xFE")
+ return expectedToErrorOr(
+ MachOObjectFile::create(Buffer, true, false));
+ else if (Magic == "\xFE\xED\xFA\xCF")
+ return expectedToErrorOr(
+ MachOObjectFile::create(Buffer, false, true));
+ else if (Magic == "\xCF\xFA\xED\xFE")
+ return expectedToErrorOr(
+ MachOObjectFile::create(Buffer, true, true));
+ //else
+ return object_error::parse_failed;
}
More information about the llvm-commits
mailing list