[lld] 9da7aee - [COFF] Change most Optional to std::optional

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 16:39:45 PST 2022


Author: Fangrui Song
Date: 2022-11-27T16:39:40-08:00
New Revision: 9da7aee7c9ace687e643269adc2d5cf3f0d18785

URL: https://github.com/llvm/llvm-project/commit/9da7aee7c9ace687e643269adc2d5cf3f0d18785
DIFF: https://github.com/llvm/llvm-project/commit/9da7aee7c9ace687e643269adc2d5cf3f0d18785.diff

LOG: [COFF] Change most Optional to std::optional

Added: 
    

Modified: 
    lld/COFF/DebugTypes.cpp
    lld/COFF/Driver.cpp
    lld/COFF/Driver.h
    lld/COFF/DriverUtils.cpp
    lld/COFF/InputFiles.cpp
    lld/COFF/InputFiles.h
    lld/COFF/PDB.cpp
    lld/COFF/PDB.h
    lld/COFF/SymbolTable.cpp
    lld/COFF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/DebugTypes.cpp b/lld/COFF/DebugTypes.cpp
index 2b35b68032d02..09a0e59dc6697 100644
--- a/lld/COFF/DebugTypes.cpp
+++ b/lld/COFF/DebugTypes.cpp
@@ -284,14 +284,14 @@ static bool canUseDebugH(ArrayRef<uint8_t> debugH) {
          (debugH.size() % 8 == 0);
 }
 
-static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *file) {
+static std::optional<ArrayRef<uint8_t>> getDebugH(ObjFile *file) {
   SectionChunk *sec =
       SectionChunk::findByName(file->getDebugChunks(), ".debug$H");
   if (!sec)
     return llvm::None;
   ArrayRef<uint8_t> 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<ArrayRef<uint8_t>> debugH = getDebugH(file)) {
+  if (std::optional<ArrayRef<uint8_t>> debugH = getDebugH(file)) {
     ghashes = getHashesFromDebugH(*debugH);
     ownedGHashes = false;
   } else {

diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 33ed55ad76630..84684b09d49f3 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<StringRef> path = findLib(arg->getValue()))
+      if (std::optional<StringRef> 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<sys::fs::UniqueID> getUniqueID(StringRef path) {
+static std::optional<sys::fs::UniqueID> 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<StringRef> LinkerDriver::findFile(StringRef filename) {
+std::optional<StringRef> LinkerDriver::findFile(StringRef filename) {
   StringRef path = doFindFile(filename);
 
-  if (Optional<sys::fs::UniqueID> id = getUniqueID(path)) {
+  if (std::optional<sys::fs::UniqueID> 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<StringRef> LinkerDriver::findLib(StringRef filename) {
+std::optional<StringRef> 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<sys::fs::UniqueID> id = getUniqueID(path))
+  if (std::optional<sys::fs::UniqueID> 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<std::string> getReproduceFile(const opt::InputArgList &args) {
+std::optional<std::string> getReproduceFile(const opt::InputArgList &args) {
   if (auto *arg = args.getLastArg(OPT_reproduce))
     return std::string(arg->getValue());
 
@@ -1374,7 +1373,7 @@ Optional<std::string> getReproduceFile(const opt::InputArgList &args) {
   if (auto *path = getenv("LLD_REPRODUCE"))
     return std::string(path);
 
-  return None;
+  return std::nullopt;
 }
 
 static std::unique_ptr<llvm::vfs::FileSystem>
@@ -1483,7 +1482,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
                                   " (use --error-limit=0 to see all errors)";
 
   // Handle /linkrepro and /reproduce.
-  if (Optional<std::string> path = getReproduceFile(args)) {
+  if (std::optional<std::string> path = getReproduceFile(args)) {
     Expected<std::unique_ptr<TarWriter>> errOrWriter =
         TarWriter::create(*path, sys::path::stem(*path));
 
@@ -1953,7 +1952,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   std::set<sys::fs::UniqueID> wholeArchives;
   for (auto *arg : args.filtered(OPT_wholearchive_file))
     if (std::optional<StringRef> path = doFindFile(arg->getValue()))
-      if (Optional<sys::fs::UniqueID> id = getUniqueID(*path))
+      if (std::optional<sys::fs::UniqueID> 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<const char *> argsArr) {
   auto isWholeArchive = [&](StringRef path) -> bool {
     if (args.hasArg(OPT_wholearchive_flag))
       return true;
-    if (Optional<sys::fs::UniqueID> id = getUniqueID(path))
+    if (std::optional<sys::fs::UniqueID> id = getUniqueID(path))
       return wholeArchives.count(*id);
     return false;
   };
@@ -1985,11 +1984,11 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
       inLib = true;
       break;
     case OPT_wholearchive_file:
-      if (Optional<StringRef> path = findFile(arg->getValue()))
+      if (std::optional<StringRef> path = findFile(arg->getValue()))
         enqueuePath(*path, true, inLib);
       break;
     case OPT_INPUT:
-      if (Optional<StringRef> path = findFile(arg->getValue()))
+      if (std::optional<StringRef> path = findFile(arg->getValue()))
         enqueuePath(*path, isWholeArchive(*path), inLib);
       break;
     default:
@@ -2015,7 +2014,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> 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<StringRef> path = findLib(arg->getValue()))
+    if (std::optional<StringRef> path = findLib(arg->getValue()))
       enqueuePath(*path, false, false);
   run();
   if (errorCount())

diff  --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h
index 3f6f98d1a0606..0c1ae3e013c58 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 <memory>
+#include <optional>
 #include <set>
 #include <vector>
 
@@ -34,7 +34,7 @@ extern std::unique_ptr<class LinkerDriver> 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 @@ class LinkerDriver {
 
 private:
   // Searches a file from search paths.
-  Optional<StringRef> findFile(StringRef filename);
-  Optional<StringRef> findLib(StringRef filename);
+  std::optional<StringRef> findFile(StringRef filename);
+  std::optional<StringRef> 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 3a0f0eb07eca0..1943d1b6aabba 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 10ff860c97b72..ce0f6b4661c9c 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<coff_aux_weak_external>()->TagIndex;
       weakAliases.emplace_back(symbols[i], tagIndex);
-    } else if (Optional<Symbol *> optSym =
+    } else if (std::optional<Symbol *> optSym =
                    createDefined(coffSym, comdatDefs, prevailingComdat)) {
       symbols[i] = *optSym;
       if (config->mingw && prevailingComdat)
@@ -582,7 +582,7 @@ void ObjFile::handleComdatSelection(
   }
 }
 
-Optional<Symbol *> ObjFile::createDefined(
+std::optional<Symbol *> ObjFile::createDefined(
     COFFSymbolRef sym,
     std::vector<const coff_aux_section_definition *> &comdatDefs,
     bool &prevailing) {
@@ -677,7 +677,7 @@ Optional<Symbol *> 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<std::string> 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::pair<StringRef, uint32_t>>
+std::optional<std::pair<StringRef, uint32_t>>
 ObjFile::getVariableLocation(StringRef var) {
   if (!dwarf) {
     dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
     if (!dwarf)
-      return None;
+      return std::nullopt;
   }
   if (config->machine == I386)
     var.consume_front("_");
   Optional<std::pair<std::string, unsigned>> 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<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
   if (!dwarf) {
     dwarf = make<DWARFCache>(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 2cabb54cb3863..6af44b09af355 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -192,7 +192,7 @@ class ObjFile : public InputFile {
   // 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 
diff erence indicates out-of-date objects.
-  llvm::Optional<uint32_t> pchSignature;
+  std::optional<uint32_t> pchSignature;
 
   // Whether this file was compiled with /hotpatch.
   bool hotPatchable = false;
@@ -206,7 +206,7 @@ class ObjFile : public InputFile {
   // The .debug$P or .debug$T section data if present. Empty otherwise.
   ArrayRef<uint8_t> debugTypes;
 
-  llvm::Optional<std::pair<StringRef, uint32_t>>
+  std::optional<std::pair<StringRef, uint32_t>>
   getVariableLocation(StringRef var);
 
   llvm::Optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
@@ -258,7 +258,7 @@ class ObjFile : public InputFile {
                         bool &prevailing, DefinedRegular *leader,
                         const llvm::object::coff_aux_section_definition *def);
 
-  llvm::Optional<Symbol *>
+  std::optional<Symbol *>
   createDefined(COFFSymbolRef sym,
                 std::vector<const llvm::object::coff_aux_section_definition *>
                     &comdatDefs,
@@ -319,7 +319,7 @@ class PDBInputFile : public InputFile {
                                           StringRef path, ObjFile *fromFile);
 
   // Record possible errors while opening the PDB file
-  llvm::Optional<Error> loadErr;
+  std::optional<Error> loadErr;
 
   // This is the actual interface to the PDB (if it was opened successfully)
   std::unique_ptr<llvm::pdb::NativeSession> session;

diff  --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp
index 4251549ecc313..5d14e1e154ec1 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::pair<StringRef, uint32_t>>
+std::optional<std::pair<StringRef, uint32_t>>
 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<uint32_t> nameIndex;
   std::optional<uint32_t> 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 9fd41fc5cdc24..991805cc95b38 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 <optional>
 
 namespace llvm::codeview {
 union DebugInfo;
@@ -27,7 +27,7 @@ class COFFLinkerContext;
 void createPDB(COFFLinkerContext &ctx, llvm::ArrayRef<uint8_t> sectionTable,
                llvm::codeview::DebugInfo *buildId);
 
-llvm::Optional<std::pair<llvm::StringRef, uint32_t>>
+std::optional<std::pair<llvm::StringRef, uint32_t>>
 getFileLineCodeView(const SectionChunk *c, uint32_t addr);
 
 } // namespace coff

diff  --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index 95f7b4e15b88c..dd60b519237e4 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -125,22 +125,22 @@ static std::vector<std::string> getSymbolLocations(BitcodeFile *file) {
   return {res};
 }
 
-static Optional<std::pair<StringRef, uint32_t>>
+static std::optional<std::pair<StringRef, uint32_t>>
 getFileLineDwarf(const SectionChunk *c, uint32_t addr) {
   Optional<DILineInfo> 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<std::pair<StringRef, uint32_t>>
+static std::optional<std::pair<StringRef, uint32_t>>
 getFileLine(const SectionChunk *c, uint32_t addr) {
   // MinGW can optionally use codeview, even if the default is dwarf.
-  Optional<std::pair<StringRef, uint32_t>> fileLine =
+  std::optional<std::pair<StringRef, uint32_t>> 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<std::pair<StringRef, uint32_t>> fileLine =
+      std::optional<std::pair<StringRef, uint32_t>> 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<std::pair<StringRef, uint32_t>> fileLine;
+  std::optional<std::pair<StringRef, uint32_t>> fileLine;
   if (sc)
     fileLine = getFileLine(sc, offset);
   if (!fileLine)

diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 926edb71460ce..5e57763e00427 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -240,7 +240,7 @@ class Writer {
   PartialSection *createPartialSection(StringRef name, uint32_t outChars);
   PartialSection *findPartialSection(StringRef name, uint32_t outChars);
 
-  llvm::Optional<coff_symbol16> createSymbol(Defined *d);
+  std::optional<coff_symbol16> 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<coff_symbol16> Writer::createSymbol(Defined *def) {
+std::optional<coff_symbol16> Writer::createSymbol(Defined *def) {
   coff_symbol16 sym;
   switch (def->kind()) {
   case Symbol::DefinedAbsoluteKind: {
@@ -1156,10 +1156,10 @@ Optional<coff_symbol16> 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<coff_symbol16> 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<coff_symbol16> sym = createSymbol(d))
+        if (std::optional<coff_symbol16> sym = createSymbol(d))
           outputSymtab.push_back(*sym);
 
         if (auto *dthunk = dyn_cast<DefinedImportThunk>(d)) {
           if (!dthunk->wrappedSym->writtenToSymtab) {
             dthunk->wrappedSym->writtenToSymtab = true;
-            if (Optional<coff_symbol16> sym = createSymbol(dthunk->wrappedSym))
+            if (std::optional<coff_symbol16> sym =
+                    createSymbol(dthunk->wrappedSym))
               outputSymtab.push_back(*sym);
           }
         }


        


More information about the llvm-commits mailing list