[lld] r275511 - Make check() always return a value.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 14 18:06:38 PDT 2016
Author: ruiu
Date: Thu Jul 14 20:06:38 2016
New Revision: 275511
URL: http://llvm.org/viewvc/llvm-project?rev=275511&view=rev
Log:
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.
Modified:
lld/trunk/COFF/Driver.cpp
lld/trunk/COFF/DriverUtils.cpp
lld/trunk/COFF/Error.h
lld/trunk/COFF/InputFiles.cpp
lld/trunk/COFF/Writer.cpp
Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=275511&r1=275510&r2=275511&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Thu Jul 14 20:06:38 2016
@@ -61,10 +61,8 @@ static std::string getOutputPath(StringR
// 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<MemoryBuffer> &MB = *MBOrErr;
+ std::unique_ptr<MemoryBuffer> MB =
+ check(MemoryBuffer::getFile(Path), "Could not open " + Path);
MemoryBufferRef MBRef = MB->getMemBufferRef();
OwningMBs.push_back(std::move(MB)); // take ownership
return MBRef;
Modified: lld/trunk/COFF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/DriverUtils.cpp?rev=275511&r1=275510&r2=275511&view=diff
==============================================================================
--- lld/trunk/COFF/DriverUtils.cpp (original)
+++ lld/trunk/COFF/DriverUtils.cpp Thu Jul 14 20:06:38 2016
@@ -320,10 +320,9 @@ static std::string createDefaultXml() {
}
static std::string readFile(StringRef Path) {
- ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = MemoryBuffer::getFile(Path);
- if (auto EC = BufOrErr.getError())
- fatal(EC, "Could not open " + Path);
- std::unique_ptr<MemoryBuffer> Buf(std::move(*BufOrErr));
+ std::unique_ptr<MemoryBuffer> MB =
+ check(MemoryBuffer::getFile(Path), "Could not open " + Path);
+ std::unique_ptr<MemoryBuffer> Buf(std::move(MB));
return Buf->getBuffer();
}
@@ -390,10 +389,7 @@ std::unique_ptr<MemoryBuffer> createMani
E.add("/nologo");
E.add(RCPath.str());
E.run();
- ErrorOr<std::unique_ptr<MemoryBuffer>> 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<Memor
for (MemoryBufferRef MB : MBs)
E.add(MB.getBufferIdentifier());
E.run();
- ErrorOr<std::unique_ptr<MemoryBuffer>> 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
Modified: lld/trunk/COFF/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.h?rev=275511&r1=275510&r2=275511&view=diff
==============================================================================
--- lld/trunk/COFF/Error.h (original)
+++ lld/trunk/COFF/Error.h Thu Jul 14 20:06:38 2016
@@ -20,15 +20,16 @@ LLVM_ATTRIBUTE_NORETURN void fatal(const
LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
LLVM_ATTRIBUTE_NORETURN void fatal(llvm::Error &Err, const Twine &Prefix);
-template <typename T> void check(const ErrorOr<T> &V, const Twine &Prefix) {
+template <class T> T check(ErrorOr<T> &&V, const Twine &Prefix) {
if (auto EC = V.getError())
fatal(EC, Prefix);
+ return std::move(*V);
}
template <class T> T check(Expected<T> 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
Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=275511&r1=275510&r2=275511&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Thu Jul 14 20:06:38 2016
@@ -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<MemoryBufferRef> 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<Binary> Bin = std::move(*BinOrErr);
+ std::unique_ptr<Binary> Bin =
+ check(createBinary(MB), "Failed to parse object file");
if (auto *Obj = dyn_cast<COFFObjectFile>(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<std::unique_ptr<LTOModule>> 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) {
Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=275511&r1=275510&r2=275511&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Thu Jul 14 20:06:38 2016
@@ -652,11 +652,9 @@ template <typename PEHeaderTy> void Writ
}
void Writer::openFile(StringRef Path) {
- ErrorOr<std::unique_ptr<FileOutputBuffer>> 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() {
More information about the llvm-commits
mailing list