[llvm] r273429 - llvm-ar: reduce some duplication, NFC
Saleem Abdulrasool via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 22 08:44:25 PDT 2016
Author: compnerd
Date: Wed Jun 22 10:44:25 2016
New Revision: 273429
URL: http://llvm.org/viewvc/llvm-project?rev=273429&view=rev
Log:
llvm-ar: reduce some duplication, NFC
Improve the previous change by using a local function to reduce the duplication
of the object file scanning. NFC.
Modified:
llvm/trunk/tools/llvm-ar/llvm-ar.cpp
Modified: llvm/trunk/tools/llvm-ar/llvm-ar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/llvm-ar.cpp?rev=273429&r1=273428&r2=273429&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-ar/llvm-ar.cpp (original)
+++ llvm/trunk/tools/llvm-ar/llvm-ar.cpp Wed Jun 22 10:44:25 2016
@@ -583,27 +583,29 @@ static object::Archive::Kind getDefaultF
static object::Archive::Kind
getKindFromMember(const NewArchiveIterator &Member) {
+ auto getKindFromMemberInner =
+ [](MemoryBufferRef Buffer) -> object::Archive::Kind {
+ Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
+ object::ObjectFile::createObjectFile(Buffer);
+
+ if (OptionalObject)
+ return isa<object::MachOObjectFile>(**OptionalObject)
+ ? object::Archive::K_BSD
+ : object::Archive::K_GNU;
+
+ // squelch the error in case we had a non-object file
+ consumeError(OptionalObject.takeError());
+ return getDefaultForHost();
+ };
+
if (Member.isNewMember()) {
object::Archive::Kind Kind = getDefaultForHost();
sys::fs::file_status Status;
if (auto OptionalFD = Member.getFD(Status)) {
- auto OptionalMB = MemoryBuffer::getOpenFile(*OptionalFD, Member.getName(),
- Status.getSize(), false);
- if (OptionalMB) {
- MemoryBufferRef MemoryBuffer = (*OptionalMB)->getMemBufferRef();
-
- Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
- object::ObjectFile::createObjectFile(MemoryBuffer);
-
- if (OptionalObject)
- Kind = isa<object::MachOObjectFile>(**OptionalObject)
- ? object::Archive::K_BSD
- : object::Archive::K_GNU;
-
- // squelch the error in case we had a non-object file
- consumeError(OptionalObject.takeError());
- }
+ if (auto MB = MemoryBuffer::getOpenFile(*OptionalFD, Member.getName(),
+ Status.getSize(), false))
+ Kind = getKindFromMemberInner((*MB)->getMemBufferRef());
if (close(*OptionalFD) != 0)
failIfError(std::error_code(errno, std::generic_category()),
@@ -619,17 +621,7 @@ getKindFromMember(const NewArchiveIterat
auto OptionalMB = OldMember.getMemoryBufferRef();
failIfError(OptionalMB.getError());
- Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
- object::ObjectFile::createObjectFile(*OptionalMB);
-
- if (OptionalObject)
- return isa<object::MachOObjectFile>(*OptionalObject->get())
- ? object::Archive::K_BSD
- : object::Archive::K_GNU;
-
- // squelch the error in case we had a non-object file
- consumeError(OptionalObject.takeError());
- return getDefaultForHost();
+ return getKindFromMemberInner(*OptionalMB);
}
}
More information about the llvm-commits
mailing list