Simplify error handling.

This makes fatal return T when there is no error. This avoids the need
for quite a few temporaries.

llvm-svn: 262626
This commit is contained in:
Rafael Espindola 2016-03-03 16:21:44 +00:00
parent b68e2db8f9
commit 1130935c4a
7 changed files with 54 additions and 79 deletions

View File

@ -70,17 +70,18 @@ static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
// Returns slices of MB by parsing MB as an archive file. // Returns slices of MB by parsing MB as an archive file.
// Each slice consists of a member file in the archive. // Each slice consists of a member file in the archive.
static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) { static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) {
ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB); std::unique_ptr<Archive> File =
fatal(FileOrErr, "Failed to parse archive"); fatal(Archive::create(MB), "Failed to parse archive");
std::unique_ptr<Archive> File = std::move(*FileOrErr);
std::vector<MemoryBufferRef> V; std::vector<MemoryBufferRef> V;
for (const ErrorOr<Archive::Child> &C : File->children()) { for (const ErrorOr<Archive::Child> &COrErr : File->children()) {
fatal(C, "Could not get the child of the archive " + File->getFileName()); Archive::Child C = fatal(COrErr, "Could not get the child of the archive " +
ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef();
fatal(MbOrErr, "Could not get the buffer for a child of the archive " +
File->getFileName()); File->getFileName());
V.push_back(*MbOrErr); MemoryBufferRef Mb =
fatal(C.getMemoryBufferRef(),
"Could not get the buffer for a child of the archive " +
File->getFileName());
V.push_back(Mb);
} }
return V; return V;
} }

View File

@ -50,6 +50,10 @@ void fatal(const Twine &Msg) {
exit(1); exit(1);
} }
void fatal(const Twine &Msg, const Twine &Prefix) {
fatal(Prefix + ": " + Msg);
}
void fatal(std::error_code EC, const Twine &Prefix) { void fatal(std::error_code EC, const Twine &Prefix) {
if (EC) if (EC)
fatal(Prefix + ": " + EC.message()); fatal(Prefix + ": " + EC.message());

View File

@ -38,14 +38,21 @@ template <typename T> bool error(const ErrorOr<T> &V) {
} }
LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg); LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg, const Twine &Prefix);
void fatal(std::error_code EC, const Twine &Prefix); void fatal(std::error_code EC, const Twine &Prefix);
void fatal(std::error_code EC); void fatal(std::error_code EC);
template <typename T> void fatal(const ErrorOr<T> &V, const Twine &Prefix) { template <class T> T fatal(ErrorOr<T> EO) {
fatal(V.getError(), Prefix); if (EO)
return std::move(*EO);
fatal(EO.getError().message());
} }
template <typename T> void fatal(const ErrorOr<T> &V) { fatal(V.getError()); } template <class T> T fatal(ErrorOr<T> EO, const Twine &Prefix) {
if (EO)
return std::move(*EO);
fatal(EO.getError().message(), Prefix);
}
} // namespace elf } // namespace elf
} // namespace lld } // namespace lld

View File

@ -73,9 +73,7 @@ uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
template <class ELFT> void ELFFileBase<ELFT>::initStringTable() { template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
if (!Symtab) if (!Symtab)
return; return;
ErrorOr<StringRef> StringTableOrErr = ELFObj.getStringTableForSymtab(*Symtab); StringTable = fatal(ELFObj.getStringTableForSymtab(*Symtab));
fatal(StringTableOrErr);
StringTable = *StringTableOrErr;
} }
template <class ELFT> template <class ELFT>
@ -124,26 +122,19 @@ template <class ELFT>
StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) { StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
const ELFFile<ELFT> &Obj = this->ELFObj; const ELFFile<ELFT> &Obj = this->ELFObj;
uint32_t SymtabdSectionIndex = Sec.sh_link; uint32_t SymtabdSectionIndex = Sec.sh_link;
ErrorOr<const Elf_Shdr *> SecOrErr = Obj.getSection(SymtabdSectionIndex); const Elf_Shdr *SymtabSec = fatal(Obj.getSection(SymtabdSectionIndex));
fatal(SecOrErr);
const Elf_Shdr *SymtabSec = *SecOrErr;
uint32_t SymIndex = Sec.sh_info; uint32_t SymIndex = Sec.sh_info;
const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex); const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
ErrorOr<StringRef> StringTableOrErr = Obj.getStringTableForSymtab(*SymtabSec); StringRef StringTable = fatal(Obj.getStringTableForSymtab(*SymtabSec));
fatal(StringTableOrErr); return fatal(Sym->getName(StringTable));
ErrorOr<StringRef> SignatureOrErr = Sym->getName(*StringTableOrErr);
fatal(SignatureOrErr);
return *SignatureOrErr;
} }
template <class ELFT> template <class ELFT>
ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X> ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X>
elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
const ELFFile<ELFT> &Obj = this->ELFObj; const ELFFile<ELFT> &Obj = this->ELFObj;
ErrorOr<ArrayRef<uint32_X>> EntriesOrErr = ArrayRef<uint32_X> Entries =
Obj.template getSectionContentsAsArray<uint32_X>(&Sec); fatal(Obj.template getSectionContentsAsArray<uint32_X>(&Sec));
fatal(EntriesOrErr);
ArrayRef<uint32_X> Entries = *EntriesOrErr;
if (Entries.empty() || Entries[0] != GRP_COMDAT) if (Entries.empty() || Entries[0] != GRP_COMDAT)
fatal("Unsupported SHT_GROUP format"); fatal("Unsupported SHT_GROUP format");
return Entries.slice(1); return Entries.slice(1);
@ -202,12 +193,9 @@ void elf::ObjectFile<ELFT>::initializeSections(
case SHT_SYMTAB: case SHT_SYMTAB:
this->Symtab = &Sec; this->Symtab = &Sec;
break; break;
case SHT_SYMTAB_SHNDX: { case SHT_SYMTAB_SHNDX:
ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec); this->SymtabSHNDX = fatal(Obj.getSHNDXTable(Sec));
fatal(ErrorOrTable);
this->SymtabSHNDX = *ErrorOrTable;
break; break;
}
case SHT_STRTAB: case SHT_STRTAB:
case SHT_NULL: case SHT_NULL:
break; break;
@ -248,9 +236,7 @@ void elf::ObjectFile<ELFT>::initializeSections(
template <class ELFT> template <class ELFT>
InputSectionBase<ELFT> * InputSectionBase<ELFT> *
elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
ErrorOr<StringRef> NameOrErr = this->ELFObj.getSectionName(&Sec); StringRef Name = fatal(this->ELFObj.getSectionName(&Sec));
fatal(NameOrErr);
StringRef Name = *NameOrErr;
// .note.GNU-stack is a marker section to control the presence of // .note.GNU-stack is a marker section to control the presence of
// PT_GNU_STACK segment in outputs. Since the presence of the segment // PT_GNU_STACK segment in outputs. Since the presence of the segment
@ -300,9 +286,7 @@ elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
template <class ELFT> template <class ELFT>
SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) { SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
ErrorOr<StringRef> NameOrErr = Sym->getName(this->StringTable); StringRef Name = fatal(Sym->getName(this->StringTable));
fatal(NameOrErr);
StringRef Name = *NameOrErr;
switch (Sym->st_shndx) { switch (Sym->st_shndx) {
case SHN_UNDEF: case SHN_UNDEF:
@ -328,9 +312,7 @@ SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
} }
void ArchiveFile::parse() { void ArchiveFile::parse() {
ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB); File = fatal(Archive::create(MB), "Failed to parse archive");
fatal(FileOrErr, "Failed to parse archive");
File = std::move(*FileOrErr);
// Allocate a buffer for Lazy objects. // Allocate a buffer for Lazy objects.
size_t NumSyms = File->getNumberOfSymbols(); size_t NumSyms = File->getNumberOfSymbols();
@ -343,18 +325,16 @@ void ArchiveFile::parse() {
// Returns a buffer pointing to a member file containing a given symbol. // Returns a buffer pointing to a member file containing a given symbol.
MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
ErrorOr<Archive::Child> COrErr = Sym->getMember(); Archive::Child C =
fatal(COrErr, "Could not get the member for symbol " + Sym->getName()); fatal(Sym->getMember(),
const Archive::Child &C = *COrErr; "Could not get the member for symbol " + Sym->getName());
if (!Seen.insert(C.getChildOffset()).second) if (!Seen.insert(C.getChildOffset()).second)
return MemoryBufferRef(); return MemoryBufferRef();
ErrorOr<MemoryBufferRef> RefOrErr = C.getMemoryBufferRef(); return fatal(C.getMemoryBufferRef(),
if (!RefOrErr) "Could not get the buffer for the member defining symbol " +
fatal(RefOrErr, "Could not get the buffer for the member defining symbol " +
Sym->getName()); Sym->getName());
return *RefOrErr;
} }
template <class ELFT> template <class ELFT>
@ -367,9 +347,7 @@ SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
uint32_t Index = this->getSectionIndex(Sym); uint32_t Index = this->getSectionIndex(Sym);
if (Index == 0) if (Index == 0)
return nullptr; return nullptr;
ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index); return fatal(this->ELFObj.getSection(Index));
fatal(Ret);
return *Ret;
} }
// Partially parse the shared object file so that we can call // Partially parse the shared object file so that we can call
@ -390,14 +368,11 @@ template <class ELFT> void SharedFile<ELFT>::parseSoName() {
case SHT_DYNAMIC: case SHT_DYNAMIC:
DynamicSec = &Sec; DynamicSec = &Sec;
break; break;
case SHT_SYMTAB_SHNDX: { case SHT_SYMTAB_SHNDX:
ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec); this->SymtabSHNDX = fatal(Obj.getSHNDXTable(Sec));
fatal(ErrorOrTable);
this->SymtabSHNDX = *ErrorOrTable;
break; break;
} }
} }
}
this->initStringTable(); this->initStringTable();
SoName = this->getName(); SoName = this->getName();
@ -444,11 +419,8 @@ bool BitcodeFile::classof(const InputFile *F) {
void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) { void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
LLVMContext Context; LLVMContext Context;
ErrorOr<std::unique_ptr<IRObjectFile>> ObjOrErr = std::unique_ptr<IRObjectFile> Obj = fatal(IRObjectFile::create(MB, Context));
IRObjectFile::create(MB, Context); const Module &M = Obj->getModule();
fatal(ObjOrErr);
IRObjectFile &Obj = **ObjOrErr;
const Module &M = Obj.getModule();
DenseSet<const Comdat *> KeptComdats; DenseSet<const Comdat *> KeptComdats;
for (const auto &P : M.getComdatSymbolTable()) { for (const auto &P : M.getComdatSymbolTable()) {
@ -457,8 +429,8 @@ void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
KeptComdats.insert(&P.second); KeptComdats.insert(&P.second);
} }
for (const BasicSymbolRef &Sym : Obj.symbols()) { for (const BasicSymbolRef &Sym : Obj->symbols()) {
if (const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl())) if (const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()))
if (const Comdat *C = GV->getComdat()) if (const Comdat *C = GV->getComdat())
if (!KeptComdats.count(C)) if (!KeptComdats.count(C))
continue; continue;

View File

@ -39,17 +39,12 @@ InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
} }
template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const { template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header); return fatal(File->getObj().getSectionName(this->Header));
fatal(Name);
return *Name;
} }
template <class ELFT> template <class ELFT>
ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const { ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
ErrorOr<ArrayRef<uint8_t>> Ret = return fatal(this->File->getObj().getSectionContents(this->Header));
this->File->getObj().getSectionContents(this->Header);
fatal(Ret);
return *Ret;
} }
template <class ELFT> template <class ELFT>

View File

@ -136,11 +136,9 @@ ObjectFile<ELFT> *SymbolTable<ELFT>::createCombinedLtoObject() {
for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) { for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) {
std::unique_ptr<MemoryBuffer> Buffer = std::unique_ptr<MemoryBuffer> Buffer =
MemoryBuffer::getMemBuffer(F->MB, false); MemoryBuffer::getMemBuffer(F->MB, false);
ErrorOr<std::unique_ptr<Module>> MOrErr = std::unique_ptr<Module> M =
getLazyBitcodeModule(std::move(Buffer), Context, fatal(getLazyBitcodeModule(std::move(Buffer), Context,
/*ShouldLazyLoadMetadata*/ true); /*ShouldLazyLoadMetadata*/ true));
fatal(MOrErr);
std::unique_ptr<Module> &M = *MOrErr;
L.linkInModule(std::move(M)); L.linkInModule(std::move(M));
} }
std::unique_ptr<InputFile> F = codegen(Combined); std::unique_ptr<InputFile> F = codegen(Combined);

View File

@ -564,9 +564,7 @@ template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
return; return;
for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) { for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
for (const Elf_Sym &Sym : F->getLocalSymbols()) { for (const Elf_Sym &Sym : F->getLocalSymbols()) {
ErrorOr<StringRef> SymNameOrErr = Sym.getName(F->getStringTable()); StringRef SymName = fatal(Sym.getName(F->getStringTable()));
fatal(SymNameOrErr);
StringRef SymName = *SymNameOrErr;
if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym)) if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym))
continue; continue;
if (Sym.st_shndx != SHN_ABS) { if (Sym.st_shndx != SHN_ABS) {