[Lldb-commits] [lldb] r345853 - [FileSystem] Remove ResolveExecutableLocation() from FileSpec

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 1 10:09:22 PDT 2018


Author: jdevlieghere
Date: Thu Nov  1 10:09:22 2018
New Revision: 345853

URL: http://llvm.org/viewvc/llvm-project?rev=345853&view=rev
Log:
[FileSystem] Remove ResolveExecutableLocation() from FileSpec

This patch removes the ResolveExecutableLocation method from FileSpec
and updates its uses with calls to the FileSystem.

Differential revision: https://reviews.llvm.org/D53834

Modified:
    lldb/trunk/include/lldb/Host/FileSystem.h
    lldb/trunk/include/lldb/Utility/FileSpec.h
    lldb/trunk/source/API/SBFileSpec.cpp
    lldb/trunk/source/Host/common/FileSystem.cpp
    lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp
    lldb/trunk/source/Host/macosx/objcxx/Host.mm
    lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
    lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
    lldb/trunk/source/Target/ProcessLaunchInfo.cpp
    lldb/trunk/source/Utility/FileSpec.cpp

Modified: lldb/trunk/include/lldb/Host/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/FileSystem.h?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/FileSystem.h (original)
+++ lldb/trunk/include/lldb/Host/FileSystem.h Thu Nov  1 10:09:22 2018
@@ -92,6 +92,9 @@ public:
   void Resolve(FileSpec &file_spec);
   /// @}
 
+  /// Call into the Host to see if it can help find the file.
+  bool ResolveExecutableLocation(FileSpec &file_spec);
+
   enum EnumerateDirectoryResult {
     /// Enumerate next entry in the current directory.
     eEnumerateDirectoryResultNext,

Modified: lldb/trunk/include/lldb/Utility/FileSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/FileSpec.h?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/FileSpec.h (original)
+++ lldb/trunk/include/lldb/Utility/FileSpec.h Thu Nov  1 10:09:22 2018
@@ -281,21 +281,6 @@ public:
   bool Exists() const;
 
   //------------------------------------------------------------------
-  /// Expanded existence test.
-  ///
-  /// Call into the Host to see if it can help find the file (e.g. by
-  /// searching paths set in the environment, etc.).
-  ///
-  /// If found, sets the value of m_directory to the directory where the file
-  /// was found.
-  ///
-  /// @return
-  ///     \b true if was able to find the file using expanded search
-  ///     methods, \b false otherwise.
-  //------------------------------------------------------------------
-  bool ResolveExecutableLocation();
-
-  //------------------------------------------------------------------
   /// Canonicalize this file path (basically running the static
   /// FileSpec::Resolve method on it). Useful if you asked us not to resolve
   /// the file path when you set the file.

Modified: lldb/trunk/source/API/SBFileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBFileSpec.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/API/SBFileSpec.cpp (original)
+++ lldb/trunk/source/API/SBFileSpec.cpp Thu Nov  1 10:09:22 2018
@@ -12,6 +12,7 @@
 
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/API/SBStream.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PosixApi.h"
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
@@ -61,7 +62,7 @@ bool SBFileSpec::Exists() const {
 }
 
 bool SBFileSpec::ResolveExecutableLocation() {
-  return m_opaque_ap->ResolveExecutableLocation();
+  return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_ap);
 }
 
 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,

Modified: lldb/trunk/source/Host/common/FileSystem.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSystem.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/FileSystem.cpp (original)
+++ lldb/trunk/source/Host/common/FileSystem.cpp Thu Nov  1 10:09:22 2018
@@ -13,6 +13,8 @@
 #include "lldb/Utility/TildeExpressionResolver.h"
 
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
 #include "llvm/Support/Threading.h"
 
 #include <algorithm>
@@ -178,3 +180,36 @@ void FileSystem::Resolve(FileSpec &file_
   // Update the FileSpec with the resolved path.
   file_spec.SetPath(path);
 }
+
+bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
+  // If the directory is set there's nothing to do.
+  const ConstString &directory = file_spec.GetDirectory();
+  if (directory)
+    return false;
+
+  // We cannot look for a file if there's no file name.
+  const ConstString &filename = file_spec.GetFilename();
+  if (!filename)
+    return false;
+
+  // Search for the file on the host.
+  const std::string filename_str(filename.GetCString());
+  llvm::ErrorOr<std::string> error_or_path =
+      llvm::sys::findProgramByName(filename_str);
+  if (!error_or_path)
+    return false;
+
+  // findProgramByName returns "." if it can't find the file.
+  llvm::StringRef path = *error_or_path;
+  llvm::StringRef parent = llvm::sys::path::parent_path(path);
+  if (parent.empty() || parent == ".")
+    return false;
+
+  // Make sure that the result exists.
+  FileSpec result(*error_or_path, false);
+  if (!Exists(result))
+    return false;
+
+  file_spec = result;
+  return true;
+}

Modified: lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp (original)
+++ lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp Thu Nov  1 10:09:22 2018
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/MonitoringProcessLauncher.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostProcess.h"
 #include "lldb/Target/ProcessLaunchInfo.h"
 #include "lldb/Utility/Log.h"
@@ -38,7 +39,7 @@ MonitoringProcessLauncher::LaunchProcess
     status(exe_spec.GetPath(), stats);
   }
   if (!exists(stats)) {
-    exe_spec.ResolveExecutableLocation();
+    FileSystem::Instance().ResolveExecutableLocation(exe_spec);
     status(exe_spec.GetPath(), stats);
   }
 

Modified: lldb/trunk/source/Host/macosx/objcxx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/objcxx/Host.mm?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/objcxx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/objcxx/Host.mm Thu Nov  1 10:09:22 2018
@@ -55,10 +55,11 @@
 #include <unistd.h>
 
 #include "lldb/Host/ConnectionFileDescriptor.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Host/ThreadLauncher.h"
-#include "lldb/Target/ProcessLaunchInfo.h"
 #include "lldb/Target/Process.h"
+#include "lldb/Target/ProcessLaunchInfo.h"
 #include "lldb/Utility/ArchSpec.h"
 #include "lldb/Utility/CleanUp.h"
 #include "lldb/Utility/DataBufferHeap.h"
@@ -1282,7 +1283,7 @@ Status Host::LaunchProcess(ProcessLaunch
     status(exe_spec.GetPath(), stats);
   }
   if (!exists(stats)) {
-    exe_spec.ResolveExecutableLocation();
+    FileSystem::Instance().ResolveExecutableLocation(exe_spec);
     status(exe_spec.GetPath(), stats);
   }
   if (!exists(stats)) {

Modified: lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp Thu Nov  1 10:09:22 2018
@@ -135,7 +135,8 @@ PlatformPOSIX::ResolveExecutable(const M
     }
 
     if (!resolved_module_spec.GetFileSpec().Exists())
-      resolved_module_spec.GetFileSpec().ResolveExecutableLocation();
+      FileSystem::Instance().ResolveExecutableLocation(
+          resolved_module_spec.GetFileSpec());
 
     // Resolve any executable within a bundle on MacOSX
     Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());

Modified: lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp Thu Nov  1 10:09:22 2018
@@ -199,7 +199,8 @@ Status PlatformWindows::ResolveExecutabl
     }
 
     if (!resolved_module_spec.GetFileSpec().Exists())
-      resolved_module_spec.GetFileSpec().ResolveExecutableLocation();
+      FileSystem::Instance().ResolveExecutableLocation(
+          resolved_module_spec.GetFileSpec());
 
     if (resolved_module_spec.GetFileSpec().Exists())
       error.Clear();

Modified: lldb/trunk/source/Target/ProcessLaunchInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ProcessLaunchInfo.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Target/ProcessLaunchInfo.cpp (original)
+++ lldb/trunk/source/Target/ProcessLaunchInfo.cpp Thu Nov  1 10:09:22 2018
@@ -151,7 +151,7 @@ const FileSpec &ProcessLaunchInfo::GetSh
 void ProcessLaunchInfo::SetShell(const FileSpec &shell) {
   m_shell = shell;
   if (m_shell) {
-    m_shell.ResolveExecutableLocation();
+    FileSystem::Instance().ResolveExecutableLocation(m_shell);
     m_flags.Set(lldb::eLaunchFlagLaunchInShell);
   } else
     m_flags.Clear(lldb::eLaunchFlagLaunchInShell);

Modified: lldb/trunk/source/Utility/FileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/FileSpec.cpp?rev=345853&r1=345852&r2=345853&view=diff
==============================================================================
--- lldb/trunk/source/Utility/FileSpec.cpp (original)
+++ lldb/trunk/source/Utility/FileSpec.cpp Thu Nov  1 10:09:22 2018
@@ -458,42 +458,6 @@ void FileSpec::Dump(Stream *s) const {
 //------------------------------------------------------------------
 bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); }
 
-bool FileSpec::ResolveExecutableLocation() {
-  // CLEANUP: Use StringRef for string handling.
-  if (!m_directory) {
-    const char *file_cstr = m_filename.GetCString();
-    if (file_cstr) {
-      const std::string file_str(file_cstr);
-      llvm::ErrorOr<std::string> error_or_path =
-          llvm::sys::findProgramByName(file_str);
-      if (!error_or_path)
-        return false;
-      std::string path = error_or_path.get();
-      llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
-      if (!dir_ref.empty()) {
-        // FindProgramByName returns "." if it can't find the file.
-        if (strcmp(".", dir_ref.data()) == 0)
-          return false;
-
-        m_directory.SetCString(dir_ref.data());
-        if (Exists())
-          return true;
-        else {
-          // If FindProgramByName found the file, it returns the directory +
-          // filename in its return results. We need to separate them.
-          FileSpec tmp_file(dir_ref.data(), false);
-          if (tmp_file.Exists()) {
-            m_directory = tmp_file.m_directory;
-            return true;
-          }
-        }
-      }
-    }
-  }
-
-  return false;
-}
-
 bool FileSpec::ResolvePath() {
   if (m_is_resolved)
     return true; // We have already resolved this path




More information about the lldb-commits mailing list