llvm-project/llvm/tools/llvm-objcopy
Lang Hames 3e040e05f8 [ADT] Add a fallible_iterator wrapper.
A fallible iterator is one whose increment or decrement operations may fail.
This would usually be supported by replacing the ++ and -- operators with
methods that return error:

    class MyFallibleIterator {
    public:
      // ...
      Error inc();
      Errro dec();
      // ...
    };

The downside of this style is that it no longer conforms to the C++ iterator
concept, and can not make use of standard algorithms and features such as
range-based for loops.

The fallible_iterator wrapper takes an iterator written in the style above
and adapts it to (mostly) conform with the C++ iterator concept. It does this
by providing standard ++ and -- operator implementations, returning any errors
generated via a side channel (an Error reference passed into the wrapper at
construction time), and immediately jumping the iterator to a known 'end'
value upon error. It also marks the Error as checked any time an iterator is
compared with a known end value and found to be inequal, allowing early exit
from loops without redundant error checking*.

Usage looks like:

    MyFallibleIterator I = ..., E = ...;

    Error Err = Error::success();
    for (auto &Elem : make_fallible_range(I, E, Err)) {
      // Loop body is only entered when safe.

      // Early exits from loop body permitted without checking Err.
      if (SomeCondition)
        return;

    }
    if (Err)
      // Handle error.

* Since failure causes a fallible iterator to jump to end, testing that a
  fallible iterator is not an end value implicitly verifies that the error is a
  success value, and so is equivalent to an error check.

Reviewers: dblaikie, rupprecht

Subscribers: mgorny, dexonsmith, kristina, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D57618

llvm-svn: 353237
2019-02-05 23:17:11 +00:00
..
COFF [llvm-objcopy] Support -X|--discard-locals. 2019-01-30 14:58:13 +00:00
ELF [llvm-objcopy][NFC] Propagate errors in removeSymbols/removeSectionReferences 2019-02-01 15:20:36 +00:00
MachO [llvm-objcopy][NFC] simplify an error return 2019-02-04 19:09:20 +00:00
Buffer.cpp [llvm-objcopy] Fix crash when writing empty binary output 2019-01-28 15:02:40 +00:00
Buffer.h [llvm-objcopy] Fix crash when writing empty binary output 2019-01-28 15:02:40 +00:00
CMakeLists.txt [llvm-objcopy] Add ability to copy MachO object files 2019-02-02 00:38:07 +00:00
CopyConfig.cpp [llvm-objcopy][NFC] Use StringSaver for --keep-global-symbols 2019-02-04 18:38:00 +00:00
CopyConfig.h [llvm-objcopy][NFC] Use StringSaver for --keep-global-symbols 2019-02-04 18:38:00 +00:00
LLVMBuild.txt Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ObjcopyOpts.td [llvm-objcopy] Support -X|--discard-locals. 2019-01-30 14:58:13 +00:00
StripOpts.td [llvm-strip] Implement --keep-file-symbols 2019-02-01 15:25:15 +00:00
llvm-objcopy.cpp [ADT] Add a fallible_iterator wrapper. 2019-02-05 23:17:11 +00:00
llvm-objcopy.h [llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit() 2019-01-22 23:49:16 +00:00