[Bitcode(Reader|Writer)] Convert Optional to std::optional

This commit is contained in:
Krzysztof Parzyszek 2022-12-07 10:04:25 -08:00
parent a0568eabaf
commit 49e75ebd85
13 changed files with 64 additions and 58 deletions

View File

@ -8,7 +8,6 @@
#include "BitcodeReader.h"
#include "llvm/ADT/IndexedMap.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
@ -795,7 +794,7 @@ llvm::Error ClangDocBitcodeReader::validateStream() {
}
llvm::Error ClangDocBitcodeReader::readBlockInfoBlock() {
Expected<Optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
Expected<std::optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
Stream.ReadBlockInfoBlock();
if (!MaybeBlockInfo)
return MaybeBlockInfo.takeError();

View File

@ -18,10 +18,10 @@
#include "BitcodeWriter.h"
#include "Representation.h"
#include "clang/AST/AST.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Bitstream/BitstreamReader.h"
#include "llvm/Support/Error.h"
#include <optional>
namespace clang {
namespace doc {
@ -65,7 +65,7 @@ private:
llvm::Expected<std::unique_ptr<Info>> readBlockToInfo(unsigned ID);
llvm::BitstreamCursor &Stream;
Optional<llvm::BitstreamBlockInfo> BlockInfo;
std::optional<llvm::BitstreamBlockInfo> BlockInfo;
FieldId CurrentReferenceField;
};

View File

@ -10,7 +10,6 @@
#include "clang/Basic/FileManager.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Frontend/SerializedDiagnostics.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Bitstream/BitCodes.h"
@ -20,6 +19,7 @@
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/ManagedStatic.h"
#include <cstdint>
#include <optional>
#include <system_error>
using namespace clang;
@ -35,7 +35,7 @@ std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) {
return SDError::CouldNotLoad;
llvm::BitstreamCursor Stream(**Buffer);
Optional<llvm::BitstreamBlockInfo> BlockInfo;
std::optional<llvm::BitstreamBlockInfo> BlockInfo;
if (Stream.AtEndOfStream())
return SDError::InvalidSignature;
@ -73,7 +73,7 @@ std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) {
switch (MaybeSubBlockID.get()) {
case llvm::bitc::BLOCKINFO_BLOCK_ID: {
Expected<Optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
Expected<std::optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
Stream.ReadBlockInfoBlock();
if (!MaybeBlockInfo) {
// FIXME this drops the error on the floor.

View File

@ -14,11 +14,11 @@
#define LLVM_BITCODE_BITCODEANALYZER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Bitstream/BitstreamReader.h"
#include "llvm/Support/Error.h"
#include <map>
#include <optional>
#include <vector>
namespace llvm {
@ -53,7 +53,7 @@ class BitcodeAnalyzer {
BitstreamCursor Stream;
BitstreamBlockInfo BlockInfo;
CurStreamTypeType CurStreamType;
Optional<BitstreamCursor> BlockInfoStream;
std::optional<BitstreamCursor> BlockInfoStream;
unsigned NumTopBlocks = 0;
struct PerRecordStats {
@ -86,18 +86,19 @@ class BitcodeAnalyzer {
public:
BitcodeAnalyzer(StringRef Buffer,
Optional<StringRef> BlockInfoBuffer = std::nullopt);
std::optional<StringRef> BlockInfoBuffer = std::nullopt);
/// Analyze the bitcode file.
Error analyze(Optional<BCDumpOptions> O = std::nullopt,
Optional<StringRef> CheckHash = std::nullopt);
Error analyze(std::optional<BCDumpOptions> O = std::nullopt,
std::optional<StringRef> CheckHash = std::nullopt);
/// Print stats about the bitcode file.
void printStats(BCDumpOptions O, Optional<StringRef> Filename = std::nullopt);
void printStats(BCDumpOptions O,
std::optional<StringRef> Filename = std::nullopt);
private:
/// Read a block, updating statistics, etc.
Error parseBlock(unsigned BlockID, unsigned IndentLevel,
Optional<BCDumpOptions> O = std::nullopt,
Optional<StringRef> CheckHash = std::nullopt);
std::optional<BCDumpOptions> O = std::nullopt,
std::optional<StringRef> CheckHash = std::nullopt);
Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
StringRef Blob, raw_ostream &OS);

View File

@ -26,6 +26,7 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
@ -564,7 +565,7 @@ public:
///
/// \param ReadBlockInfoNames Whether to read block/record name information in
/// the BlockInfo block. Only llvm-bcanalyzer uses this.
Expected<Optional<BitstreamBlockInfo>>
Expected<std::optional<BitstreamBlockInfo>>
ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
/// Set the block info to be used by this BitstreamCursor to interpret

View File

@ -15,7 +15,6 @@
#define LLVM_BITSTREAM_BITSTREAMWRITER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Bitstream/BitCodes.h"
@ -23,6 +22,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <optional>
#include <vector>
namespace llvm {
@ -370,7 +370,7 @@ private:
/// the code.
template <typename uintty>
void EmitRecordWithAbbrevImpl(unsigned Abbrev, ArrayRef<uintty> Vals,
StringRef Blob, Optional<unsigned> Code) {
StringRef Blob, std::optional<unsigned> Code) {
const char *BlobData = Blob.data();
unsigned BlobLen = (unsigned) Blob.size();
unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;

View File

@ -13,6 +13,7 @@
#include "llvm/Bitstream/BitstreamReader.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/SHA1.h"
#include <optional>
using namespace llvm;
@ -21,9 +22,9 @@ static Error reportError(StringRef Message) {
}
/// Return a symbolic block name if known, otherwise return null.
static Optional<const char *> GetBlockName(unsigned BlockID,
const BitstreamBlockInfo &BlockInfo,
CurStreamTypeType CurStreamType) {
static std::optional<const char *>
GetBlockName(unsigned BlockID, const BitstreamBlockInfo &BlockInfo,
CurStreamTypeType CurStreamType) {
// Standard blocks for all bitcode files.
if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
@ -84,9 +85,10 @@ static Optional<const char *> GetBlockName(unsigned BlockID,
}
/// Return a symbolic code name if known, otherwise return null.
static Optional<const char *> GetCodeName(unsigned CodeID, unsigned BlockID,
const BitstreamBlockInfo &BlockInfo,
CurStreamTypeType CurStreamType) {
static std::optional<const char *>
GetCodeName(unsigned CodeID, unsigned BlockID,
const BitstreamBlockInfo &BlockInfo,
CurStreamTypeType CurStreamType) {
// Standard blocks for all bitcode files.
if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
@ -473,7 +475,7 @@ static Expected<CurStreamTypeType> ReadSignature(BitstreamCursor &Stream) {
return UnknownBitstream;
}
static Expected<CurStreamTypeType> analyzeHeader(Optional<BCDumpOptions> O,
static Expected<CurStreamTypeType> analyzeHeader(std::optional<BCDumpOptions> O,
BitstreamCursor &Stream) {
ArrayRef<uint8_t> Bytes = Stream.getBitcodeBytes();
const unsigned char *BufPtr = (const unsigned char *)Bytes.data();
@ -553,14 +555,14 @@ Error BitcodeAnalyzer::decodeMetadataStringsBlob(StringRef Indent,
}
BitcodeAnalyzer::BitcodeAnalyzer(StringRef Buffer,
Optional<StringRef> BlockInfoBuffer)
std::optional<StringRef> BlockInfoBuffer)
: Stream(Buffer) {
if (BlockInfoBuffer)
BlockInfoStream.emplace(*BlockInfoBuffer);
}
Error BitcodeAnalyzer::analyze(Optional<BCDumpOptions> O,
Optional<StringRef> CheckHash) {
Error BitcodeAnalyzer::analyze(std::optional<BCDumpOptions> O,
std::optional<StringRef> CheckHash) {
if (Error E = analyzeHeader(O, Stream).moveInto(CurStreamType))
return E;
@ -584,7 +586,7 @@ Error BitcodeAnalyzer::analyze(Optional<BCDumpOptions> O,
if (!MaybeBlockID)
return MaybeBlockID.takeError();
if (MaybeBlockID.get() == bitc::BLOCKINFO_BLOCK_ID) {
Optional<BitstreamBlockInfo> NewBlockInfo;
std::optional<BitstreamBlockInfo> NewBlockInfo;
if (Error E =
BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
.moveInto(NewBlockInfo))
@ -621,7 +623,7 @@ Error BitcodeAnalyzer::analyze(Optional<BCDumpOptions> O,
}
void BitcodeAnalyzer::printStats(BCDumpOptions O,
Optional<StringRef> Filename) {
std::optional<StringRef> Filename) {
uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
// Print a summary of the read file.
O.OS << "Summary ";
@ -655,7 +657,7 @@ void BitcodeAnalyzer::printStats(BCDumpOptions O,
O.OS << "Per-block Summary:\n";
for (const auto &Stat : BlockIDStats) {
O.OS << " Block ID #" << Stat.first;
if (Optional<const char *> BlockName =
if (std::optional<const char *> BlockName =
GetBlockName(Stat.first, BlockInfo, CurStreamType))
O.OS << " (" << *BlockName << ")";
O.OS << ":\n";
@ -718,7 +720,7 @@ void BitcodeAnalyzer::printStats(BCDumpOptions O,
O.OS << " ";
O.OS << " ";
if (Optional<const char *> CodeName = GetCodeName(
if (std::optional<const char *> CodeName = GetCodeName(
FreqPair.second, Stat.first, BlockInfo, CurStreamType))
O.OS << *CodeName << "\n";
else
@ -730,8 +732,8 @@ void BitcodeAnalyzer::printStats(BCDumpOptions O,
}
Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
Optional<BCDumpOptions> O,
Optional<StringRef> CheckHash) {
std::optional<BCDumpOptions> O,
std::optional<StringRef> CheckHash) {
std::string Indent(IndentLevel * 2, ' ');
uint64_t BlockBitStart = Stream.GetCurrentBitNo();
@ -745,7 +747,7 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
if (O && !O->DumpBlockinfo)
O->OS << Indent << "<BLOCKINFO_BLOCK/>\n";
Optional<BitstreamBlockInfo> NewBlockInfo;
std::optional<BitstreamBlockInfo> NewBlockInfo;
if (Error E = Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
.moveInto(NewBlockInfo))
return E;
@ -766,7 +768,7 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
// Keep it for later, when we see a MODULE_HASH record
uint64_t BlockEntryPos = Stream.getCurrentByteNo();
Optional<const char *> BlockName;
std::optional<const char *> BlockName;
if (DumpRecords) {
O->OS << Indent << "<";
if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
@ -860,7 +862,7 @@ Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
if (DumpRecords) {
O->OS << Indent << " <";
Optional<const char *> CodeName =
std::optional<const char *> CodeName =
GetCodeName(Code, BlockID, BlockInfo, CurStreamType);
if (CodeName)
O->OS << *CodeName;

View File

@ -13,7 +13,6 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
@ -3717,11 +3716,11 @@ Error BitcodeReader::rememberAndSkipFunctionBodies() {
}
Error BitcodeReaderBase::readBlockInfo() {
Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
Stream.ReadBlockInfoBlock();
if (!MaybeNewBlockInfo)
return MaybeNewBlockInfo.takeError();
Optional<BitstreamBlockInfo> NewBlockInfo =
std::optional<BitstreamBlockInfo> NewBlockInfo =
std::move(MaybeNewBlockInfo.get());
if (!NewBlockInfo)
return error("Malformed block");

View File

@ -17,7 +17,6 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
@ -78,6 +77,7 @@
#include <iterator>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
@ -525,7 +525,7 @@ private:
void writeModStrings();
void writeCombinedGlobalValueSummary();
Optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
std::optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
auto VMI = GUIDToValueIdMap.find(ValGUID);
if (VMI == GUIDToValueIdMap.end())
return std::nullopt;
@ -3811,7 +3811,7 @@ static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
Record.push_back(Arg.Calls.size());
for (auto &Call : Arg.Calls) {
Record.push_back(Call.ParamNo);
Optional<unsigned> ValueID = GetValueID(Call.Callee);
std::optional<unsigned> ValueID = GetValueID(Call.Callee);
if (!ValueID) {
// If ValueID is unknown we can't drop just this call, we must drop
// entire parameter.
@ -3975,7 +3975,7 @@ void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
FunctionSummary *FS = cast<FunctionSummary>(Summary);
writeFunctionTypeMetadataRecords(
Stream, FS, [&](const ValueInfo &VI) -> Optional<unsigned> {
Stream, FS, [&](const ValueInfo &VI) -> std::optional<unsigned> {
return {VE.getValueID(VI.getValue())};
});
@ -4429,7 +4429,7 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
return;
}
auto GetValueId = [&](const ValueInfo &VI) -> Optional<unsigned> {
auto GetValueId = [&](const ValueInfo &VI) -> std::optional<unsigned> {
if (!VI)
return std::nullopt;
return getValueId(VI.getGUID());
@ -4443,7 +4443,7 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
Stream, FS, CallsiteAbbrev, AllocAbbrev,
/*PerModule*/ false,
/*GetValueId*/ [&](const ValueInfo &VI) -> unsigned {
Optional<unsigned> ValueID = GetValueId(VI);
std::optional<unsigned> ValueID = GetValueId(VI);
// This can happen in shared index files for distributed ThinLTO if
// the callee function summary is not included. Record 0 which we
// will have to deal with conservatively when doing any kind of
@ -4499,7 +4499,7 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
for (auto &EI : FS->calls()) {
// If this GUID doesn't have a value id, it doesn't have a function
// summary and we don't need to record any calls to it.
Optional<unsigned> CallValueId = GetValueId(EI.first);
std::optional<unsigned> CallValueId = GetValueId(EI.first);
if (!CallValueId)
continue;
NameVals.push_back(*CallValueId);

View File

@ -9,6 +9,7 @@
#include "llvm/Bitstream/BitstreamReader.h"
#include "llvm/ADT/StringRef.h"
#include <cassert>
#include <optional>
#include <string>
using namespace llvm;
@ -417,7 +418,7 @@ Error BitstreamCursor::ReadAbbrevRecord() {
return Error::success();
}
Expected<Optional<BitstreamBlockInfo>>
Expected<std::optional<BitstreamBlockInfo>>
BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
if (llvm::Error Err = EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID))
return std::move(Err);

View File

@ -16,6 +16,7 @@
#include "llvm/Remarks/Remark.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include <optional>
using namespace llvm;
using namespace llvm::remarks;
@ -230,7 +231,7 @@ Error BitstreamParserHelper::parseBlockInfoBlock() {
"Error while parsing BLOCKINFO_BLOCK: expecting [ENTER_SUBBLOCK, "
"BLOCKINFO_BLOCK, ...].");
Expected<Optional<BitstreamBlockInfo>> MaybeBlockInfo =
Expected<std::optional<BitstreamBlockInfo>> MaybeBlockInfo =
Stream.ReadBlockInfoBlock();
if (!MaybeBlockInfo)
return MaybeBlockInfo.takeError();

View File

@ -27,7 +27,6 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Optional.h"
#include "llvm/Bitcode/BitcodeAnalyzer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
@ -36,6 +35,7 @@
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <optional>
using namespace llvm;
static cl::OptionCategory BCAnalyzerCategory("BC Analyzer Options");
@ -113,8 +113,9 @@ int main(int argc, char **argv) {
BlockInfoMB = ExitOnErr(openBitcodeFile(BlockInfoFilename));
BitcodeAnalyzer BA(MB->getBuffer(),
BlockInfoMB ? Optional<StringRef>(BlockInfoMB->getBuffer())
: std::nullopt);
BlockInfoMB
? std::optional<StringRef>(BlockInfoMB->getBuffer())
: std::nullopt);
BCDumpOptions O(outs());
O.Histogram = !NoHistogram;
@ -123,8 +124,8 @@ int main(int argc, char **argv) {
O.DumpBlockinfo = DumpBlockinfo;
ExitOnErr(BA.analyze(
Dump ? Optional<BCDumpOptions>(O) : Optional<BCDumpOptions>(),
CheckHash.empty() ? std::nullopt : Optional<StringRef>(CheckHash)));
Dump ? std::optional<BCDumpOptions>(O) : std::optional<BCDumpOptions>(),
CheckHash.empty() ? std::nullopt : std::optional<StringRef>(CheckHash)));
if (Dump)
outs() << "\n\n";

View File

@ -11,6 +11,7 @@
#include "llvm/Remarks/Remark.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <optional>
#include <string>
// We need to supprt Windows paths as well. In order to have paths with the same
@ -34,8 +35,8 @@ static void checkAnalyze(StringRef Input, StringRef Expected) {
}
static void check(remarks::SerializerMode Mode, const remarks::Remark &R,
StringRef ExpectedR, Optional<StringRef> ExpectedMeta,
Optional<remarks::StringTable> StrTab) {
StringRef ExpectedR, std::optional<StringRef> ExpectedMeta,
std::optional<remarks::StringTable> StrTab) {
// Emit the remark.
std::string InputBuf;
raw_string_ostream InputOS(InputBuf);
@ -67,14 +68,14 @@ static void check(remarks::SerializerMode Mode, const remarks::Remark &R,
static void check(const remarks::Remark &R, StringRef ExpectedR,
StringRef ExpectedMeta,
Optional<remarks::StringTable> StrTab = std::nullopt) {
std::optional<remarks::StringTable> StrTab = std::nullopt) {
return check(remarks::SerializerMode::Separate, R, ExpectedR, ExpectedMeta,
std::move(StrTab));
}
static void
checkStandalone(const remarks::Remark &R, StringRef ExpectedR,
Optional<remarks::StringTable> StrTab = std::nullopt) {
std::optional<remarks::StringTable> StrTab = std::nullopt) {
return check(remarks::SerializerMode::Standalone, R, ExpectedR,
/*ExpectedMeta=*/std::nullopt, std::move(StrTab));
}