diff options
author | George Rimar <grimar@accesssoftek.com> | 2019-08-14 11:10:11 +0000 |
---|---|---|
committer | George Rimar <grimar@accesssoftek.com> | 2019-08-14 11:10:11 +0000 |
commit | bcc00e1afbdc107e05b2977fde26053a52295dd1 (patch) | |
tree | 0ad3e328d73375164f99adb046a919273a128c1c /llvm/lib | |
parent | a11d302fa00ba423b09c94491bc868cb991ff379 (diff) | |
download | llvm-bcc00e1afbdc107e05b2977fde26053a52295dd1.zip llvm-bcc00e1afbdc107e05b2977fde26053a52295dd1.tar.gz llvm-bcc00e1afbdc107e05b2977fde26053a52295dd1.tar.bz2 |
Recommit r368812 "[llvm/Object] - Convert SectionRef::getName() to return Expected<>"
Changes: no changes. A fix for the clang code will be landed right on top.
Original commit message:
SectionRef::getName() returns std::error_code now.
Returning Expected<> instead has multiple benefits.
For example, it forces user to check the error returned.
Also Expected<> may keep a valuable string error message,
what is more useful than having a error code.
(Object\invalid.test was updated to show the new messages printed.)
This patch makes a change for all users to switch to Expected<> version.
Note: in a few places the error returned was ignored before my changes.
In such places I left them ignored. My intention was to convert the interface
used, and not to improve and/or the existent users in this patch.
(Though I think this is good idea for a follow-ups to revisit such places
and either remove consumeError calls or comment each of them to clarify why
it is OK to have them).
Differential revision: https://reviews.llvm.org/D66089
llvm-svn: 368826
Diffstat (limited to 'llvm/lib')
17 files changed, 113 insertions, 63 deletions
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 4b66a30..2379ba6 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1506,7 +1506,11 @@ public: StringMap<unsigned> SectionAmountMap; for (const SectionRef &Section : Obj.sections()) { StringRef Name; - Section.getName(Name); + if (auto NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + ++SectionAmountMap[Name]; SectionNames.push_back({ Name, true }); @@ -1571,12 +1575,15 @@ public: continue; StringRef RelSecName; - StringRef RelSecData; - RelocatedSection->getName(RelSecName); + if (auto NameOrErr = RelocatedSection->getName()) + RelSecName = *NameOrErr; + else + consumeError(NameOrErr.takeError()); // If the section we're relocating was relocated already by the JIT, // then we used the relocated version above, so we do not need to process // relocations for it now. + StringRef RelSecData; if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData)) continue; diff --git a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp index bc4070b..b4d49d9 100644 --- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp @@ -54,10 +54,11 @@ SymbolizableObjectFile::create(const object::ObjectFile *Obj, // PowerPC64 ELF. if (Obj->getArch() == Triple::ppc64) { for (section_iterator Section : Obj->sections()) { - StringRef Name; - if (auto EC = Section->getName(Name)) - return EC; - if (Name == ".opd") { + Expected<StringRef> NameOrErr = Section->getName(); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); + + if (*NameOrErr == ".opd") { Expected<StringRef> E = Section->getContents(); if (!E) return errorToErrorCode(E.takeError()); diff --git a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp index 271d0cc..a555937 100644 --- a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -259,7 +259,11 @@ bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, return false; for (const SectionRef &Section : Obj->sections()) { StringRef Name; - Section.getName(Name); + if (Expected<StringRef> NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + Name = Name.substr(Name.find_first_not_of("._")); if (Name == "gnu_debuglink") { Expected<StringRef> ContentsOrErr = Section.getContents(); diff --git a/llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp index 1501c7a..92c7e49 100644 --- a/llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp @@ -96,9 +96,10 @@ Error MachOAtomGraphBuilder::parseSections() { assert((SecRef.getAlignment() <= std::numeric_limits<uint32_t>::max()) && "Section alignment does not fit in 32 bits"); - StringRef Name; - if (auto EC = SecRef.getName(Name)) - return errorCodeToError(EC); + Expected<StringRef> NameOrErr = SecRef.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; unsigned SectionIndex = SecRef.getIndex() + 1; diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index f73d1c6..4b32862 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -535,9 +535,10 @@ Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, bool IsCode = Section.isText(); bool IsReadOnly = isReadOnlyData(Section); - StringRef Name; - if (auto EC = Section.getName(Name)) - return errorCodeToError(EC); + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section); @@ -777,9 +778,10 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj, // anyway, so we should guarantee that the alignment is always at least 1. Alignment = std::max(1u, Alignment); - StringRef Name; - if (auto EC = Section.getName(Name)) - return errorCodeToError(EC); + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef Name = *NameOrErr; StubBufSize = computeSectionStubBufSize(Obj, Section); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index 60041a4..e3ace26 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -160,9 +160,13 @@ createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject, // Iterate over all sections in the object. auto SI = SourceObject.section_begin(); for (const auto &Sec : Obj->sections()) { - StringRef SectionName; - Sec.getName(SectionName); - if (SectionName != "") { + Expected<StringRef> NameOrErr = Sec.getName(); + if (!NameOrErr) { + consumeError(NameOrErr.takeError()); + continue; + } + + if (*NameOrErr != "") { DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); Elf_Shdr *shdr = const_cast<Elf_Shdr *>( reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); @@ -567,10 +571,11 @@ Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj, // The TOC consists of sections .got, .toc, .tocbss, .plt in that // order. The TOC starts where the first of these sections starts. - for (auto &Section: Obj.sections()) { - StringRef SectionName; - if (auto EC = Section.getName(SectionName)) - return errorCodeToError(EC); + for (auto &Section : Obj.sections()) { + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef SectionName = *NameOrErr; if (SectionName == ".got" || SectionName == ".toc" @@ -605,9 +610,10 @@ Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj, if (RelSecI == Obj.section_end()) continue; - StringRef RelSectionName; - if (auto EC = RelSecI->getName(RelSectionName)) - return errorCodeToError(EC); + Expected<StringRef> NameOrErr = RelSecI->getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + StringRef RelSectionName = *NameOrErr; if (RelSectionName != ".opd") continue; @@ -1879,8 +1885,14 @@ Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap::iterator i, e; for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { const SectionRef &Section = i->first; + StringRef Name; - Section.getName(Name); + Expected<StringRef> NameOrErr = Section.getName(); + if (NameOrErr) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); + if (Name == ".eh_frame") { UnregisteredEHFrameSections.push_back(i->second); break; diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index 202c3ca..fbdfb8d 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -233,7 +233,10 @@ RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj, for (const auto &Section : Obj.sections()) { StringRef Name; - Section.getName(Name); + if (Expected<StringRef> NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); // Force emission of the __text, __eh_frame, and __gcc_except_tab sections // if they're present. Otherwise call down to the impl to handle other diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h index d2d7453..dc4af08 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h @@ -284,14 +284,14 @@ public: // Look for and record the EH frame section IDs. for (const auto &SectionPair : SectionMap) { const object::SectionRef &Section = SectionPair.first; - StringRef Name; - if (auto EC = Section.getName(Name)) - return errorCodeToError(EC); + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); // Note unwind info is stored in .pdata but often points to .xdata // with an IMAGE_REL_AMD64_ADDR32NB relocation. Using a memory manager // that keeps sections ordered in relation to __ImageBase is necessary. - if (Name == ".pdata") + if ((*NameOrErr) == ".pdata") UnregisteredEHFrameSections.push_back(SectionPair.second); } return Error::success(); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h index 3bec8b9..a76958a 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h @@ -289,7 +289,10 @@ public: Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section) { StringRef Name; - Section.getName(Name); + if (Expected<StringRef> NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); if (Name == "__nl_symbol_ptr") return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj), diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h index f0de27b..523deb2 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h @@ -128,7 +128,10 @@ public: Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section) { StringRef Name; - Section.getName(Name); + if (Expected<StringRef> NameOrErr = Section.getName()) + Name = *NameOrErr; + else + consumeError(NameOrErr.takeError()); if (Name == "__jump_table") return populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID); diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp index 854664e..afd6869 100644 --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -994,11 +994,12 @@ std::error_code COFFObjectFile::getSection(int32_t Index, std::error_code COFFObjectFile::getSection(StringRef SectionName, const coff_section *&Result) const { Result = nullptr; - StringRef SecName; for (const SectionRef &Section : sections()) { - if (std::error_code E = Section.getName(SecName)) - return E; - if (SecName == SectionName) { + auto NameOrErr = Section.getName(); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); + + if (*NameOrErr == SectionName) { Result = getCOFFSection(Section); return std::error_code(); } diff --git a/llvm/lib/Object/Decompressor.cpp b/llvm/lib/Object/Decompressor.cpp index 53b8e84..11efd85 100644 --- a/llvm/lib/Object/Decompressor.cpp +++ b/llvm/lib/Object/Decompressor.cpp @@ -77,10 +77,15 @@ bool Decompressor::isGnuStyle(StringRef Name) { } bool Decompressor::isCompressed(const object::SectionRef &Section) { - StringRef Name; - if (Section.getName(Name)) - return false; - return Section.isCompressed() || isGnuStyle(Name); + if (Section.isCompressed()) + return true; + + Expected<StringRef> SecNameOrErr = Section.getName(); + if (SecNameOrErr) + return isGnuStyle(*SecNameOrErr); + + consumeError(SecNameOrErr.takeError()); + return false; } bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) { diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp index 9945018d..f3b0347 100644 --- a/llvm/lib/Object/ELFObjectFile.cpp +++ b/llvm/lib/Object/ELFObjectFile.cpp @@ -392,9 +392,13 @@ ELFObjectFileBase::getPltAddresses() const { return {}; Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None; for (const SectionRef &Section : sections()) { - StringRef Name; - if (Section.getName(Name)) + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) { + consumeError(NameOrErr.takeError()); continue; + } + StringRef Name = *NameOrErr; + if (Name == ".plt") Plt = Section; else if (Name == ".rela.plt" || Name == ".rel.plt") diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 3bfbfe06..179166dd 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -1986,13 +1986,12 @@ Expected<SectionRef> MachOObjectFile::getSection(unsigned SectionIndex) const { } Expected<SectionRef> MachOObjectFile::getSection(StringRef SectionName) const { - StringRef SecName; for (const SectionRef &Section : sections()) { - if (std::error_code E = Section.getName(SecName)) - return errorCodeToError(E); - if (SecName == SectionName) { + auto NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + if (*NameOrErr == SectionName) return Section; - } } return errorCodeToError(object_error::parse_failed); } @@ -3995,7 +3994,11 @@ BindRebaseSegInfo::BindRebaseSegInfo(const object::MachOObjectFile *Obj) { uint64_t CurSegAddress; for (const SectionRef &Section : Obj->sections()) { SectionInfo Info; - Section.getName(Info.SectionName); + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) + consumeError(NameOrErr.takeError()); + else + Info.SectionName = *NameOrErr; Info.Address = Section.getAddress(); Info.Size = Section.getSize(); Info.SegmentName = diff --git a/llvm/lib/Object/Object.cpp b/llvm/lib/Object/Object.cpp index d84798c..b518e2d 100644 --- a/llvm/lib/Object/Object.cpp +++ b/llvm/lib/Object/Object.cpp @@ -251,10 +251,10 @@ void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) { // SectionRef accessors const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) { - StringRef ret; - if (std::error_code ec = (*unwrap(SI))->getName(ret)) - report_fatal_error(ec.message()); - return ret.data(); + auto NameOrErr = (*unwrap(SI))->getName(); + if (!NameOrErr) + report_fatal_error(NameOrErr.takeError()); + return NameOrErr->data(); } uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) { diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp index e193e10f..e70bc31 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -666,11 +666,11 @@ static Expected<SectionRef> lookupSection(ObjectFile &OF, StringRef Name) { }; Name = stripSuffix(Name); - StringRef FoundName; for (const auto &Section : OF.sections()) { - if (auto EC = Section.getName(FoundName)) - return errorCodeToError(EC); - if (stripSuffix(FoundName) == Name) + Expected<StringRef> NameOrErr = Section.getName(); + if (!NameOrErr) + return NameOrErr.takeError(); + if (stripSuffix(*NameOrErr) == Name) return Section; } return make_error<CoverageMapError>(coveragemap_error::no_data_found); diff --git a/llvm/lib/XRay/InstrumentationMap.cpp b/llvm/lib/XRay/InstrumentationMap.cpp index 7de953d..7453613 100644 --- a/llvm/lib/XRay/InstrumentationMap.cpp +++ b/llvm/lib/XRay/InstrumentationMap.cpp @@ -67,10 +67,11 @@ loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile, StringRef Contents = ""; const auto &Sections = ObjFile.getBinary()->sections(); auto I = llvm::find_if(Sections, [&](object::SectionRef Section) { - StringRef Name = ""; - if (Section.getName(Name)) - return false; - return Name == "xray_instr_map"; + Expected<StringRef> NameOrErr = Section.getName(); + if (NameOrErr) + return *NameOrErr == "xray_instr_map"; + consumeError(NameOrErr.takeError()); + return false; }); if (I == Sections.end()) |