labath created this revision. 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). https://reviews.llvm.org/D28858 Files: include/lldb/Host/windows/PosixApi.h source/Host/windows/Windows.cpp source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp source/Target/Platform.cpp source/Target/ProcessLaunchInfo.cpp source/Target/TargetList.cpp
Index: source/Target/TargetList.cpp =================================================================== --- source/Target/TargetList.cpp +++ source/Target/TargetList.cpp @@ -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 @@ 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; } Index: source/Target/ProcessLaunchInfo.cpp =================================================================== --- source/Target/ProcessLaunchInfo.cpp +++ source/Target/ProcessLaunchInfo.cpp @@ -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 @@ 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; Index: source/Target/Platform.cpp =================================================================== --- source/Target/Platform.cpp +++ source/Target/Platform.cpp @@ -523,11 +523,11 @@ 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(); Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp =================================================================== --- source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp +++ source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp @@ -356,12 +356,12 @@ // 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()); } Index: source/Host/windows/Windows.cpp =================================================================== --- source/Host/windows/Windows.cpp +++ source/Host/windows/Windows.cpp @@ -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 @@ 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); } Index: include/lldb/Host/windows/PosixApi.h =================================================================== --- include/lldb/Host/windows/PosixApi.h +++ include/lldb/Host/windows/PosixApi.h @@ -82,7 +82,6 @@ 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);
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits