[Lldb-commits] [lldb] r292795 - Replace getcwd with the llvm equivalent

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Jan 23 07:56:46 PST 2017


Author: labath
Date: Mon Jan 23 09:56:45 2017
New Revision: 292795

URL: http://llvm.org/viewvc/llvm-project?rev=292795&view=rev
Log:
Replace getcwd with the llvm equivalent

Summary:
getcwd() is not available (well.. um.. deprecated?) on windows, and the way
PosixApi.h is providing it causes strange compile errors when it's included in
the wrong order. The best way to avoid that is to just not use chdir.

This replaces all uses of getcwd in generic code. There are still a couple of
more uses, but these are in platform-specific code.

chdir() is causing a similar problem, but for that there is no llvm equivalent
for that (yet).

Reviewers: zturner

Subscribers: lldb-commits

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

Modified:
    lldb/trunk/include/lldb/Host/windows/PosixApi.h
    lldb/trunk/source/Host/windows/Windows.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
    lldb/trunk/source/Target/Platform.cpp
    lldb/trunk/source/Target/ProcessLaunchInfo.cpp
    lldb/trunk/source/Target/TargetList.cpp

Modified: lldb/trunk/include/lldb/Host/windows/PosixApi.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/windows/PosixApi.h?rev=292795&r1=292794&r2=292795&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/windows/PosixApi.h (original)
+++ lldb/trunk/include/lldb/Host/windows/PosixApi.h Mon Jan 23 09:56:45 2017
@@ -82,7 +82,6 @@ char *strcasestr(const char *s, const ch
 char *realpath(const char *name, char *resolved);
 
 int usleep(uint32_t useconds);
-char *getcwd(char *path, int max);
 int chdir(const char *path);
 char *basename(char *path);
 char *dirname(char *path);

Modified: lldb/trunk/source/Host/windows/Windows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/Windows.cpp?rev=292795&r1=292794&r2=292795&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/Windows.cpp (original)
+++ lldb/trunk/source/Host/windows/Windows.cpp Mon Jan 23 09:56:45 2017
@@ -23,10 +23,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-// These prototypes are defined in <direct.h>, but it also defines chdir() and
-// getcwd(), giving multiply defined errors
+// These prototypes are defined in <direct.h>, but it also defines chdir(),
+// giving multiply defined errors
 extern "C" {
-char *_getcwd(char *buffer, int maxlen);
 int _chdir(const char *path);
 }
 
@@ -190,28 +189,6 @@ char *basename(char *path) {
   return &l1[1];
 }
 
-// use _getcwd() instead of GetCurrentDirectory() because it updates errno
-char *getcwd(char *path, int max) {
-  assert(path == NULL || max <= PATH_MAX);
-  wchar_t wpath[PATH_MAX];
-  if (wchar_t *wresult = _wgetcwd(wpath, PATH_MAX)) {
-    // Caller is allowed to pass in NULL for `path`.
-    // In that case, we're supposed to allocate a
-    // buffer on the caller's behalf.
-    if (path == NULL) {
-      max = UNI_MAX_UTF8_BYTES_PER_CODE_POINT * wcslen(wresult) + 1;
-      path = (char *)malloc(max);
-      if (path == NULL) {
-        errno = ENOMEM;
-        return NULL;
-      }
-    }
-    if (wideToUtf8(wresult, path, max))
-      return path;
-  }
-  return NULL;
-}
-
 // use _chdir() instead of SetCurrentDirectory() because it updates errno
 int chdir(const char *path) { return _chdir(path); }
 

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp?rev=292795&r1=292794&r2=292795&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp Mon Jan 23 09:56:45 2017
@@ -356,12 +356,12 @@ GDBRemoteCommunicationServerPlatform::Ha
   // If this packet is sent to a platform, then change the current working
   // directory
 
-  char cwd[PATH_MAX];
-  if (getcwd(cwd, sizeof(cwd)) == NULL)
-    return SendErrorResponse(errno);
+  llvm::SmallString<64> cwd;
+  if (std::error_code ec = llvm::sys::fs::current_path(cwd))
+    return SendErrorResponse(ec.value());
 
   StreamString response;
-  response.PutBytesAsRawHex8(cwd, strlen(cwd));
+  response.PutBytesAsRawHex8(cwd.data(), cwd.size());
   return SendPacketNoLock(response.GetString());
 }
 

Modified: lldb/trunk/source/Target/Platform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Platform.cpp?rev=292795&r1=292794&r2=292795&view=diff
==============================================================================
--- lldb/trunk/source/Target/Platform.cpp (original)
+++ lldb/trunk/source/Target/Platform.cpp Mon Jan 23 09:56:45 2017
@@ -523,11 +523,11 @@ void Platform::AddClangModuleCompilation
 
 FileSpec Platform::GetWorkingDirectory() {
   if (IsHost()) {
-    char cwd[PATH_MAX];
-    if (getcwd(cwd, sizeof(cwd)))
-      return FileSpec{cwd, true};
-    else
+    llvm::SmallString<64> cwd;
+    if (llvm::sys::fs::current_path(cwd))
       return FileSpec{};
+    else
+      return FileSpec(cwd, true);
   } else {
     if (!m_working_dir)
       m_working_dir = GetRemoteWorkingDirectory();

Modified: lldb/trunk/source/Target/ProcessLaunchInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ProcessLaunchInfo.cpp?rev=292795&r1=292794&r2=292795&view=diff
==============================================================================
--- lldb/trunk/source/Target/ProcessLaunchInfo.cpp (original)
+++ lldb/trunk/source/Target/ProcessLaunchInfo.cpp Mon Jan 23 09:56:45 2017
@@ -23,6 +23,7 @@
 #include "lldb/Target/Target.h"
 
 #include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/FileSystem.h"
 
 #if !defined(_WIN32)
 #include <limits.h>
@@ -368,10 +369,8 @@ bool ProcessLaunchInfo::ConvertArguments
           if (working_dir) {
             new_path += working_dir.GetPath();
           } else {
-            char current_working_dir[PATH_MAX];
-            const char *cwd =
-                getcwd(current_working_dir, sizeof(current_working_dir));
-            if (cwd && cwd[0])
+            llvm::SmallString<64> cwd;
+            if (! llvm::sys::fs::current_path(cwd))
               new_path += cwd;
           }
           std::string curr_path;

Modified: lldb/trunk/source/Target/TargetList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/TargetList.cpp?rev=292795&r1=292794&r2=292795&view=diff
==============================================================================
--- lldb/trunk/source/Target/TargetList.cpp (original)
+++ lldb/trunk/source/Target/TargetList.cpp Mon Jan 23 09:56:45 2017
@@ -7,11 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// C Includes
-// C++ Includes
-// Other libraries and framework includes
-#include "llvm/ADT/SmallString.h"
-
 // Project includes
 #include "lldb/Core/Broadcaster.h"
 #include "lldb/Core/Debugger.h"
@@ -29,6 +24,10 @@
 #include "lldb/Target/Process.h"
 #include "lldb/Target/TargetList.h"
 
+// Other libraries and framework includes
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/FileSystem.h"
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -369,12 +368,11 @@ Error TargetList::CreateTargetInternal(D
     if (file.IsRelative() && !user_exe_path.empty()) {
       // Ignore paths that start with "./" and "../"
       if (!user_exe_path.startswith("./") && !user_exe_path.startswith("../")) {
-        char cwd[PATH_MAX];
-        if (getcwd(cwd, sizeof(cwd))) {
-          std::string cwd_user_exe_path(cwd);
-          cwd_user_exe_path += '/';
-          cwd_user_exe_path += user_exe_path;
-          FileSpec cwd_file(cwd_user_exe_path, false);
+        llvm::SmallString<64> cwd;
+        if (! llvm::sys::fs::current_path(cwd)) {
+          cwd += '/';
+          cwd += user_exe_path;
+          FileSpec cwd_file(cwd, false);
           if (cwd_file.Exists())
             file = cwd_file;
         }




More information about the lldb-commits mailing list