diff options
author | Lang Hames <lhames@gmail.com> | 2016-07-13 21:13:05 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2016-07-13 21:13:05 +0000 |
commit | c2773e97d248c396636464de9726c04efe47bf03 (patch) | |
tree | d06cf695118c691289eabb963bdd2613836388c2 /llvm/lib/Object/Archive.cpp | |
parent | 02c15da062621032fbb9d72b73ee1996be9205ec (diff) | |
download | llvm-c2773e97d248c396636464de9726c04efe47bf03.zip llvm-c2773e97d248c396636464de9726c04efe47bf03.tar.gz llvm-c2773e97d248c396636464de9726c04efe47bf03.tar.bz2 |
[Object] Change Archive::child_iterator for better interop with Error/Expected.
See http://reviews.llvm.org/D22079
Changes the Archive::child_begin and Archive::children to require a reference
to an Error. If iterator increment fails (because the archive header is
damaged) the iterator will be set to 'end()', and the error stored in the
given Error&. The Error value should be checked by the user immediately after
the loop. E.g.:
Error Err;
for (auto &C : A->children(Err)) {
// Do something with archive child C.
}
// Check the error immediately after the loop.
if (Err)
return Err;
Failure to check the Error will result in an abort() when the Error goes out of
scope (as guaranteed by the Error class).
llvm-svn: 275316
Diffstat (limited to 'llvm/lib/Object/Archive.cpp')
-rw-r--r-- | llvm/lib/Object/Archive.cpp | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/llvm/lib/Object/Archive.cpp b/llvm/lib/Object/Archive.cpp index 4720bf8..4827e58 100644 --- a/llvm/lib/Object/Archive.cpp +++ b/llvm/lib/Object/Archive.cpp @@ -298,12 +298,9 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) } // Get the special members. - child_iterator I = child_begin(false); - std::error_code ec; - if ((ec = I->getError())) { - Err = errorCodeToError(ec); + child_iterator I = child_begin(Err, false); + if (Err) return; - } child_iterator E = child_end(); // This is at least a valid empty archive. Since an empty archive is the @@ -315,13 +312,13 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) Err = Error::success(); return; } - const Child *C = &**I; + const Child *C = &*I; auto Increment = [&]() { ++I; - if ((Err = errorCodeToError(I->getError()))) + if (Err) return true; - C = &**I; + C = &*I; return false; }; @@ -366,8 +363,7 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) Format = K_BSD; // We know this is BSD, so getName will work since there is no string table. ErrorOr<StringRef> NameOrErr = C->getName(); - ec = NameOrErr.getError(); - if (ec) { + if (auto ec = NameOrErr.getError()) { Err = errorCodeToError(ec); return; } @@ -465,23 +461,29 @@ Archive::Archive(MemoryBufferRef Source, Error &Err) Err = Error::success(); } -Archive::child_iterator Archive::child_begin(bool SkipInternal) const { +Archive::child_iterator Archive::child_begin(Error &Err, + bool SkipInternal) const { if (Data.getBufferSize() == 8) // empty archive. return child_end(); if (SkipInternal) - return Child(this, FirstRegularData, FirstRegularStartOfFile); + return child_iterator(Child(this, FirstRegularData, + FirstRegularStartOfFile), + &Err); const char *Loc = Data.getBufferStart() + strlen(Magic); std::error_code EC; - Child c(this, Loc, &EC); - if (EC) - return child_iterator(EC); - return child_iterator(c); + Child C(this, Loc, &EC); + if (EC) { + ErrorAsOutParameter ErrAsOutParam(Err); + Err = errorCodeToError(EC); + return child_end(); + } + return child_iterator(C, &Err); } Archive::child_iterator Archive::child_end() const { - return Child(this, nullptr, nullptr); + return child_iterator(Child(this, nullptr, nullptr), nullptr); } StringRef Archive::Symbol::getName() const { @@ -665,18 +667,20 @@ uint32_t Archive::getNumberOfSymbols() const { return read32le(buf); } -Archive::child_iterator Archive::findSym(StringRef name) const { +Archive::child_iterator Archive::findSym(Error &Err, StringRef name) const { Archive::symbol_iterator bs = symbol_begin(); Archive::symbol_iterator es = symbol_end(); for (; bs != es; ++bs) { StringRef SymName = bs->getName(); if (SymName == name) { - ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember(); - // FIXME: Should we really eat the error? - if (ResultOrErr.getError()) + if (auto MemberOrErr = bs->getMember()) { + return child_iterator(*MemberOrErr, &Err); + } else { + ErrorAsOutParameter ErrAsOutParam(Err); + Err = errorCodeToError(MemberOrErr.getError()); return child_end(); - return ResultOrErr.get(); + } } } return child_end(); |