From 9da7aee7c9ace687e643269adc2d5cf3f0d18785 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 27 Nov 2022 16:39:40 -0800 Subject: [PATCH] [COFF] Change most Optional to std::optional --- lld/COFF/DebugTypes.cpp | 6 +++--- lld/COFF/Driver.cpp | 41 ++++++++++++++++++++-------------------- lld/COFF/Driver.h | 8 ++++---- lld/COFF/DriverUtils.cpp | 1 - lld/COFF/InputFiles.cpp | 16 ++++++++-------- lld/COFF/InputFiles.h | 8 ++++---- lld/COFF/PDB.cpp | 6 +++--- lld/COFF/PDB.h | 4 ++-- lld/COFF/SymbolTable.cpp | 14 +++++++------- lld/COFF/Writer.cpp | 15 ++++++++------- 10 files changed, 59 insertions(+), 60 deletions(-) diff --git a/lld/COFF/DebugTypes.cpp b/lld/COFF/DebugTypes.cpp index 2b35b68032d0..09a0e59dc669 100644 --- a/lld/COFF/DebugTypes.cpp +++ b/lld/COFF/DebugTypes.cpp @@ -284,14 +284,14 @@ static bool canUseDebugH(ArrayRef debugH) { (debugH.size() % 8 == 0); } -static Optional> getDebugH(ObjFile *file) { +static std::optional> getDebugH(ObjFile *file) { SectionChunk *sec = SectionChunk::findByName(file->getDebugChunks(), ".debug$H"); if (!sec) return llvm::None; ArrayRef contents = sec->getContents(); if (!canUseDebugH(contents)) - return None; + return std::nullopt; return contents; } @@ -579,7 +579,7 @@ void PrecompSource::registerMapping() { //===----------------------------------------------------------------------===// void TpiSource::loadGHashes() { - if (Optional> debugH = getDebugH(file)) { + if (std::optional> debugH = getDebugH(file)) { ghashes = getHashesFromDebugH(*debugH); ownedGHashes = false; } else { diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 33ed55ad7663..84684b09d49f 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -23,7 +23,6 @@ #include "lld/Common/Timer.h" #include "lld/Common/Version.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Triple.h" #include "llvm/BinaryFormat/Magic.h" @@ -394,7 +393,7 @@ void LinkerDriver::parseDirectives(InputFile *file) { parseAlternateName(arg->getValue()); break; case OPT_defaultlib: - if (Optional path = findLib(arg->getValue())) + if (std::optional path = findLib(arg->getValue())) enqueuePath(*path, false, false); break; case OPT_entry: @@ -475,22 +474,22 @@ StringRef LinkerDriver::doFindFile(StringRef filename) { return filename; } -static Optional getUniqueID(StringRef path) { +static std::optional getUniqueID(StringRef path) { sys::fs::UniqueID ret; if (sys::fs::getUniqueID(path, ret)) - return None; + return std::nullopt; return ret; } // Resolves a file path. This never returns the same path // (in that case, it returns None). -Optional LinkerDriver::findFile(StringRef filename) { +std::optional LinkerDriver::findFile(StringRef filename) { StringRef path = doFindFile(filename); - if (Optional id = getUniqueID(path)) { + if (std::optional id = getUniqueID(path)) { bool seen = !visitedFiles.insert(*id).second; if (seen) - return None; + return std::nullopt; } if (path.endswith_insensitive(".lib")) @@ -527,19 +526,19 @@ StringRef LinkerDriver::doFindLib(StringRef filename) { // Resolves a library path. /nodefaultlib options are taken into // consideration. This never returns the same path (in that case, // it returns None). -Optional LinkerDriver::findLib(StringRef filename) { +std::optional LinkerDriver::findLib(StringRef filename) { if (config->noDefaultLibAll) - return None; + return std::nullopt; if (!visitedLibs.insert(filename.lower()).second) - return None; + return std::nullopt; StringRef path = doFindLib(filename); if (config->noDefaultLibs.count(path.lower())) - return None; + return std::nullopt; - if (Optional id = getUniqueID(path)) + if (std::optional id = getUniqueID(path)) if (!visitedFiles.insert(*id).second) - return None; + return std::nullopt; return path; } @@ -1359,7 +1358,7 @@ void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &args) { // /linkrepro and /reproduce are very similar, but /linkrepro takes a directory // name while /reproduce takes a full path. We have /linkrepro for compatibility // with Microsoft link.exe. -Optional getReproduceFile(const opt::InputArgList &args) { +std::optional getReproduceFile(const opt::InputArgList &args) { if (auto *arg = args.getLastArg(OPT_reproduce)) return std::string(arg->getValue()); @@ -1374,7 +1373,7 @@ Optional getReproduceFile(const opt::InputArgList &args) { if (auto *path = getenv("LLD_REPRODUCE")) return std::string(path); - return None; + return std::nullopt; } static std::unique_ptr @@ -1483,7 +1482,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { " (use --error-limit=0 to see all errors)"; // Handle /linkrepro and /reproduce. - if (Optional path = getReproduceFile(args)) { + if (std::optional path = getReproduceFile(args)) { Expected> errOrWriter = TarWriter::create(*path, sys::path::stem(*path)); @@ -1953,7 +1952,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { std::set wholeArchives; for (auto *arg : args.filtered(OPT_wholearchive_file)) if (std::optional path = doFindFile(arg->getValue())) - if (Optional id = getUniqueID(*path)) + if (std::optional id = getUniqueID(*path)) wholeArchives.insert(*id); // A predicate returning true if a given path is an argument for @@ -1963,7 +1962,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { auto isWholeArchive = [&](StringRef path) -> bool { if (args.hasArg(OPT_wholearchive_flag)) return true; - if (Optional id = getUniqueID(path)) + if (std::optional id = getUniqueID(path)) return wholeArchives.count(*id); return false; }; @@ -1985,11 +1984,11 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { inLib = true; break; case OPT_wholearchive_file: - if (Optional path = findFile(arg->getValue())) + if (std::optional path = findFile(arg->getValue())) enqueuePath(*path, true, inLib); break; case OPT_INPUT: - if (Optional path = findFile(arg->getValue())) + if (std::optional path = findFile(arg->getValue())) enqueuePath(*path, isWholeArchive(*path), inLib); break; default: @@ -2015,7 +2014,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { // Process files specified as /defaultlib. These must be processed after // addWinSysRootLibSearchPaths(), which is why they are in a separate loop. for (auto *arg : args.filtered(OPT_defaultlib)) - if (Optional path = findLib(arg->getValue())) + if (std::optional path = findLib(arg->getValue())) enqueuePath(*path, false, false); run(); if (errorCount()) diff --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h index 3f6f98d1a060..0c1ae3e013c5 100644 --- a/lld/COFF/Driver.h +++ b/lld/COFF/Driver.h @@ -14,7 +14,6 @@ #include "SymbolTable.h" #include "lld/Common/LLVM.h" #include "lld/Common/Reproduce.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/Object/Archive.h" @@ -25,6 +24,7 @@ #include "llvm/Support/TarWriter.h" #include "llvm/WindowsDriver/MSVCPaths.h" #include +#include #include #include @@ -34,7 +34,7 @@ extern std::unique_ptr driver; using llvm::COFF::MachineTypes; using llvm::COFF::WindowsSubsystem; -using llvm::Optional; +using std::optional; class COFFOptTable : public llvm::opt::OptTable { public: @@ -104,8 +104,8 @@ public: private: // Searches a file from search paths. - Optional findFile(StringRef filename); - Optional findLib(StringRef filename); + std::optional findFile(StringRef filename); + std::optional findLib(StringRef filename); StringRef doFindFile(StringRef filename); StringRef doFindLib(StringRef filename); StringRef doFindLibMinGW(StringRef filename); diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp index 3a0f0eb07eca..1943d1b6aabb 100644 --- a/lld/COFF/DriverUtils.cpp +++ b/lld/COFF/DriverUtils.cpp @@ -17,7 +17,6 @@ #include "Symbols.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/BinaryFormat/COFF.h" diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index 10ff860c97b7..ce0f6b4661c9 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -397,7 +397,7 @@ void ObjFile::initializeSymbols() { symbols[i] = createUndefined(coffSym); uint32_t tagIndex = coffSym.getAux()->TagIndex; weakAliases.emplace_back(symbols[i], tagIndex); - } else if (Optional optSym = + } else if (std::optional optSym = createDefined(coffSym, comdatDefs, prevailingComdat)) { symbols[i] = *optSym; if (config->mingw && prevailingComdat) @@ -582,7 +582,7 @@ void ObjFile::handleComdatSelection( } } -Optional ObjFile::createDefined( +std::optional ObjFile::createDefined( COFFSymbolRef sym, std::vector &comdatDefs, bool &prevailing) { @@ -677,7 +677,7 @@ Optional ObjFile::createDefined( if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE) comdatDefs[sectionNumber] = def; } - return None; + return std::nullopt; } return createRegular(sym); @@ -852,7 +852,7 @@ static std::optional findPdbPath(StringRef pdbPath, std::string ret = getPdbBaseName(dependentFile, pdbPath); if (llvm::sys::fs::exists(ret)) return normalizePdbPath(ret); - return None; + return std::nullopt; } PDBInputFile::PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m) @@ -897,18 +897,18 @@ void PDBInputFile::parse() { // Used only for DWARF debug info, which is not common (except in MinGW // environments). This returns an optional pair of file name and line // number for where the variable was defined. -Optional> +std::optional> ObjFile::getVariableLocation(StringRef var) { if (!dwarf) { dwarf = make(DWARFContext::create(*getCOFFObj())); if (!dwarf) - return None; + return std::nullopt; } if (config->machine == I386) var.consume_front("_"); Optional> ret = dwarf->getVariableLoc(var); if (!ret) - return None; + return std::nullopt; return std::make_pair(saver().save(ret->first), ret->second); } @@ -919,7 +919,7 @@ Optional ObjFile::getDILineInfo(uint32_t offset, if (!dwarf) { dwarf = make(DWARFContext::create(*getCOFFObj())); if (!dwarf) - return None; + return std::nullopt; } return dwarf->getDILineInfo(offset, sectionIndex); diff --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h index 2cabb54cb386..6af44b09af35 100644 --- a/lld/COFF/InputFiles.h +++ b/lld/COFF/InputFiles.h @@ -192,7 +192,7 @@ public: // When using Microsoft precompiled headers, this is the PCH's key. // The same key is used by both the precompiled object, and objects using the // precompiled object. Any difference indicates out-of-date objects. - llvm::Optional pchSignature; + std::optional pchSignature; // Whether this file was compiled with /hotpatch. bool hotPatchable = false; @@ -206,7 +206,7 @@ public: // The .debug$P or .debug$T section data if present. Empty otherwise. ArrayRef debugTypes; - llvm::Optional> + std::optional> getVariableLocation(StringRef var); llvm::Optional getDILineInfo(uint32_t offset, @@ -258,7 +258,7 @@ private: bool &prevailing, DefinedRegular *leader, const llvm::object::coff_aux_section_definition *def); - llvm::Optional + std::optional createDefined(COFFSymbolRef sym, std::vector &comdatDefs, @@ -319,7 +319,7 @@ public: StringRef path, ObjFile *fromFile); // Record possible errors while opening the PDB file - llvm::Optional loadErr; + std::optional loadErr; // This is the actual interface to the PDB (if it was opened successfully) std::unique_ptr session; diff --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp index 4251549ecc31..5d14e1e154ec 100644 --- a/lld/COFF/PDB.cpp +++ b/lld/COFF/PDB.cpp @@ -1777,7 +1777,7 @@ static bool findLineTable(const SectionChunk *c, uint32_t addr, // Use CodeView line tables to resolve a file and line number for the given // offset into the given chunk and return them, or None if a line table was // not found. -Optional> +std::optional> lld::coff::getFileLineCodeView(const SectionChunk *c, uint32_t addr) { ExitOnError exitOnErr; @@ -1787,7 +1787,7 @@ lld::coff::getFileLineCodeView(const SectionChunk *c, uint32_t addr) { uint32_t offsetInLinetable; if (!findLineTable(c, addr, cvStrTab, checksums, lines, offsetInLinetable)) - return None; + return std::nullopt; std::optional nameIndex; std::optional lineNumber; @@ -1808,7 +1808,7 @@ lld::coff::getFileLineCodeView(const SectionChunk *c, uint32_t addr) { } } if (!nameIndex) - return None; + return std::nullopt; StringRef filename = exitOnErr(getFileName(cvStrTab, checksums, *nameIndex)); return std::make_pair(filename, *lineNumber); } diff --git a/lld/COFF/PDB.h b/lld/COFF/PDB.h index 9fd41fc5cdc2..991805cc95b3 100644 --- a/lld/COFF/PDB.h +++ b/lld/COFF/PDB.h @@ -10,8 +10,8 @@ #define LLD_COFF_PDB_H #include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" +#include namespace llvm::codeview { union DebugInfo; @@ -27,7 +27,7 @@ class COFFLinkerContext; void createPDB(COFFLinkerContext &ctx, llvm::ArrayRef sectionTable, llvm::codeview::DebugInfo *buildId); -llvm::Optional> +std::optional> getFileLineCodeView(const SectionChunk *c, uint32_t addr); } // namespace coff diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp index 95f7b4e15b88..dd60b519237e 100644 --- a/lld/COFF/SymbolTable.cpp +++ b/lld/COFF/SymbolTable.cpp @@ -125,22 +125,22 @@ static std::vector getSymbolLocations(BitcodeFile *file) { return {res}; } -static Optional> +static std::optional> getFileLineDwarf(const SectionChunk *c, uint32_t addr) { Optional optionalLineInfo = c->file->getDILineInfo(addr, c->getSectionNumber() - 1); if (!optionalLineInfo) - return None; + return std::nullopt; const DILineInfo &lineInfo = *optionalLineInfo; if (lineInfo.FileName == DILineInfo::BadString) - return None; + return std::nullopt; return std::make_pair(saver().save(lineInfo.FileName), lineInfo.Line); } -static Optional> +static std::optional> getFileLine(const SectionChunk *c, uint32_t addr) { // MinGW can optionally use codeview, even if the default is dwarf. - Optional> fileLine = + std::optional> fileLine = getFileLineCodeView(c, addr); // If codeview didn't yield any result, check dwarf in MinGW mode. if (!fileLine && config->mingw) @@ -174,7 +174,7 @@ getSymbolLocations(ObjFile *file, uint32_t symIndex, size_t maxStrings) { if (locations.size() >= maxStrings) continue; - Optional> fileLine = + std::optional> fileLine = getFileLine(sc, r.VirtualAddress); Symbol *sym = getSymbol(sc, r.VirtualAddress); if (fileLine) @@ -604,7 +604,7 @@ static std::string getSourceLocationBitcode(BitcodeFile *file) { static std::string getSourceLocationObj(ObjFile *file, SectionChunk *sc, uint32_t offset, StringRef name) { - Optional> fileLine; + std::optional> fileLine; if (sc) fileLine = getFileLine(sc, offset); if (!fileLine) diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp index 926edb71460c..5e57763e0042 100644 --- a/lld/COFF/Writer.cpp +++ b/lld/COFF/Writer.cpp @@ -240,7 +240,7 @@ private: PartialSection *createPartialSection(StringRef name, uint32_t outChars); PartialSection *findPartialSection(StringRef name, uint32_t outChars); - llvm::Optional createSymbol(Defined *d); + std::optional createSymbol(Defined *d); size_t addEntryToStringTable(StringRef str); OutputSection *findSection(StringRef name); @@ -1137,7 +1137,7 @@ size_t Writer::addEntryToStringTable(StringRef str) { return offsetOfEntry; } -Optional Writer::createSymbol(Defined *def) { +std::optional Writer::createSymbol(Defined *def) { coff_symbol16 sym; switch (def->kind()) { case Symbol::DefinedAbsoluteKind: { @@ -1156,10 +1156,10 @@ Optional Writer::createSymbol(Defined *def) { // like __ImageBase are outside of sections and thus cannot be represented. Chunk *c = def->getChunk(); if (!c) - return None; + return std::nullopt; OutputSection *os = ctx.getOutputSection(c); if (!os) - return None; + return std::nullopt; sym.Value = def->getRVA() - os->getRVA(); sym.SectionNumber = os->sectionIndex; @@ -1172,7 +1172,7 @@ Optional Writer::createSymbol(Defined *def) { // instead. Avoid emitting them to the symbol table, as they can confuse // debuggers. if (def->isRuntimePseudoReloc) - return None; + return std::nullopt; StringRef name = def->getName(); if (name.size() > COFF::NameSize) { @@ -1235,13 +1235,14 @@ void Writer::createSymbolAndStringTable() { continue; } - if (Optional sym = createSymbol(d)) + if (std::optional sym = createSymbol(d)) outputSymtab.push_back(*sym); if (auto *dthunk = dyn_cast(d)) { if (!dthunk->wrappedSym->writtenToSymtab) { dthunk->wrappedSym->writtenToSymtab = true; - if (Optional sym = createSymbol(dthunk->wrappedSym)) + if (std::optional sym = + createSymbol(dthunk->wrappedSym)) outputSymtab.push_back(*sym); } }