From 659a4f2f387e9bdfa5d239cc09783cb331315d6e Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 15 Jul 2016 01:06:38 +0000 Subject: [PATCH] Make check() always return a value. Previously, one of two check functions didn't return a value. It was confusing. This patch makes both functions return values. llvm-svn: 275511 --- lld/COFF/Driver.cpp | 6 ++---- lld/COFF/DriverUtils.cpp | 17 +++++------------ lld/COFF/Error.h | 9 +++++---- lld/COFF/InputFiles.cpp | 37 ++++++++++++------------------------- lld/COFF/Writer.cpp | 8 +++----- 5 files changed, 27 insertions(+), 50 deletions(-) diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 7f8e580..469f0d3 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -61,10 +61,8 @@ static std::string getOutputPath(StringRef Path) { // Opens a file. Path has to be resolved already. // Newly created memory buffers are owned by this driver. MemoryBufferRef LinkerDriver::openFile(StringRef Path) { - auto MBOrErr = MemoryBuffer::getFile(Path); - if (auto EC = MBOrErr.getError()) - fatal(EC, "Could not open " + Path); - std::unique_ptr &MB = *MBOrErr; + std::unique_ptr MB = + check(MemoryBuffer::getFile(Path), "Could not open " + Path); MemoryBufferRef MBRef = MB->getMemBufferRef(); OwningMBs.push_back(std::move(MB)); // take ownership return MBRef; diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp index 3dd1cfe..334c740 100644 --- a/lld/COFF/DriverUtils.cpp +++ b/lld/COFF/DriverUtils.cpp @@ -320,10 +320,9 @@ static std::string createDefaultXml() { } static std::string readFile(StringRef Path) { - ErrorOr> BufOrErr = MemoryBuffer::getFile(Path); - if (auto EC = BufOrErr.getError()) - fatal(EC, "Could not open " + Path); - std::unique_ptr Buf(std::move(*BufOrErr)); + std::unique_ptr MB = + check(MemoryBuffer::getFile(Path), "Could not open " + Path); + std::unique_ptr Buf(std::move(MB)); return Buf->getBuffer(); } @@ -390,10 +389,7 @@ std::unique_ptr createManifestRes() { E.add("/nologo"); E.add(RCPath.str()); E.run(); - ErrorOr> Ret = MemoryBuffer::getFile(ResPath); - if (auto EC = Ret.getError()) - fatal(EC, "Could not open " + ResPath); - return std::move(*Ret); + return check(MemoryBuffer::getFile(ResPath), "Could not open " + ResPath); } void createSideBySideManifest() { @@ -572,10 +568,7 @@ convertResToCOFF(const std::vector &MBs) { for (MemoryBufferRef MB : MBs) E.add(MB.getBufferIdentifier()); E.run(); - ErrorOr> Ret = MemoryBuffer::getFile(Path); - if (auto EC = Ret.getError()) - fatal(EC, "Could not open " + Path); - return std::move(*Ret); + return check(MemoryBuffer::getFile(Path), "Could not open " + Path); } // Create OptTable diff --git a/lld/COFF/Error.h b/lld/COFF/Error.h index 7255d9b..c9f64c6 100644 --- a/lld/COFF/Error.h +++ b/lld/COFF/Error.h @@ -20,15 +20,16 @@ LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg); LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix); LLVM_ATTRIBUTE_NORETURN void fatal(llvm::Error &Err, const Twine &Prefix); -template void check(const ErrorOr &V, const Twine &Prefix) { +template T check(ErrorOr &&V, const Twine &Prefix) { if (auto EC = V.getError()) fatal(EC, Prefix); + return std::move(*V); } template T check(Expected E, const Twine &Prefix) { - if (E) - return std::move(*E); - fatal(E.takeError(), Prefix); + if (llvm::Error Err = E.takeError()) + fatal(Err, Prefix); + return std::move(*E); } } // namespace coff diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index b2f61c2a..54aac74 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -63,10 +63,7 @@ std::string InputFile::getShortName() { void ArchiveFile::parse() { // Parse a MemoryBufferRef as an archive file. - auto ArchiveOrErr = Archive::create(MB); - if (auto Err = ArchiveOrErr.takeError()) - fatal(Err, "Failed to parse static library"); - File = std::move(*ArchiveOrErr); + File = check(Archive::create(MB), "Failed to parse static library"); // Allocate a buffer for Lazy objects. size_t NumSyms = File->getNumberOfSymbols(); @@ -89,27 +86,22 @@ void ArchiveFile::parse() { // Returns a buffer pointing to a member file containing a given symbol. // This function is thread-safe. MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { - auto COrErr = Sym->getMember(); - if (auto EC = COrErr.getError()) - fatal(EC, "Could not get the member for symbol " + Sym->getName()); - const Archive::Child &C = *COrErr; + const Archive::Child &C = + check(Sym->getMember(), + "Could not get the member for symbol " + Sym->getName()); // Return an empty buffer if we have already returned the same buffer. if (Seen[C.getChildOffset()].test_and_set()) return MemoryBufferRef(); - ErrorOr Ret = C.getMemoryBufferRef(); - if (auto EC = Ret.getError()) - fatal(EC, "Could not get the buffer for the member defining symbol " + - Sym->getName()); - return *Ret; + return check(C.getMemoryBufferRef(), + "Could not get the buffer for the member defining symbol " + + Sym->getName()); } void ObjectFile::parse() { // Parse a memory buffer as a COFF file. - auto BinOrErr = createBinary(MB); - if (auto Err = BinOrErr.takeError()) - fatal(Err, "Failed to parse object file"); - std::unique_ptr Bin = std::move(*BinOrErr); + std::unique_ptr Bin = + check(createBinary(MB), "Failed to parse object file"); if (auto *Obj = dyn_cast(Bin.get())) { Bin.release(); @@ -168,11 +160,8 @@ void ObjectFile::initializeSymbols() { int32_t LastSectionNumber = 0; for (uint32_t I = 0; I < NumSymbols; ++I) { // Get a COFFSymbolRef object. - auto SymOrErr = COFFObj->getSymbol(I); - if (auto EC = SymOrErr.getError()) - fatal(EC, "broken object file: " + getName()); - - COFFSymbolRef Sym = *SymOrErr; + COFFSymbolRef Sym = + check(COFFObj->getSymbol(I), "broken object file: " + getName()); const void *AuxP = nullptr; if (Sym.getNumberOfAuxSymbols()) @@ -337,9 +326,7 @@ void BitcodeFile::parse() { Context.enableDebugTypeODRUniquing(); ErrorOr> ModOrErr = LTOModule::createFromBuffer( Context, MB.getBufferStart(), MB.getBufferSize(), llvm::TargetOptions()); - if (auto EC = ModOrErr.getError()) - fatal(EC, "Could not create lto module"); - M = std::move(*ModOrErr); + M = check(std::move(ModOrErr), "Could not create lto module"); llvm::StringSaver Saver(Alloc); for (unsigned I = 0, E = M->getSymbolCount(); I != E; ++I) { diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp index ce29242..f970b51 100644 --- a/lld/COFF/Writer.cpp +++ b/lld/COFF/Writer.cpp @@ -652,11 +652,9 @@ template void Writer::writeHeader() { } void Writer::openFile(StringRef Path) { - ErrorOr> BufferOrErr = - FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); - if (auto EC = BufferOrErr.getError()) - fatal(EC, "failed to open " + Path); - Buffer = std::move(*BufferOrErr); + Buffer = check( + FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable), + "failed to open " + Path); } void Writer::fixSafeSEHSymbols() { -- 2.7.4