Skip to content

Commit fccabce

Browse files
author
git apple-llvm automerger
committed
Merge commit 'b12a864c2930' from llvm.org/main into next
2 parents fabd416 + b12a864 commit fccabce

File tree

3 files changed

+73
-97
lines changed

3 files changed

+73
-97
lines changed

llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,9 @@ Error BitcodeAnalyzer::decodeMetadataStringsBlob(StringRef Indent,
529529
if (R.AtEndOfStream())
530530
return reportError("bad length");
531531

532-
Expected<uint32_t> MaybeSize = R.ReadVBR(6);
533-
if (!MaybeSize)
534-
return MaybeSize.takeError();
535-
uint32_t Size = MaybeSize.get();
532+
uint32_t Size;
533+
if (Error E = R.ReadVBR(6).moveInto(Size))
534+
return E;
536535
if (Strings.size() < Size)
537536
return reportError("truncated chars");
538537

@@ -555,21 +554,17 @@ BitcodeAnalyzer::BitcodeAnalyzer(StringRef Buffer,
555554

556555
Error BitcodeAnalyzer::analyze(Optional<BCDumpOptions> O,
557556
Optional<StringRef> CheckHash) {
558-
Expected<CurStreamTypeType> MaybeType = analyzeHeader(O, Stream);
559-
if (!MaybeType)
560-
return MaybeType.takeError();
561-
else
562-
CurStreamType = *MaybeType;
557+
if (Error E = analyzeHeader(O, Stream).moveInto(CurStreamType))
558+
return E;
563559

564560
Stream.setBlockInfo(&BlockInfo);
565561

566562
// Read block info from BlockInfoStream, if specified.
567563
// The block info must be a top-level block.
568564
if (BlockInfoStream) {
569565
BitstreamCursor BlockInfoCursor(*BlockInfoStream);
570-
Expected<CurStreamTypeType> H = analyzeHeader(O, BlockInfoCursor);
571-
if (!H)
572-
return H.takeError();
566+
if (Error E = analyzeHeader(O, BlockInfoCursor).takeError())
567+
return E;
573568

574569
while (!BlockInfoCursor.AtEndOfStream()) {
575570
Expected<unsigned> MaybeCode = BlockInfoCursor.ReadCode();
@@ -582,12 +577,11 @@ Error BitcodeAnalyzer::analyze(Optional<BCDumpOptions> O,
582577
if (!MaybeBlockID)
583578
return MaybeBlockID.takeError();
584579
if (MaybeBlockID.get() == bitc::BLOCKINFO_BLOCK_ID) {
585-
Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
586-
BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
587-
if (!MaybeNewBlockInfo)
588-
return MaybeNewBlockInfo.takeError();
589-
Optional<BitstreamBlockInfo> NewBlockInfo =
590-
std::move(MaybeNewBlockInfo.get());
580+
Optional<BitstreamBlockInfo> NewBlockInfo;
581+
if (Error E =
582+
BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
583+
.moveInto(NewBlockInfo))
584+
return E;
591585
if (!NewBlockInfo)
592586
return reportError("Malformed BlockInfoBlock in block info file");
593587
BlockInfo = std::move(*NewBlockInfo);
@@ -746,12 +740,10 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
746740
if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
747741
if (O && !O->DumpBlockinfo)
748742
O->OS << Indent << "<BLOCKINFO_BLOCK/>\n";
749-
Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
750-
Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
751-
if (!MaybeNewBlockInfo)
752-
return MaybeNewBlockInfo.takeError();
753-
Optional<BitstreamBlockInfo> NewBlockInfo =
754-
std::move(MaybeNewBlockInfo.get());
743+
Optional<BitstreamBlockInfo> NewBlockInfo;
744+
if (Error E = Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
745+
.moveInto(NewBlockInfo))
746+
return E;
755747
if (!NewBlockInfo)
756748
return reportError("Malformed BlockInfoBlock");
757749
BlockInfo = std::move(*NewBlockInfo);
@@ -796,11 +788,10 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
796788

797789
uint64_t RecordStartBit = Stream.GetCurrentBitNo();
798790

799-
Expected<BitstreamEntry> MaybeEntry =
800-
Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
801-
if (!MaybeEntry)
802-
return MaybeEntry.takeError();
803-
BitstreamEntry Entry = MaybeEntry.get();
791+
BitstreamEntry Entry;
792+
if (Error E = Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs)
793+
.moveInto(Entry))
794+
return E;
804795

805796
switch (Entry.Kind) {
806797
case BitstreamEntry::Error:
@@ -847,10 +838,9 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
847838

848839
StringRef Blob;
849840
uint64_t CurrentRecordPos = Stream.GetCurrentBitNo();
850-
Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record, &Blob);
851-
if (!MaybeCode)
852-
return MaybeCode.takeError();
853-
unsigned Code = MaybeCode.get();
841+
unsigned Code;
842+
if (Error E = Stream.readRecord(Entry.ID, Record, &Blob).moveInto(Code))
843+
return E;
854844

855845
// Increment the # occurrences of this code.
856846
if (BlockStats.CodeFreq.size() <= Code)

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,9 @@ static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {
241241
return std::move(Err);
242242
continue;
243243
case BitstreamEntry::Record:
244-
if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
245-
continue;
246-
else
247-
return Skipped.takeError();
244+
if (Error E = Stream.skipRecord(Entry.ID).takeError())
245+
return std::move(E);
246+
continue;
248247
}
249248
}
250249
}
@@ -320,10 +319,9 @@ static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {
320319
continue;
321320

322321
case BitstreamEntry::Record:
323-
if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
324-
continue;
325-
else
326-
return Skipped.takeError();
322+
if (Error E = Stream.skipRecord(Entry.ID).takeError())
323+
return std::move(E);
324+
continue;
327325
}
328326
}
329327
}
@@ -6772,10 +6770,9 @@ llvm::getBitcodeFileContents(MemoryBufferRef Buffer) {
67726770
continue;
67736771
}
67746772
case BitstreamEntry::Record:
6775-
if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID))
6776-
continue;
6777-
else
6778-
return StreamFailed.takeError();
6773+
if (Error E = Stream.skipRecord(Entry.ID).takeError())
6774+
return std::move(E);
6775+
continue;
67796776
}
67806777
}
67816778
}
@@ -6798,12 +6795,9 @@ BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll,
67986795
if (IdentificationBit != -1ull) {
67996796
if (Error JumpFailed = Stream.JumpToBit(IdentificationBit))
68006797
return std::move(JumpFailed);
6801-
Expected<std::string> ProducerIdentificationOrErr =
6802-
readIdentificationBlock(Stream);
6803-
if (!ProducerIdentificationOrErr)
6804-
return ProducerIdentificationOrErr.takeError();
6805-
6806-
ProducerIdentification = *ProducerIdentificationOrErr;
6798+
if (Error E =
6799+
readIdentificationBlock(Stream).moveInto(ProducerIdentification))
6800+
return std::move(E);
68076801
}
68086802

68096803
if (Error JumpFailed = Stream.JumpToBit(ModuleBit))
@@ -6877,10 +6871,9 @@ static Expected<bool> getEnableSplitLTOUnitFlag(BitstreamCursor &Stream,
68776871
SmallVector<uint64_t, 64> Record;
68786872

68796873
while (true) {
6880-
Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
6881-
if (!MaybeEntry)
6882-
return MaybeEntry.takeError();
6883-
BitstreamEntry Entry = MaybeEntry.get();
6874+
BitstreamEntry Entry;
6875+
if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
6876+
return std::move(E);
68846877

68856878
switch (Entry.Kind) {
68866879
case BitstreamEntry::SubBlock: // Handled for us already.
@@ -6925,10 +6918,9 @@ Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() {
69256918
return std::move(Err);
69266919

69276920
while (true) {
6928-
Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6929-
if (!MaybeEntry)
6930-
return MaybeEntry.takeError();
6931-
llvm::BitstreamEntry Entry = MaybeEntry.get();
6921+
llvm::BitstreamEntry Entry;
6922+
if (Error E = Stream.advance().moveInto(Entry))
6923+
return std::move(E);
69326924

69336925
switch (Entry.Kind) {
69346926
case BitstreamEntry::Error:

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,12 @@ MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
697697
// Get the abbrevs, and preload record positions to make them lazy-loadable.
698698
while (true) {
699699
uint64_t SavedPos = IndexCursor.GetCurrentBitNo();
700-
Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks(
701-
BitstreamCursor::AF_DontPopBlockAtEnd);
702-
if (!MaybeEntry)
703-
return MaybeEntry.takeError();
704-
BitstreamEntry Entry = MaybeEntry.get();
700+
BitstreamEntry Entry;
701+
if (Error E =
702+
IndexCursor
703+
.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd)
704+
.moveInto(Entry))
705+
return std::move(E);
705706

706707
switch (Entry.Kind) {
707708
case BitstreamEntry::SubBlock: // Handled for us already.
@@ -714,10 +715,9 @@ MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
714715
// The interesting case.
715716
++NumMDRecordLoaded;
716717
uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
717-
Expected<unsigned> MaybeCode = IndexCursor.skipRecord(Entry.ID);
718-
if (!MaybeCode)
719-
return MaybeCode.takeError();
720-
unsigned Code = MaybeCode.get();
718+
unsigned Code;
719+
if (Error E = IndexCursor.skipRecord(Entry.ID).moveInto(Code))
720+
return std::move(E);
721721
switch (Code) {
722722
case bitc::METADATA_STRINGS: {
723723
// Rewind and parse the strings.
@@ -904,11 +904,12 @@ Expected<bool> MetadataLoader::MetadataLoaderImpl::loadGlobalDeclAttachments() {
904904
if (Error Err = TempCursor.JumpToBit(GlobalDeclAttachmentPos))
905905
return std::move(Err);
906906
while (true) {
907-
Expected<BitstreamEntry> MaybeEntry = TempCursor.advanceSkippingSubblocks(
908-
BitstreamCursor::AF_DontPopBlockAtEnd);
909-
if (!MaybeEntry)
910-
return MaybeEntry.takeError();
911-
BitstreamEntry Entry = MaybeEntry.get();
907+
BitstreamEntry Entry;
908+
if (Error E =
909+
TempCursor
910+
.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd)
911+
.moveInto(Entry))
912+
return std::move(E);
912913

913914
switch (Entry.Kind) {
914915
case BitstreamEntry::SubBlock: // Handled for us already.
@@ -1024,10 +1025,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
10241025

10251026
// Read all the records.
10261027
while (true) {
1027-
Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1028-
if (!MaybeEntry)
1029-
return MaybeEntry.takeError();
1030-
BitstreamEntry Entry = MaybeEntry.get();
1028+
BitstreamEntry Entry;
1029+
if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
1030+
return E;
10311031

10321032
switch (Entry.Kind) {
10331033
case BitstreamEntry::SubBlock: // Handled for us already.
@@ -1081,12 +1081,11 @@ void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
10811081
GlobalMetadataBitPosIndex[ID - MDStringRef.size()]))
10821082
report_fatal_error("lazyLoadOneMetadata failed jumping: " +
10831083
Twine(toString(std::move(Err))));
1084-
Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks();
1085-
if (!MaybeEntry)
1084+
BitstreamEntry Entry;
1085+
if (Error E = IndexCursor.advanceSkippingSubblocks().moveInto(Entry))
10861086
// FIXME this drops the error on the floor.
10871087
report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " +
1088-
Twine(toString(MaybeEntry.takeError())));
1089-
BitstreamEntry Entry = MaybeEntry.get();
1088+
Twine(toString(std::move(E))));
10901089
++NumMDRecordLoaded;
10911090
if (Expected<unsigned> MaybeCode =
10921091
IndexCursor.readRecord(Entry.ID, Record, &Blob)) {
@@ -1193,10 +1192,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
11931192
// Read name of the named metadata.
11941193
SmallString<8> Name(Record.begin(), Record.end());
11951194
Record.clear();
1196-
Expected<unsigned> MaybeCode = Stream.ReadCode();
1197-
if (!MaybeCode)
1198-
return MaybeCode.takeError();
1199-
Code = MaybeCode.get();
1195+
if (Error E = Stream.ReadCode().moveInto(Code))
1196+
return E;
12001197

12011198
++NumMDRecordLoaded;
12021199
if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) {
@@ -2151,10 +2148,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
21512148
if (R.AtEndOfStream())
21522149
return error("Invalid record: metadata strings bad length");
21532150

2154-
Expected<uint32_t> MaybeSize = R.ReadVBR(6);
2155-
if (!MaybeSize)
2156-
return MaybeSize.takeError();
2157-
uint32_t Size = MaybeSize.get();
2151+
uint32_t Size;
2152+
if (Error E = R.ReadVBR(6).moveInto(Size))
2153+
return E;
21582154
if (Strings.size() < Size)
21592155
return error("Invalid record: metadata strings truncated chars");
21602156

@@ -2191,10 +2187,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
21912187
PlaceholderQueue Placeholders;
21922188

21932189
while (true) {
2194-
Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2195-
if (!MaybeEntry)
2196-
return MaybeEntry.takeError();
2197-
BitstreamEntry Entry = MaybeEntry.get();
2190+
BitstreamEntry Entry;
2191+
if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
2192+
return E;
21982193

21992194
switch (Entry.Kind) {
22002195
case BitstreamEntry::SubBlock: // Handled for us already.
@@ -2295,10 +2290,9 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
22952290

22962291
// Read all the records.
22972292
while (true) {
2298-
Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2299-
if (!MaybeEntry)
2300-
return MaybeEntry.takeError();
2301-
BitstreamEntry Entry = MaybeEntry.get();
2293+
BitstreamEntry Entry;
2294+
if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
2295+
return E;
23022296

23032297
switch (Entry.Kind) {
23042298
case BitstreamEntry::SubBlock: // Handled for us already.

0 commit comments

Comments
 (0)