[lld] 1d31fb8 - [lld-macho] Have path-related functions return std::string, not StringRef

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 18 13:36:38 PDT 2021


Author: Jez Ng
Date: 2021-06-18T16:36:14-04:00
New Revision: 1d31fb8d122b1117cf20a9edc09812db8472e930

URL: https://github.com/llvm/llvm-project/commit/1d31fb8d122b1117cf20a9edc09812db8472e930
DIFF: https://github.com/llvm/llvm-project/commit/1d31fb8d122b1117cf20a9edc09812db8472e930.diff

LOG: [lld-macho] Have path-related functions return std::string, not StringRef

findLibrary() returned a StringRef while findFramework & other helper
functions returned std::strings. Standardize on std::string.

(I initially tried making the helper functions all return StringRefs,
but I realized we shouldn't return input StringRefs since their
lifetimes would not be obvious from the calling code.)

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index aa8049ebeb6b..726ca3e00fb1 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -78,9 +78,9 @@ static HeaderFileType getOutputType(const InputArgList &args) {
   }
 }
 
-static Optional<StringRef> findLibrary(StringRef name) {
+static Optional<std::string> findLibrary(StringRef name) {
   if (config->searchDylibsFirst) {
-    if (Optional<StringRef> path = findPathCombination(
+    if (Optional<std::string> path = findPathCombination(
             "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"}))
       return path;
     return findPathCombination("lib" + name, config->librarySearchPaths,
@@ -337,7 +337,7 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
 
 static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
                        bool isReexport, bool isExplicit, bool forceLoad) {
-  if (Optional<StringRef> path = findLibrary(name)) {
+  if (Optional<std::string> path = findLibrary(name)) {
     if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
             addFile(*path, forceLoad, isExplicit))) {
       if (isNeeded)

diff  --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index 148c309b52d5..9ae468cd2a36 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -58,14 +58,14 @@ DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr,
 
 // Search for all possible combinations of `{root}/{name}.{extension}`.
 // If \p extensions are not specified, then just search for `{root}/{name}`.
-llvm::Optional<llvm::StringRef>
+llvm::Optional<std::string>
 findPathCombination(const llvm::Twine &name,
                     const std::vector<llvm::StringRef> &roots,
                     ArrayRef<llvm::StringRef> extensions = {""});
 
 // If -syslibroot is specified, absolute paths to non-object files may be
 // rerooted.
-llvm::StringRef rerootPath(llvm::StringRef path);
+std::string rerootPath(llvm::StringRef path);
 
 llvm::Optional<InputFile *> loadArchiveMember(MemoryBufferRef, uint32_t modTime,
                                               StringRef archiveName,

diff  --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index 55582731aab9..f32027b2bcbf 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -246,7 +246,7 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
   return newFile;
 }
 
-Optional<StringRef>
+Optional<std::string>
 macho::findPathCombination(const Twine &name,
                            const std::vector<StringRef> &roots,
                            ArrayRef<StringRef> extensions) {
@@ -259,21 +259,21 @@ macho::findPathCombination(const Twine &name,
       bool exists = fs::exists(location);
       searchedDylib(location, exists);
       if (exists)
-        return saver.save(location.str());
+        return location.str();
     }
   }
   return {};
 }
 
-StringRef macho::rerootPath(StringRef path) {
+std::string macho::rerootPath(StringRef path) {
   if (!path::is_absolute(path, path::Style::posix) || path.endswith(".o"))
-    return path;
+    return std::string(path);
 
-  if (Optional<StringRef> rerootedPath =
+  if (Optional<std::string> rerootedPath =
           findPathCombination(path, config->systemLibraryRoots))
     return *rerootedPath;
 
-  return path;
+  return std::string(path);
 }
 
 Optional<InputFile *> macho::loadArchiveMember(MemoryBufferRef mb,


        


More information about the llvm-commits mailing list