summaryrefslogtreecommitdiffstats
path: root/lldb/source
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-03-22 17:33:23 +0000
committerZachary Turner <zturner@google.com>2017-03-22 17:33:23 +0000
commit8d48cd60098cb4cea3455b98f77d531869e64e19 (patch)
tree93800a872e766a5492f085b304e6e45ce5c43269 /lldb/source
parent1012fe8a5459c7227791e4f14714846af249f094 (diff)
downloadbcm5719-llvm-8d48cd60098cb4cea3455b98f77d531869e64e19.tar.gz
bcm5719-llvm-8d48cd60098cb4cea3455b98f77d531869e64e19.zip
Resubmit "Delete the remainder of platform specific code in FileSpec."
This was causing a test failure in one of LLDB's tests which specifically dealt with a limitation in LLVM's implementation of home_directory() that LLDB's own implementation had worked around. This limitation has been addressed in r298513 on the LLVM side, so the failing test (which is now unnecessary as the limitation no longer exists) was removed in r298519, allowing this patch to be re-submitted without modification. llvm-svn: 298526
Diffstat (limited to 'lldb/source')
-rw-r--r--lldb/source/API/SBDeclaration.cpp4
-rw-r--r--lldb/source/API/SBFileSpec.cpp1
-rw-r--r--lldb/source/API/SBFileSpecList.cpp1
-rw-r--r--lldb/source/API/SBLineEntry.cpp1
-rw-r--r--lldb/source/Host/common/FileSpec.cpp118
-rw-r--r--lldb/source/Host/windows/FileSystem.cpp1
-rw-r--r--lldb/source/Host/windows/HostInfoWindows.cpp1
-rw-r--r--lldb/source/Target/TargetList.cpp9
-rw-r--r--lldb/source/Utility/TildeExpressionResolver.cpp19
9 files changed, 35 insertions, 120 deletions
diff --git a/lldb/source/API/SBDeclaration.cpp b/lldb/source/API/SBDeclaration.cpp
index f440304bc96..d6e61e32582 100644
--- a/lldb/source/API/SBDeclaration.cpp
+++ b/lldb/source/API/SBDeclaration.cpp
@@ -1,5 +1,4 @@
-//===-- SBDeclaration.cpp -----------------------------------------*- C++
-//-*-===//
+//===-- SBDeclaration.cpp ----------------------------------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
@@ -10,6 +9,7 @@
#include "lldb/API/SBDeclaration.h"
#include "lldb/API/SBStream.h"
+#include "lldb/Host/PosixApi.h"
#include "lldb/Symbol/Declaration.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
diff --git a/lldb/source/API/SBFileSpec.cpp b/lldb/source/API/SBFileSpec.cpp
index 1039c5c1e96..ef22461e50a 100644
--- a/lldb/source/API/SBFileSpec.cpp
+++ b/lldb/source/API/SBFileSpec.cpp
@@ -13,6 +13,7 @@
#include "lldb/API/SBFileSpec.h"
#include "lldb/API/SBStream.h"
#include "lldb/Host/FileSpec.h"
+#include "lldb/Host/PosixApi.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
diff --git a/lldb/source/API/SBFileSpecList.cpp b/lldb/source/API/SBFileSpecList.cpp
index 508bfb4f17d..f9e9020b97e 100644
--- a/lldb/source/API/SBFileSpecList.cpp
+++ b/lldb/source/API/SBFileSpecList.cpp
@@ -14,6 +14,7 @@
#include "lldb/API/SBStream.h"
#include "lldb/Core/FileSpecList.h"
#include "lldb/Host/FileSpec.h"
+#include "lldb/Host/PosixApi.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
diff --git a/lldb/source/API/SBLineEntry.cpp b/lldb/source/API/SBLineEntry.cpp
index a7754afaa4c..7341d3603df 100644
--- a/lldb/source/API/SBLineEntry.cpp
+++ b/lldb/source/API/SBLineEntry.cpp
@@ -11,6 +11,7 @@
#include "lldb/API/SBLineEntry.h"
#include "lldb/API/SBStream.h"
+#include "lldb/Host/PosixApi.h"
#include "lldb/Symbol/LineEntry.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
diff --git a/lldb/source/Host/common/FileSpec.cpp b/lldb/source/Host/common/FileSpec.cpp
index 8a4facd0b8c..27866eb5b73 100644
--- a/lldb/source/Host/common/FileSpec.cpp
+++ b/lldb/source/Host/common/FileSpec.cpp
@@ -7,21 +7,13 @@
//
//===----------------------------------------------------------------------===//
-#include <fstream>
-#include <set>
-#include <string.h>
-
-#include "llvm/Config/llvm-config.h"
-#ifndef LLVM_ON_WIN32
-#include <pwd.h>
-#endif
-
#include "lldb/Host/FileSpec.h"
#include "lldb/Utility/CleanUp.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StringList.h"
+#include "lldb/Utility/TildeExpressionResolver.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
@@ -144,115 +136,13 @@ size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) {
} // end anonymous namespace
-// Resolves the username part of a path of the form ~user/other/directories, and
-// writes the result into dst_path. This will also resolve "~" to the current
-// user.
-// If you want to complete "~" to the list of users, pass it to
-// ResolvePartialUsername.
-void FileSpec::ResolveUsername(llvm::SmallVectorImpl<char> &path) {
-#ifndef LLVM_ON_WIN32
- if (path.empty() || path[0] != '~')
- return;
-
- llvm::StringRef path_str(path.data(), path.size());
- size_t slash_pos = path_str.find('/', 1);
- if (slash_pos == 1 || path.size() == 1) {
- // A path of ~/ resolves to the current user's home dir
- llvm::SmallString<64> home_dir;
- // llvm::sys::path::home_directory() only checks if "HOME" is set in the
- // environment and does nothing else to locate the user home directory
- if (!llvm::sys::path::home_directory(home_dir)) {
- struct passwd *pw = getpwuid(getuid());
- if (pw && pw->pw_dir && pw->pw_dir[0]) {
- // Update our environemnt so llvm::sys::path::home_directory() works
- // next time
- setenv("HOME", pw->pw_dir, 0);
- home_dir.assign(llvm::StringRef(pw->pw_dir));
- } else {
- return;
- }
- }
-
- // Overwrite the ~ with the first character of the homedir, and insert
- // the rest. This way we only trigger one move, whereas an insert
- // followed by a delete (or vice versa) would trigger two.
- path[0] = home_dir[0];
- path.insert(path.begin() + 1, home_dir.begin() + 1, home_dir.end());
- return;
- }
-
- auto username_begin = path.begin() + 1;
- auto username_end = (slash_pos == llvm::StringRef::npos)
- ? path.end()
- : (path.begin() + slash_pos);
- size_t replacement_length = std::distance(path.begin(), username_end);
-
- llvm::SmallString<20> username(username_begin, username_end);
- struct passwd *user_entry = ::getpwnam(username.c_str());
- if (user_entry != nullptr) {
- // Copy over the first n characters of the path, where n is the smaller of
- // the length
- // of the home directory and the slash pos.
- llvm::StringRef homedir(user_entry->pw_dir);
- size_t initial_copy_length = std::min(homedir.size(), replacement_length);
- auto src_begin = homedir.begin();
- auto src_end = src_begin + initial_copy_length;
- std::copy(src_begin, src_end, path.begin());
- if (replacement_length > homedir.size()) {
- // We copied the entire home directory, but the ~username portion of the
- // path was
- // longer, so there's characters that need to be removed.
- path.erase(path.begin() + initial_copy_length, username_end);
- } else if (replacement_length < homedir.size()) {
- // We copied all the way up to the slash in the destination, but there's
- // still more
- // characters that need to be inserted.
- path.insert(username_end, src_end, homedir.end());
- }
- } else {
- // Unable to resolve username (user doesn't exist?)
- path.clear();
- }
-#endif
-}
-
-size_t FileSpec::ResolvePartialUsername(llvm::StringRef partial_name,
- StringList &matches) {
-#if !defined(LLVM_ON_WIN32) && !defined(__ANDROID__)
- size_t extant_entries = matches.GetSize();
-
- setpwent();
- struct passwd *user_entry;
- partial_name = partial_name.drop_front();
- std::set<std::string> name_list;
-
- while ((user_entry = getpwent()) != NULL) {
- if (llvm::StringRef(user_entry->pw_name).startswith(partial_name)) {
- std::string tmp_buf("~");
- tmp_buf.append(user_entry->pw_name);
- tmp_buf.push_back('/');
- name_list.insert(tmp_buf);
- }
- }
-
- for (auto &name : name_list) {
- matches.AppendString(name);
- }
- return matches.GetSize() - extant_entries;
-#else
- // Resolving home directories is not supported, just copy the path...
- return 0;
-#endif // #ifdef LLVM_ON_WIN32
-}
-
void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
if (path.empty())
return;
-#ifndef LLVM_ON_WIN32
- if (path[0] == '~')
- ResolveUsername(path);
-#endif // #ifdef LLVM_ON_WIN32
+ llvm::SmallString<32> Source(path.begin(), path.end());
+ StandardTildeExpressionResolver Resolver;
+ Resolver.ResolveFullPath(Source, path);
// Save a copy of the original path that's passed in
llvm::SmallString<128> original_path(path.begin(), path.end());
diff --git a/lldb/source/Host/windows/FileSystem.cpp b/lldb/source/Host/windows/FileSystem.cpp
index 907c0c9337a..092b70b1f4d 100644
--- a/lldb/source/Host/windows/FileSystem.cpp
+++ b/lldb/source/Host/windows/FileSystem.cpp
@@ -15,6 +15,7 @@
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/windows/AutoHandle.h"
+#include "lldb/Host/windows/PosixApi.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/FileSystem.h"
diff --git a/lldb/source/Host/windows/HostInfoWindows.cpp b/lldb/source/Host/windows/HostInfoWindows.cpp
index 5b38e6021e0..53a24ad1893 100644
--- a/lldb/source/Host/windows/HostInfoWindows.cpp
+++ b/lldb/source/Host/windows/HostInfoWindows.cpp
@@ -14,6 +14,7 @@
#include <mutex> // std::once
#include "lldb/Host/windows/HostInfoWindows.h"
+#include "lldb/Host/windows/PosixApi.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/FileSystem.h"
diff --git a/lldb/source/Target/TargetList.cpp b/lldb/source/Target/TargetList.cpp
index 0156ab17357..0849c18b89e 100644
--- a/lldb/source/Target/TargetList.cpp
+++ b/lldb/source/Target/TargetList.cpp
@@ -23,6 +23,7 @@
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
+#include "lldb/Utility/TildeExpressionResolver.h"
// Other libraries and framework includes
#include "llvm/ADT/SmallString.h"
@@ -347,10 +348,10 @@ Error TargetList::CreateTargetInternal(Debugger &debugger,
FileSpec file(user_exe_path, false);
if (!file.Exists() && user_exe_path.startswith("~")) {
// we want to expand the tilde but we don't want to resolve any symbolic
- // links
- // so we can't use the FileSpec constructor's resolve flag
- llvm::SmallString<64> unglobbed_path(user_exe_path);
- FileSpec::ResolveUsername(unglobbed_path);
+ // links so we can't use the FileSpec constructor's resolve flag
+ llvm::SmallString<64> unglobbed_path;
+ StandardTildeExpressionResolver Resolver;
+ Resolver.ResolveFullPath(user_exe_path, unglobbed_path);
if (unglobbed_path.empty())
file = FileSpec(user_exe_path, false);
diff --git a/lldb/source/Utility/TildeExpressionResolver.cpp b/lldb/source/Utility/TildeExpressionResolver.cpp
index c7be68dbbb9..2ad0c4e119d 100644
--- a/lldb/source/Utility/TildeExpressionResolver.cpp
+++ b/lldb/source/Utility/TildeExpressionResolver.cpp
@@ -68,3 +68,22 @@ bool StandardTildeExpressionResolver::ResolvePartial(StringRef Expr,
return true;
#endif
}
+
+bool TildeExpressionResolver::ResolveFullPath(
+ StringRef Expr, llvm::SmallVectorImpl<char> &Output) {
+ Output.clear();
+ if (!Expr.startswith("~")) {
+ Output.append(Expr.begin(), Expr.end());
+ return false;
+ }
+
+ namespace path = llvm::sys::path;
+ StringRef Left =
+ Expr.take_until([](char c) { return path::is_separator(c); });
+
+ if (!ResolveExact(Left, Output))
+ return false;
+
+ Output.append(Expr.begin() + Left.size(), Expr.end());
+ return true;
+}
OpenPOWER on IntegriCloud