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 /tools/llvm-size | |
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 'tools/llvm-size')
-rw-r--r-- | tools/llvm-size/llvm-size.cpp | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/tools/llvm-size/llvm-size.cpp b/tools/llvm-size/llvm-size.cpp index ecc0a0eac3c..c5966ead4b6 100644 --- a/tools/llvm-size/llvm-size.cpp +++ b/tools/llvm-size/llvm-size.cpp @@ -527,15 +527,12 @@ static void printFileSectionSizes(StringRef file) { if (Archive *a = dyn_cast<Archive>(&Bin)) { // This is an archive. Iterate over each member and display its sizes. - for (object::Archive::child_iterator i = a->child_begin(), - e = a->child_end(); - i != e; ++i) { - if (error(i->getError())) - exit(1); - Expected<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary(); + Error Err; + for (auto &C : a->children(Err)) { + Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) - error(std::move(E), a->getFileName(), i->get()); + error(std::move(E), a->getFileName(), C); continue; } if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) { @@ -555,6 +552,8 @@ static void printFileSectionSizes(StringRef file) { } } } + if (Err) + error(std::move(Err), a->getFileName()); } else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) { // If we have a list of architecture flags specified dump only those. @@ -597,17 +596,13 @@ static void printFileSectionSizes(StringRef file) { std::unique_ptr<Archive> &UA = *AOrErr; // This is an archive. Iterate over each member and display its // sizes. - for (object::Archive::child_iterator i = UA->child_begin(), - e = UA->child_end(); - i != e; ++i) { - if (error(i->getError())) - exit(1); - Expected<std::unique_ptr<Binary>> ChildOrErr = - i->get().getAsBinary(); + Error Err; + for (auto &C : UA->children(Err)) { + Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (auto E = isNotObjectErrorInvalidFileType( ChildOrErr.takeError())) - error(std::move(E), UA->getFileName(), i->get(), + error(std::move(E), UA->getFileName(), C, ArchFlags.size() > 1 ? StringRef(I->getArchTypeName()) : StringRef()); continue; @@ -637,6 +632,8 @@ static void printFileSectionSizes(StringRef file) { } } } + if (Err) + error(std::move(Err), UA->getFileName()); } else { consumeError(AOrErr.takeError()); error("Mach-O universal file: " + file + " for architecture " + @@ -688,17 +685,13 @@ static void printFileSectionSizes(StringRef file) { std::unique_ptr<Archive> &UA = *AOrErr; // This is an archive. Iterate over each member and display its // sizes. - for (object::Archive::child_iterator i = UA->child_begin(), - e = UA->child_end(); - i != e; ++i) { - if (error(i->getError())) - exit(1); - Expected<std::unique_ptr<Binary>> ChildOrErr = - i->get().getAsBinary(); + Error Err; + for (auto &C : UA->children(Err)) { + Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (auto E = isNotObjectErrorInvalidFileType( ChildOrErr.takeError())) - error(std::move(E), UA->getFileName(), i->get()); + error(std::move(E), UA->getFileName(), C); continue; } if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) { @@ -721,6 +714,8 @@ static void printFileSectionSizes(StringRef file) { } } } + if (Err) + error(std::move(Err), UA->getFileName()); } else { consumeError(AOrErr.takeError()); error("Mach-O universal file: " + file + " for architecture " + @@ -765,16 +760,13 @@ static void printFileSectionSizes(StringRef file) { I->getAsArchive()) { std::unique_ptr<Archive> &UA = *AOrErr; // This is an archive. Iterate over each member and display its sizes. - for (object::Archive::child_iterator i = UA->child_begin(), - e = UA->child_end(); - i != e; ++i) { - if (error(i->getError())) - exit(1); - Expected<std::unique_ptr<Binary>> ChildOrErr = i->get().getAsBinary(); + Error Err; + for (auto &C : UA->children(Err)) { + Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (auto E = isNotObjectErrorInvalidFileType( ChildOrErr.takeError())) - error(std::move(E), UA->getFileName(), i->get(), MoreThanOneArch ? + error(std::move(E), UA->getFileName(), C, MoreThanOneArch ? StringRef(I->getArchTypeName()) : StringRef()); continue; } @@ -798,6 +790,8 @@ static void printFileSectionSizes(StringRef file) { } } } + if (Err) + error(std::move(Err), UA->getFileName()); } else { consumeError(AOrErr.takeError()); error("Mach-O universal file: " + file + " for architecture " + |