diff options
author | Pavel Labath <labath@google.com> | 2017-01-16 10:07:02 +0000 |
---|---|---|
committer | Pavel Labath <labath@google.com> | 2017-01-16 10:07:02 +0000 |
commit | 59d725cabf44b53be32ab4402e59f89e52b51f26 (patch) | |
tree | ca83ae010bad66b9cd6e4db5aefe06b40c47b293 /lldb/source/Host/common/FileSpec.cpp | |
parent | e6b5b34f6ff0c7021b13daf6d3c052f5de70aa93 (diff) | |
download | bcm5719-llvm-59d725cabf44b53be32ab4402e59f89e52b51f26.tar.gz bcm5719-llvm-59d725cabf44b53be32ab4402e59f89e52b51f26.zip |
FileSpec: Fix PrependPathComponent("/")
Summary:
PrependPathComponent was unconditionally inserting path separators between the
path components. This is not correct if the prepended path is "/", which caused
problems down the line. Fix the function to use the same algorithm as
AppendPathComponent and add a test. This fixes one part of llvm.org/pr31611.
Reviewers: clayborg, zturner
Subscribers: lldb-commits
Differential Revision: https://reviews.llvm.org/D28677
llvm-svn: 292100
Diffstat (limited to 'lldb/source/Host/common/FileSpec.cpp')
-rw-r--r-- | lldb/source/Host/common/FileSpec.cpp | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/lldb/source/Host/common/FileSpec.cpp b/lldb/source/Host/common/FileSpec.cpp index 7f46d303a5d..578ca9f07cf 100644 --- a/lldb/source/Host/common/FileSpec.cpp +++ b/lldb/source/Host/common/FileSpec.cpp @@ -1248,6 +1248,22 @@ ConstString FileSpec::GetLastPathComponent() const { return ConstString(); } +static std::string +join_path_components(FileSpec::PathSyntax syntax, + const std::vector<llvm::StringRef> components) { + std::string result; + for (size_t i = 0; i < components.size(); ++i) { + if (components[i].empty()) + continue; + result += components[i]; + if (i != components.size() - 1 && + !IsPathSeparator(components[i].back(), syntax)) + result += GetPreferredPathSeparator(syntax); + } + + return result; +} + void FileSpec::PrependPathComponent(llvm::StringRef component) { if (component.empty()) return; @@ -1258,16 +1274,9 @@ void FileSpec::PrependPathComponent(llvm::StringRef component) { return; } - char sep = GetPreferredPathSeparator(m_syntax); - std::string result; - if (m_filename.IsEmpty()) - result = llvm::join_items(sep, component, m_directory.GetStringRef()); - else if (m_directory.IsEmpty()) - result = llvm::join_items(sep, component, m_filename.GetStringRef()); - else - result = llvm::join_items(sep, component, m_directory.GetStringRef(), - m_filename.GetStringRef()); - + std::string result = + join_path_components(m_syntax, {component, m_directory.GetStringRef(), + m_filename.GetStringRef()}); SetFile(result, resolve); } @@ -1279,23 +1288,12 @@ void FileSpec::AppendPathComponent(llvm::StringRef component) { if (component.empty()) return; - std::string result; - if (!m_directory.IsEmpty()) { - result += m_directory.GetStringRef(); - if (!IsPathSeparator(m_directory.GetStringRef().back(), m_syntax)) - result += GetPreferredPathSeparator(m_syntax); - } - - if (!m_filename.IsEmpty()) { - result += m_filename.GetStringRef(); - if (!IsPathSeparator(m_filename.GetStringRef().back(), m_syntax)) - result += GetPreferredPathSeparator(m_syntax); - } - component = component.drop_while( [this](char c) { return IsPathSeparator(c, m_syntax); }); - result += component; + std::string result = + join_path_components(m_syntax, {m_directory.GetStringRef(), + m_filename.GetStringRef(), component}); SetFile(result, false, m_syntax); } |