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
This commit is contained in:
Rui Ueyama 2016-07-15 01:06:38 +00:00
parent b91805ea2b
commit 659a4f2f38
5 changed files with 27 additions and 50 deletions

View File

@ -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<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;

View File

@ -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> createManifestRes() {
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<MemoryBufferRef> &MBs) {
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

View File

@ -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 <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

View File

@ -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) {

View File

@ -652,11 +652,9 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
}
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() {