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 | 9b42acafff06fbba8d335fa77079fc4e72002ac4 (patch) | |
tree | 00632ed82e9606126df11f3acc0853b96bbc7214 /include | |
parent | 3d35f0d482cf5ce6cf87d10a108ade79e7cc2065 (diff) |
[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).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275316 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/Object/Archive.h | 41 | ||||
-rw-r--r-- | include/llvm/Support/Error.h | 5 |
2 files changed, 29 insertions, 17 deletions
diff --git a/include/llvm/Object/Archive.h b/include/llvm/Object/Archive.h index 77538cb5323..f209cb32351 100644 --- a/include/llvm/Object/Archive.h +++ b/include/llvm/Object/Archive.h @@ -106,21 +106,20 @@ public: }; class child_iterator { - ErrorOr<Child> child; + Child C; + Error *E; public: - child_iterator() : child(Child(nullptr, nullptr, nullptr)) {} - child_iterator(const Child &c) : child(c) {} - child_iterator(std::error_code EC) : child(EC) {} - const ErrorOr<Child> *operator->() const { return &child; } - const ErrorOr<Child> &operator*() const { return child; } + child_iterator() : C(Child(nullptr, nullptr, nullptr)), E(nullptr) {} + child_iterator(const Child &C, Error *E) : C(C), E(E) {} + const Child *operator->() const { return &C; } + const Child &operator*() const { return C; } bool operator==(const child_iterator &other) const { - // We ignore error states so that comparisions with end() work, which - // allows range loops. - if (child.getError() || other.child.getError()) - return false; - return *child == *other.child; + // Ignore errors here: If an error occurred during increment then getNext + // will have been set to child_end(), and the following comparison should + // do the right thing. + return C == other.C; } bool operator!=(const child_iterator &other) const { @@ -130,8 +129,15 @@ public: // Code in loops with child_iterators must check for errors on each loop // iteration. And if there is an error break out of the loop. child_iterator &operator++() { // Preincrement - assert(child && "Can't increment iterator with error"); - child = child->getNext(); + assert(E && "Can't increment iterator with no Error attached"); + if (auto ChildOrErr = C.getNext()) + C = *ChildOrErr; + else { + ErrorAsOutParameter ErrAsOutParam(*E); + C = C.getParent()->child_end().C; + *E = errorCodeToError(ChildOrErr.getError()); + E = nullptr; + } return *this; } }; @@ -190,10 +196,11 @@ public: Kind kind() const { return (Kind)Format; } bool isThin() const { return IsThin; } - child_iterator child_begin(bool SkipInternal = true) const; + child_iterator child_begin(Error &Err, bool SkipInternal = true) const; child_iterator child_end() const; - iterator_range<child_iterator> children(bool SkipInternal = true) const { - return make_range(child_begin(SkipInternal), child_end()); + iterator_range<child_iterator> children(Error &Err, + bool SkipInternal = true) const { + return make_range(child_begin(Err, SkipInternal), child_end()); } symbol_iterator symbol_begin() const; @@ -208,7 +215,7 @@ public: } // check if a symbol is in the archive - child_iterator findSym(StringRef name) const; + child_iterator findSym(Error &Err, StringRef name) const; bool hasSymbolTable() const; StringRef getSymbolTable() const { return SymbolTable; } diff --git a/include/llvm/Support/Error.h b/include/llvm/Support/Error.h index 645088ceb67..5f515a88a02 100644 --- a/include/llvm/Support/Error.h +++ b/include/llvm/Support/Error.h @@ -940,6 +940,11 @@ private: std::function<int(const Error &)> GetExitCode; }; +/// Report a serious error, calling any installed error handler. See +/// ErrorHandling.h. +LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, + bool gen_crash_diag = true); + } // namespace llvm #endif // LLVM_SUPPORT_ERROR_H |