From 026e797367d970247bf79951b82d8faa8f13b44e Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 27 Nov 2022 16:54:07 -0800 Subject: [PATCH] [lld-macho] Change most Optional to std::optional --- lld/MachO/Config.h | 2 +- lld/MachO/Driver.cpp | 26 +++++++++++++------------- lld/MachO/Driver.h | 6 +++--- lld/MachO/DriverUtils.cpp | 8 ++++---- lld/MachO/EhFrame.h | 4 ++-- lld/MachO/ExportTrie.cpp | 1 - lld/MachO/InputFiles.cpp | 16 +++++++++------- lld/MachO/InputFiles.h | 2 +- lld/MachO/LTO.cpp | 4 ++-- lld/MachO/SectionPriorities.cpp | 8 ++++---- lld/MachO/SectionPriorities.h | 2 +- 11 files changed, 40 insertions(+), 39 deletions(-) diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h index 2d5c2e1..1e8c768 100644 --- a/lld/MachO/Config.h +++ b/lld/MachO/Config.h @@ -175,7 +175,7 @@ struct Configuration { // they can't easily fix them. llvm::StringSet<> ignoreAutoLinkOptions; PlatformInfo platformInfo; - llvm::Optional secondaryPlatformInfo; + std::optional secondaryPlatformInfo; NamespaceKind namespaceKind = NamespaceKind::twolevel; UndefinedSymbolTreatment undefinedSymbolTreatment = UndefinedSymbolTreatment::error; diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp index f448634..50d045b 100644 --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -82,7 +82,7 @@ static HeaderFileType getOutputType(const InputArgList &args) { } static DenseMap resolvedLibraries; -static Optional findLibrary(StringRef name) { +static std::optional findLibrary(StringRef name) { CachedHashStringRef key(name); auto entry = resolvedLibraries.find(key); if (entry != resolvedLibraries.end()) @@ -90,7 +90,7 @@ static Optional findLibrary(StringRef name) { auto doFind = [&] { if (config->searchDylibsFirst) { - if (Optional path = findPathCombination( + if (std::optional path = findPathCombination( "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"})) return path; return findPathCombination("lib" + name, config->librarySearchPaths, @@ -100,7 +100,7 @@ static Optional findLibrary(StringRef name) { {".tbd", ".dylib", ".a"}); }; - Optional path = doFind(); + std::optional path = doFind(); if (path) resolvedLibraries[key] = *path; @@ -108,7 +108,7 @@ static Optional findLibrary(StringRef name) { } static DenseMap resolvedFrameworks; -static Optional findFramework(StringRef name) { +static std::optional findFramework(StringRef name) { CachedHashStringRef key(name); auto entry = resolvedFrameworks.find(key); if (entry != resolvedFrameworks.end()) @@ -134,7 +134,7 @@ static Optional findFramework(StringRef name) { // Suffix lookup failed, fall through to the no-suffix case. } - if (Optional path = resolveDylibPath(symlink.str())) + if (std::optional path = resolveDylibPath(symlink.str())) return resolvedFrameworks[key] = *path; } return {}; @@ -268,7 +268,7 @@ static InputFile *addFile(StringRef path, LoadType loadType, bool isLazy = false, bool isExplicit = true, bool isBundleLoader = false, bool isForceHidden = false) { - Optional buffer = readFile(path); + std::optional buffer = readFile(path); if (!buffer) return nullptr; MemoryBufferRef mbref = *buffer; @@ -309,7 +309,7 @@ static InputFile *addFile(StringRef path, LoadType loadType, path::filename(path).startswith("libswift"); if ((isCommandLineLoad && config->allLoad) || loadType == LoadType::CommandLineForce || isLCLinkerForceLoad) { - if (Optional buffer = readFile(path)) { + if (std::optional buffer = readFile(path)) { Error e = Error::success(); for (const object::Archive::Child &c : file->getArchive().children(e)) { StringRef reason; @@ -339,7 +339,7 @@ static InputFile *addFile(StringRef path, LoadType loadType, // TODO: no need to look for ObjC sections for a given archive member if // we already found that it contains an ObjC symbol. - if (Optional buffer = readFile(path)) { + if (std::optional buffer = readFile(path)) { Error e = Error::success(); for (const object::Archive::Child &c : file->getArchive().children(e)) { Expected mb = c.getMemoryBufferRef(); @@ -409,7 +409,7 @@ static InputFile *addFile(StringRef path, LoadType loadType, static void addLibrary(StringRef name, bool isNeeded, bool isWeak, bool isReexport, bool isHidden, bool isExplicit, LoadType loadType) { - if (Optional path = findLibrary(name)) { + if (std::optional path = findLibrary(name)) { if (auto *dylibFile = dyn_cast_or_null( addFile(*path, loadType, /*isLazy=*/false, isExplicit, /*isBundleLoader=*/false, isHidden))) { @@ -430,7 +430,7 @@ static void addLibrary(StringRef name, bool isNeeded, bool isWeak, static DenseSet loadedObjectFrameworks; static void addFramework(StringRef name, bool isNeeded, bool isWeak, bool isReexport, bool isExplicit, LoadType loadType) { - if (Optional path = findFramework(name)) { + if (std::optional path = findFramework(name)) { if (loadedObjectFrameworks.contains(*path)) return; @@ -496,7 +496,7 @@ void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) { } static void addFileList(StringRef path, bool isLazy) { - Optional buffer = readFile(path); + std::optional buffer = readFile(path); if (!buffer) return; MemoryBufferRef mbref = *buffer; @@ -1037,7 +1037,7 @@ bool SymbolPatterns::match(StringRef symbolName) const { static void parseSymbolPatternsFile(const Arg *arg, SymbolPatterns &symbolPatterns) { StringRef path = arg->getValue(); - Optional buffer = readFile(path); + std::optional buffer = readFile(path); if (!buffer) { error("Could not read symbol file: " + path); return; @@ -1820,7 +1820,7 @@ bool macho::link(ArrayRef argsArr, llvm::raw_ostream &stdoutOS, StringRef segName = arg->getValue(0); StringRef sectName = arg->getValue(1); StringRef fileName = arg->getValue(2); - Optional buffer = readFile(fileName); + std::optional buffer = readFile(fileName); if (buffer) inputFiles.insert(make(*buffer, segName, sectName)); } diff --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h index 7249fe3..d0f46e0 100644 --- a/lld/MachO/Driver.h +++ b/lld/MachO/Driver.h @@ -10,12 +10,12 @@ #define LLD_MACHO_DRIVER_H #include "lld/Common/LLVM.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/SetVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/BinaryFormat/MachO.h" #include "llvm/Option/OptTable.h" #include "llvm/Support/MemoryBuffer.h" +#include #include #include @@ -45,7 +45,7 @@ void parseLCLinkerOption(InputFile *, unsigned argc, StringRef data); std::string createResponseFile(const llvm::opt::InputArgList &args); // Check for both libfoo.dylib and libfoo.tbd (in that order). -llvm::Optional resolveDylibPath(llvm::StringRef path); +std::optional resolveDylibPath(llvm::StringRef path); DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr, bool isBundleLoader = false, @@ -54,7 +54,7 @@ void resetLoadedDylibs(); // Search for all possible combinations of `{root}/{name}.{extension}`. // If \p extensions are not specified, then just search for `{root}/{name}`. -llvm::Optional +std::optional findPathCombination(const llvm::Twine &name, const std::vector &roots, ArrayRef extensions = {""}); diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp index b91a435..e881d32 100644 --- a/lld/MachO/DriverUtils.cpp +++ b/lld/MachO/DriverUtils.cpp @@ -144,7 +144,7 @@ std::string macho::createResponseFile(const InputArgList &args) { os << "-o " << quote(path::filename(arg->getValue())) << "\n"; break; case OPT_filelist: - if (Optional buffer = readFile(arg->getValue())) + if (std::optional buffer = readFile(arg->getValue())) for (StringRef path : args::getLines(*buffer)) os << quote(rewriteInputPath(path)) << "\n"; break; @@ -184,7 +184,7 @@ static void searchedDylib(const Twine &path, bool found) { depTracker->logFileNotFound(path); } -Optional macho::resolveDylibPath(StringRef dylibPath) { +std::optional macho::resolveDylibPath(StringRef dylibPath) { // TODO: if a tbd and dylib are both present, we should check to make sure // they are consistent. SmallString<261> tbdPath = dylibPath; @@ -253,7 +253,7 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella, void macho::resetLoadedDylibs() { loadedDylibs.clear(); } -Optional +std::optional macho::findPathCombination(const Twine &name, const std::vector &roots, ArrayRef extensions) { @@ -276,7 +276,7 @@ StringRef macho::rerootPath(StringRef path) { if (!path::is_absolute(path, path::Style::posix) || path.endswith(".o")) return path; - if (Optional rerootedPath = + if (std::optional rerootedPath = findPathCombination(path, config->systemLibraryRoots)) return *rerootedPath; diff --git a/lld/MachO/EhFrame.h b/lld/MachO/EhFrame.h index 091a92e8..d5afab3 100644 --- a/lld/MachO/EhFrame.h +++ b/lld/MachO/EhFrame.h @@ -46,8 +46,8 @@ * 1. Length of the entry (4 or 12 bytes) * 2. CIE offset (4 bytes pcrel offset that points backwards to this FDE's CIE) * 3. Function address (pointer-sized pcrel offset) - * 4. (Optional) Augmentation data length - * 5. (Optional) LSDA address (pointer-sized pcrel offset) + * 4. (std::optional) Augmentation data length + * 5. (std::optional) LSDA address (pointer-sized pcrel offset) * 6. DWARF instructions (ignored by LLD) */ namespace lld::macho { diff --git a/lld/MachO/ExportTrie.cpp b/lld/MachO/ExportTrie.cpp index 810864f..3ca8d35 100644 --- a/lld/MachO/ExportTrie.cpp +++ b/lld/MachO/ExportTrie.cpp @@ -39,7 +39,6 @@ #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" -#include "llvm/ADT/Optional.h" #include "llvm/BinaryFormat/MachO.h" #include "llvm/Support/LEB128.h" #include diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp index 374e4f3..fafa57b 100644 --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -191,7 +191,7 @@ static bool checkCompatibility(const InputFile *input) { // would require altering many callers to track the state. DenseMap macho::cachedReads; // Open a given file path and return it as a memory-mapped file. -Optional macho::readFile(StringRef path) { +std::optional macho::readFile(StringRef path) { CachedHashStringRef key(path); auto entry = cachedReads.find(key); if (entry != cachedReads.end()) @@ -1498,7 +1498,7 @@ lld::DWARFCache *ObjFile::getDwarf() { } // The path can point to either a dylib or a .tbd file. static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) { - Optional mbref = readFile(path); + std::optional mbref = readFile(path); if (!mbref) { error("could not read dylib file at " + path); return nullptr; @@ -1528,10 +1528,11 @@ static DylibFile *findDylib(StringRef path, DylibFile *umbrella, for (StringRef dir : config->frameworkSearchPaths) { SmallString<128> candidate = dir; path::append(candidate, frameworkName); - if (Optional dylibPath = resolveDylibPath(candidate.str())) + if (std::optional dylibPath = + resolveDylibPath(candidate.str())) return loadDylib(*dylibPath, umbrella); } - } else if (Optional dylibPath = findPathCombination( + } else if (std::optional dylibPath = findPathCombination( stem, config->librarySearchPaths, {".tbd", ".dylib"})) return loadDylib(*dylibPath, umbrella); } @@ -1539,7 +1540,8 @@ static DylibFile *findDylib(StringRef path, DylibFile *umbrella, // 2. As absolute path. if (path::is_absolute(path, path::Style::posix)) for (StringRef root : config->systemLibraryRoots) - if (Optional dylibPath = resolveDylibPath((root + path).str())) + if (std::optional dylibPath = + resolveDylibPath((root + path).str())) return loadDylib(*dylibPath, umbrella); // 3. As relative path. @@ -1568,7 +1570,7 @@ static DylibFile *findDylib(StringRef path, DylibFile *umbrella, path::remove_filename(newPath); } path::append(newPath, rpath, path.drop_front(strlen("@rpath/"))); - if (Optional dylibPath = resolveDylibPath(newPath.str())) + if (std::optional dylibPath = resolveDylibPath(newPath.str())) return loadDylib(*dylibPath, umbrella); } } @@ -1587,7 +1589,7 @@ static DylibFile *findDylib(StringRef path, DylibFile *umbrella, } } - if (Optional dylibPath = resolveDylibPath(path)) + if (std::optional dylibPath = resolveDylibPath(path)) return loadDylib(*dylibPath, umbrella); return nullptr; diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h index b883bd0..5ddf1b4 100644 --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -313,7 +313,7 @@ private: extern llvm::SetVector inputFiles; extern llvm::DenseMap cachedReads; -llvm::Optional readFile(StringRef path); +std::optional readFile(StringRef path); void extract(InputFile &file, StringRef reason); diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp index b7bd1a1..cde59eb 100644 --- a/lld/MachO/LTO.cpp +++ b/lld/MachO/LTO.cpp @@ -57,7 +57,7 @@ static lto::Config createConfig() { // If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails, // or `originalPath` is not set, saves `buffer` to `path`. static void saveOrHardlinkBuffer(StringRef buffer, const Twine &path, - Optional originalPath) { + std::optional originalPath) { if (originalPath) { auto err = fs::create_hard_link(*originalPath, path); if (!err) @@ -168,7 +168,7 @@ std::vector BitcodeCompiler::compile() { // not use the cached MemoryBuffer directly to ensure dsymutil does not // race with the cache pruner. StringRef objBuf; - Optional cachePath = llvm::None; + std::optional cachePath = llvm::None; if (files[i]) { objBuf = files[i]->getBuffer(); cachePath = files[i]->getBufferIdentifier(); diff --git a/lld/MachO/SectionPriorities.cpp b/lld/MachO/SectionPriorities.cpp index e5b9829..d0ce754 100644 --- a/lld/MachO/SectionPriorities.cpp +++ b/lld/MachO/SectionPriorities.cpp @@ -22,7 +22,6 @@ #include "lld/Common/ErrorHandler.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/MapVector.h" -#include "llvm/ADT/Optional.h" #include "llvm/Support/Path.h" #include "llvm/Support/TimeProfiler.h" #include "llvm/Support/raw_ostream.h" @@ -250,7 +249,8 @@ DenseMap CallGraphSort::run() { return orderMap; } -Optional macho::PriorityBuilder::getSymbolPriority(const Defined *sym) { +std::optional +macho::PriorityBuilder::getSymbolPriority(const Defined *sym) { if (sym->isAbsolute()) return None; @@ -295,7 +295,7 @@ void macho::PriorityBuilder::extractCallGraphProfile() { void macho::PriorityBuilder::parseOrderFile(StringRef path) { assert(callGraphProfile.empty() && "Order file must be parsed before call graph profile is processed"); - Optional buffer = readFile(path); + std::optional buffer = readFile(path); if (!buffer) { error("Could not read order file at " + path); return; @@ -367,7 +367,7 @@ macho::PriorityBuilder::buildInputSectionPriorities() { return sectionPriorities; auto addSym = [&](const Defined *sym) { - Optional symbolPriority = getSymbolPriority(sym); + std::optional symbolPriority = getSymbolPriority(sym); if (!symbolPriority) return; size_t &priority = sectionPriorities[sym->isec]; diff --git a/lld/MachO/SectionPriorities.h b/lld/MachO/SectionPriorities.h index cfd4837..9906ea4 100644 --- a/lld/MachO/SectionPriorities.h +++ b/lld/MachO/SectionPriorities.h @@ -69,7 +69,7 @@ private: llvm::DenseMap objectFiles; }; - llvm::Optional getSymbolPriority(const Defined *sym); + std::optional getSymbolPriority(const Defined *sym); llvm::DenseMap priorities; llvm::MapVector callGraphProfile; }; -- 2.7.4