Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 7287acf

Browse files
committed
Recommit [Object] Change object::SectionRef::getContents() to return Expected<StringRef>
r360876 didn't fix 2 call sites in clang. Expected<ArrayRef<uint8_t>> may be better but use Expected<StringRef> for now. Follow-up of D61781. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360892 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5164641 commit 7287acf

File tree

25 files changed

+151
-138
lines changed

25 files changed

+151
-138
lines changed

include/llvm/Object/ObjectFile.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class SectionRef {
9898
uint64_t getAddress() const;
9999
uint64_t getIndex() const;
100100
uint64_t getSize() const;
101-
std::error_code getContents(StringRef &Result) const;
101+
Expected<StringRef> getContents() const;
102102

103103
/// Get the alignment of this section as the actual value (not log 2).
104104
uint64_t getAlignment() const;
@@ -454,13 +454,12 @@ inline uint64_t SectionRef::getSize() const {
454454
return OwningObject->getSectionSize(SectionPimpl);
455455
}
456456

457-
inline std::error_code SectionRef::getContents(StringRef &Result) const {
457+
inline Expected<StringRef> SectionRef::getContents() const {
458458
Expected<ArrayRef<uint8_t>> Res =
459459
OwningObject->getSectionContents(SectionPimpl);
460460
if (!Res)
461-
return errorToErrorCode(Res.takeError());
462-
Result = StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
463-
return std::error_code();
461+
return Res.takeError();
462+
return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
464463
}
465464

466465
inline uint64_t SectionRef::getAlignment() const {

lib/DebugInfo/DWARF/DWARFContext.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,14 @@ class DWARFObjInMemory final : public DWARFObject {
14101410
// Try to obtain an already relocated version of this section.
14111411
// Else use the unrelocated section from the object file. We'll have to
14121412
// apply relocations ourselves later.
1413-
if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1414-
Section.getContents(Data);
1413+
if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
1414+
Expected<StringRef> E = Section.getContents();
1415+
if (E)
1416+
Data = *E;
1417+
else
1418+
// maybeDecompress below will error.
1419+
consumeError(E.takeError());
1420+
}
14151421

14161422
if (auto Err = maybeDecompress(Section, Name, Data)) {
14171423
ErrorPolicy EP = HandleError(createError(

lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ SymbolizableObjectFile::create(object::ObjectFile *Obj,
5353
if (Obj->getArch() == Triple::ppc64) {
5454
for (section_iterator Section : Obj->sections()) {
5555
StringRef Name;
56-
StringRef Data;
5756
if (auto EC = Section->getName(Name))
5857
return EC;
5958
if (Name == ".opd") {
60-
if (auto EC = Section->getContents(Data))
61-
return EC;
62-
OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
59+
Expected<StringRef> E = Section->getContents();
60+
if (!E)
61+
return errorToErrorCode(E.takeError());
62+
OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
6363
Obj->getBytesInAddress()));
6464
OpdAddress = Section->getAddress();
6565
break;

lib/DebugInfo/Symbolize/Symbolize.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
221221
Section.getName(Name);
222222
Name = Name.substr(Name.find_first_not_of("._"));
223223
if (Name == "gnu_debuglink") {
224-
StringRef Data;
225-
Section.getContents(Data);
226-
DataExtractor DE(Data, Obj->isLittleEndian(), 0);
224+
Expected<StringRef> ContentsOrErr = Section.getContents();
225+
if (!ContentsOrErr) {
226+
consumeError(ContentsOrErr.takeError());
227+
return false;
228+
}
229+
DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
227230
uint32_t Offset = 0;
228231
if (const char *DebugNameStr = DE.getCStr(&Offset)) {
229232
// 4-byte align the offset.

lib/ExecutionEngine/JITLink/MachOAtomGraphBuilder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ Error MachOAtomGraphBuilder::parseSections() {
136136

137137
if (!SecRef.isVirtual()) {
138138
// If this section has content then record it.
139-
StringRef Content;
140-
if (auto EC = SecRef.getContents(Content))
141-
return errorCodeToError(EC);
142-
if (Content.size() != SecRef.getSize())
139+
Expected<StringRef> Content = SecRef.getContents();
140+
if (!Content)
141+
return Content.takeError();
142+
if (Content->size() != SecRef.getSize())
143143
return make_error<JITLinkError>("Section content size does not match "
144144
"declared size for " +
145145
Name);
146-
MachOSec.setContent(Content);
146+
MachOSec.setContent(*Content);
147147
} else {
148148
// If this is a zero-fill section then just record the size.
149149
MachOSec.setZeroFill(SecRef.getSize());

lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,10 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
792792
if (!IsVirtual && !IsZeroInit) {
793793
// In either case, set the location of the unrelocated section in memory,
794794
// since we still process relocations for it even if we're not applying them.
795-
if (auto EC = Section.getContents(data))
796-
return errorCodeToError(EC);
795+
if (Expected<StringRef> E = Section.getContents())
796+
data = *E;
797+
else
798+
return E.takeError();
797799
pData = data.data();
798800
}
799801

lib/Object/ELFObjectFile.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,13 @@ ELFObjectFileBase::getPltAddresses() const {
377377
}
378378
if (!Plt || !RelaPlt || !GotPlt)
379379
return {};
380-
StringRef PltContents;
381-
if (Plt->getContents(PltContents))
380+
Expected<StringRef> PltContents = Plt->getContents();
381+
if (!PltContents) {
382+
consumeError(PltContents.takeError());
382383
return {};
383-
ArrayRef<uint8_t> PltBytes((const uint8_t *)PltContents.data(),
384-
Plt->getSize());
385-
auto PltEntries = MIA->findPltEntries(Plt->getAddress(), PltBytes,
384+
}
385+
auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
386+
arrayRefFromStringRef(*PltContents),
386387
GotPlt->getAddress(), Triple);
387388
// Build a map from GOT entry virtual address to PLT entry virtual address.
388389
DenseMap<uint64_t, uint64_t> GotToPlt;

lib/Object/IRObjectFile.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ Expected<MemoryBufferRef>
7474
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
7575
for (const SectionRef &Sec : Obj.sections()) {
7676
if (Sec.isBitcode()) {
77-
StringRef SecContents;
78-
if (std::error_code EC = Sec.getContents(SecContents))
79-
return errorCodeToError(EC);
80-
if (SecContents.size() <= 1)
77+
Expected<StringRef> Contents = Sec.getContents();
78+
if (!Contents)
79+
return Contents.takeError();
80+
if (Contents->size() <= 1)
8181
return errorCodeToError(object_error::bitcode_section_not_found);
82-
return MemoryBufferRef(SecContents, Obj.getFileName());
82+
return MemoryBufferRef(*Contents, Obj.getFileName());
8383
}
8484
}
8585

lib/Object/Object.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
247247
}
248248

249249
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
250-
StringRef ret;
251-
if (std::error_code ec = (*unwrap(SI))->getContents(ret))
252-
report_fatal_error(ec.message());
253-
return ret.data();
250+
if (Expected<StringRef> E = (*unwrap(SI))->getContents())
251+
return E->data();
252+
else
253+
report_fatal_error(E.takeError());
254254
}
255255

256256
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {

lib/ProfileData/Coverage/CoverageMappingReader.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,10 @@ Expected<bool> RawCoverageMappingDummyChecker::isDummy() {
348348
}
349349

350350
Error InstrProfSymtab::create(SectionRef &Section) {
351-
if (auto EC = Section.getContents(Data))
352-
return errorCodeToError(EC);
351+
Expected<StringRef> DataOrErr = Section.getContents();
352+
if (!DataOrErr)
353+
return DataOrErr.takeError();
354+
Data = *DataOrErr;
353355
Address = Section.getAddress();
354356

355357
// If this is a linked PE/COFF file, then we have to skip over the null byte
@@ -687,8 +689,11 @@ static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer,
687689
return E;
688690

689691
// Get the contents of the given sections.
690-
if (auto EC = CoverageSection->getContents(CoverageMapping))
691-
return errorCodeToError(EC);
692+
if (Expected<StringRef> E = CoverageSection->getContents())
693+
CoverageMapping = *E;
694+
else
695+
return E.takeError();
696+
692697
if (Error E = ProfileNames.create(*NamesSection))
693698
return E;
694699

0 commit comments

Comments
 (0)