From 27e85bd0a6308e7b7f4d8b1b9c7ba445485ec71b Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Wed, 3 Aug 2016 21:57:47 +0000 Subject: [PATCH] Clean up of libObject/Archive interfaces and change the last three uses of ErrorOr<> changing them to Expected<> to allow them to pass through llvm Errors. No functional change. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This commit by itself will break the next lld builds.  I’ll be committing the matching change for lld immediately next. llvm-svn: 277656 --- llvm/include/llvm/Object/Archive.h | 8 +-- llvm/lib/Object/Archive.cpp | 115 ++++++++++++++++++++++++------------- llvm/tools/llvm-ar/llvm-ar.cpp | 8 ++- llvm/tools/llvm-nm/llvm-nm.cpp | 6 +- 4 files changed, 87 insertions(+), 50 deletions(-) diff --git a/llvm/include/llvm/Object/Archive.h b/llvm/include/llvm/Object/Archive.h index d15b930..555900f 100644 --- a/llvm/include/llvm/Object/Archive.h +++ b/llvm/include/llvm/Object/Archive.h @@ -19,7 +19,7 @@ #include "llvm/ADT/iterator_range.h" #include "llvm/Object/Binary.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" @@ -100,7 +100,7 @@ public: Expected getNext() const; Expected getName() const; - ErrorOr getFullName() const; + Expected getFullName() const; Expected getRawName() const { return Header.getRawName(); } Expected getLastModified() const { return Header.getLastModified(); @@ -118,7 +118,7 @@ public: /// \return the size in the archive header for this member. Expected getRawSize() const; - ErrorOr getBuffer() const; + Expected getBuffer() const; uint64_t getChildOffset() const; Expected getMemoryBufferRef() const; @@ -179,7 +179,7 @@ public: , SymbolIndex(symi) , StringIndex(stri) {} StringRef getName() const; - ErrorOr getMember() const; + Expected getMember() const; Symbol getNext() const; }; diff --git a/llvm/lib/Object/Archive.cpp b/llvm/lib/Object/Archive.cpp index 827affe..f61d16b 100644 --- a/llvm/lib/Object/Archive.cpp +++ b/llvm/lib/Object/Archive.cpp @@ -389,14 +389,14 @@ Expected Archive::Child::isThinMember() const { return Parent->IsThin && Name != "/" && Name != "//"; } -ErrorOr Archive::Child::getFullName() const { +Expected Archive::Child::getFullName() const { Expected isThin = isThinMember(); if (!isThin) - return errorToErrorCode(isThin.takeError()); + return isThin.takeError(); assert(isThin.get()); Expected NameOrErr = getName(); if (!NameOrErr) - return errorToErrorCode(NameOrErr.takeError()); + return NameOrErr.takeError(); StringRef Name = *NameOrErr; if (sys::path::is_absolute(Name)) return Name; @@ -407,24 +407,24 @@ ErrorOr Archive::Child::getFullName() const { return StringRef(FullName); } -ErrorOr Archive::Child::getBuffer() const { +Expected Archive::Child::getBuffer() const { Expected isThinOrErr = isThinMember(); if (!isThinOrErr) - return errorToErrorCode(isThinOrErr.takeError()); + return isThinOrErr.takeError(); bool isThin = isThinOrErr.get(); if (!isThin) { Expected Size = getSize(); if (!Size) - return errorToErrorCode(Size.takeError()); + return Size.takeError(); return StringRef(Data.data() + StartOfFile, Size.get()); } - ErrorOr FullNameOrEr = getFullName(); - if (std::error_code EC = FullNameOrEr.getError()) - return EC; - const std::string &FullName = *FullNameOrEr; + Expected FullNameOrErr = getFullName(); + if (!FullNameOrErr) + return FullNameOrErr.takeError(); + const std::string &FullName = *FullNameOrErr; ErrorOr> Buf = MemoryBuffer::getFile(FullName); if (std::error_code EC = Buf.getError()) - return EC; + return errorCodeToError(EC); Parent->ThinBuffers.push_back(std::move(*Buf)); return Parent->ThinBuffers.back()->getBuffer(); } @@ -485,9 +485,9 @@ Expected Archive::Child::getMemoryBufferRef() const { if (!NameOrErr) return NameOrErr.takeError(); StringRef Name = NameOrErr.get(); - ErrorOr Buf = getBuffer(); - if (std::error_code EC = Buf.getError()) - return errorCodeToError(EC); + Expected Buf = getBuffer(); + if (!Buf) + return Buf.takeError(); return MemoryBufferRef(*Buf, Name); } @@ -590,9 +590,14 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) Format = K_BSD; else // Name == "__.SYMDEF_64" Format = K_DARWIN64; - // We know that the symbol table is not an external file, so we just assert - // there is no error. - SymbolTable = *C->getBuffer(); + // We know that the symbol table is not an external file, but we still must + // check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + SymbolTable = BufOrErr.get(); if (Increment()) return; setFirstRegular(*C); @@ -611,17 +616,27 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) } Name = NameOrErr.get(); if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") { - // We know that the symbol table is not an external file, so we just - // assert there is no error. - SymbolTable = *C->getBuffer(); + // We know that the symbol table is not an external file, but we still + // must check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + SymbolTable = BufOrErr.get(); if (Increment()) return; } else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") { Format = K_DARWIN64; - // We know that the symbol table is not an external file, so we just - // assert there is no error. - SymbolTable = *C->getBuffer(); + // We know that the symbol table is not an external file, but we still + // must check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + SymbolTable = BufOrErr.get(); if (Increment()) return; } @@ -636,9 +651,14 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) bool has64SymTable = false; if (Name == "/" || Name == "/SYM64/") { - // We know that the symbol table is not an external file, so we just assert - // there is no error. - SymbolTable = *C->getBuffer(); + // We know that the symbol table is not an external file, but we still + // must check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + SymbolTable = BufOrErr.get(); if (Name == "/SYM64/") has64SymTable = true; @@ -658,9 +678,14 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) if (Name == "//") { Format = has64SymTable ? K_MIPS64 : K_GNU; - // The string table is never an external member, so we just assert on the - // ErrorOr. - StringTable = *C->getBuffer(); + // The string table is never an external member, but we still + // must check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + StringTable = BufOrErr.get(); if (Increment()) return; setFirstRegular(*C); @@ -681,9 +706,14 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) } Format = K_COFF; - // We know that the symbol table is not an external file, so we just assert - // there is no error. - SymbolTable = *C->getBuffer(); + // We know that the symbol table is not an external file, but we still + // must check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + SymbolTable = BufOrErr.get(); if (Increment()) return; @@ -702,9 +732,14 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) Name = NameOrErr.get(); if (Name == "//") { - // The string table is never an external member, so we just assert on the - // ErrorOr. - StringTable = *C->getBuffer(); + // The string table is never an external member, but we still + // must check any Expected<> return value. + Expected BufOrErr = C->getBuffer(); + if (!BufOrErr) { + Err = BufOrErr.takeError(); + return; + } + StringTable = BufOrErr.get(); if (Increment()) return; } @@ -738,7 +773,7 @@ StringRef Archive::Symbol::getName() const { return Parent->getSymbolTable().begin() + StringIndex; } -ErrorOr Archive::Symbol::getMember() const { +Expected Archive::Symbol::getMember() const { const char *Buf = Parent->getSymbolTable().begin(); const char *Offsets = Buf; if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64) @@ -773,7 +808,7 @@ ErrorOr Archive::Symbol::getMember() const { uint32_t SymbolCount = read32le(Buf); if (SymbolIndex >= SymbolCount) - return object_error::parse_failed; + return errorCodeToError(object_error::parse_failed); // Skip SymbolCount to get to the indices table. const char *Indices = Buf + 4; @@ -785,7 +820,7 @@ ErrorOr Archive::Symbol::getMember() const { --OffsetIndex; if (OffsetIndex >= MemberCount) - return object_error::parse_failed; + return errorCodeToError(object_error::parse_failed); Offset = read32le(Offsets + OffsetIndex * 4); } @@ -794,7 +829,7 @@ ErrorOr Archive::Symbol::getMember() const { Error Err; Child C(Parent, Loc, &Err); if (Err) - return errorToErrorCode(std::move(Err)); + return std::move(Err); return C; } @@ -925,7 +960,7 @@ Expected> Archive::findSym(StringRef name) const { if (auto MemberOrErr = bs->getMember()) return Child(*MemberOrErr); else - return errorCodeToError(MemberOrErr.getError()); + return MemberOrErr.takeError(); } } return Optional(); diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp index cf2f98f..e54109a 100644 --- a/llvm/tools/llvm-ar/llvm-ar.cpp +++ b/llvm/tools/llvm-ar/llvm-ar.cpp @@ -318,8 +318,8 @@ static void doPrint(StringRef Name, const object::Archive::Child &C) { if (Verbose) outs() << "Printing " << Name << "\n"; - ErrorOr DataOrErr = C.getBuffer(); - failIfError(DataOrErr.getError()); + Expected DataOrErr = C.getBuffer(); + failIfError(DataOrErr.takeError()); StringRef Data = *DataOrErr; outs().write(Data.data(), Data.size()); } @@ -376,7 +376,9 @@ static void doExtract(StringRef Name, const object::Archive::Child &C) { raw_fd_ostream file(FD, false); // Get the data and its length - StringRef Data = *C.getBuffer(); + Expected BufOrErr = C.getBuffer(); + failIfError(BufOrErr.takeError()); + StringRef Data = BufOrErr.get(); // Write the data. file.write(Data.data(), Data.size()); diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index 424303b..af3d7db 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -1097,9 +1097,9 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { if (I != E) { outs() << "Archive map\n"; for (; I != E; ++I) { - ErrorOr C = I->getMember(); - if (error(C.getError())) - return; + Expected C = I->getMember(); + if (!C) + error(C.takeError(), Filename); Expected FileNameOrErr = C->getName(); if (!FileNameOrErr) { error(FileNameOrErr.takeError(), Filename); -- 2.7.4