[lld] 8f89c05 - [lld-macho][nfc] Remove unnecessary use of Optional<T*>

Vy Nguyen via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 1 15:35:48 PDT 2021


Author: Vy Nguyen
Date: 2021-06-01T18:35:31-04:00
New Revision: 8f89c054afa115e4e45dbc76f7efa2e7f485d227

URL: https://github.com/llvm/llvm-project/commit/8f89c054afa115e4e45dbc76f7efa2e7f485d227
DIFF: https://github.com/llvm/llvm-project/commit/8f89c054afa115e4e45dbc76f7efa2e7f485d227.diff

LOG: [lld-macho][nfc] Remove unnecessary use of Optional<T*>

In all of these cases, the functions could simply return a nullptr instead of {}.
There is no case where Optional<nullptr> has a special meaning.

Differential Revision: https://reviews.llvm.org/D103489

Added: 
    

Modified: 
    lld/MachO/Driver.cpp
    lld/MachO/Driver.h
    lld/MachO/DriverUtils.cpp
    lld/MachO/InputFiles.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 07cd065f961e..62e758677620 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -293,9 +293,9 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
   case file_magic::macho_dynamically_linked_shared_lib:
   case file_magic::macho_dynamically_linked_shared_lib_stub:
   case file_magic::tapi_file:
-    if (Optional<DylibFile *> dylibFile = loadDylib(mbref)) {
-      (*dylibFile)->explicitlyLinked = true;
-      newFile = *dylibFile;
+    if (DylibFile * dylibFile = loadDylib(mbref)) {
+      dylibFile->explicitlyLinked = true;
+      newFile = dylibFile;
     }
     break;
   case file_magic::bitcode:
@@ -307,9 +307,8 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
     // as a bundle loader.
     if (!isBundleLoader)
       error(path + ": unhandled file type");
-    if (Optional<DylibFile *> dylibFile =
-            loadDylib(mbref, nullptr, isBundleLoader))
-      newFile = *dylibFile;
+    if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader))
+      newFile = dylibFile;
     break;
   default:
     error(path + ": unhandled file type");

diff  --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index 72f00cd1237a..df045c9c0f5d 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -53,9 +53,8 @@ std::string createResponseFile(const llvm::opt::InputArgList &args);
 // Check for both libfoo.dylib and libfoo.tbd (in that order).
 llvm::Optional<std::string> resolveDylibPath(llvm::StringRef path);
 
-llvm::Optional<DylibFile *> loadDylib(llvm::MemoryBufferRef mbref,
-                                      DylibFile *umbrella = nullptr,
-                                      bool isBundleLoader = false);
+DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr,
+                     bool isBundleLoader = false);
 
 // Search for all possible combinations of `{root}/{name}.{extension}`.
 // If \p extensions are not specified, then just search for `{root}/{name}`.

diff  --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index 93bb8f07b738..457c3c643ae2 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -197,9 +197,8 @@ Optional<std::string> macho::resolveDylibPath(StringRef path) {
 // especially if it's a commonly re-exported core library.
 static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
 
-Optional<DylibFile *> macho::loadDylib(MemoryBufferRef mbref,
-                                       DylibFile *umbrella,
-                                       bool isBundleLoader) {
+DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
+                            bool isBundleLoader) {
   CachedHashStringRef path(mbref.getBufferIdentifier());
   DylibFile *&file = loadedDylibs[path];
   if (file)
@@ -212,7 +211,7 @@ Optional<DylibFile *> macho::loadDylib(MemoryBufferRef mbref,
     if (!result) {
       error("could not load TAPI file at " + mbref.getBufferIdentifier() +
             ": " + toString(result.takeError()));
-      return {};
+      return nullptr;
     }
     file = make<DylibFile>(**result, umbrella, isBundleLoader);
 

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 52ad2eb19804..2e04bb2e4833 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -730,11 +730,11 @@ void ObjFile::parseDebugInfo() {
 }
 
 // The path can point to either a dylib or a .tbd file.
-static Optional<DylibFile *> loadDylib(StringRef path, DylibFile *umbrella) {
+static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {
   Optional<MemoryBufferRef> mbref = readFile(path);
   if (!mbref) {
     error("could not read dylib file at " + path);
-    return {};
+    return nullptr;
   }
   return loadDylib(*mbref, umbrella);
 }
@@ -748,9 +748,8 @@ static Optional<DylibFile *> loadDylib(StringRef path, DylibFile *umbrella) {
 //
 // Re-exports can either refer to on-disk files, or to documents within .tbd
 // files.
-static Optional<DylibFile *>
-findDylib(StringRef path, DylibFile *umbrella,
-          const InterfaceFile *currentTopLevelTapi) {
+DylibFile *findDylib(StringRef path, DylibFile *umbrella,
+                     const InterfaceFile *currentTopLevelTapi) {
   if (path::is_absolute(path, path::Style::posix))
     for (StringRef root : config->systemLibraryRoots)
       if (Optional<std::string> dylibPath =
@@ -771,7 +770,7 @@ findDylib(StringRef path, DylibFile *umbrella,
   if (Optional<std::string> dylibPath = resolveDylibPath(path))
     return loadDylib(*dylibPath, umbrella);
 
-  return {};
+  return nullptr;
 }
 
 // If a re-exported dylib is public (lives in /usr/lib or
@@ -796,12 +795,11 @@ static bool isImplicitlyLinked(StringRef path) {
 
 void loadReexport(StringRef path, DylibFile *umbrella,
                   const InterfaceFile *currentTopLevelTapi) {
-  Optional<DylibFile *> reexport =
-      findDylib(path, umbrella, currentTopLevelTapi);
+  DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);
   if (!reexport)
     error("unable to locate re-export with install name " + path);
   else if (isImplicitlyLinked(path))
-    inputFiles.insert(*reexport);
+    inputFiles.insert(reexport);
 }
 
 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
@@ -874,7 +872,7 @@ void DylibFile::parseLoadCommands(MemoryBufferRef mb, DylibFile *umbrella) {
       const auto *c = reinterpret_cast<const dylib_command *>(cmd);
       StringRef dylibPath =
           reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
-      Optional<DylibFile *> dylib = findDylib(dylibPath, umbrella, nullptr);
+      DylibFile *dylib = findDylib(dylibPath, umbrella, nullptr);
       if (!dylib)
         error(Twine("unable to locate library '") + dylibPath +
               "' loaded from '" + toString(this) + "' for -flat_namespace");


        


More information about the llvm-commits mailing list