diff options
Diffstat (limited to 'source/Target/PathMappingList.cpp')
| -rw-r--r-- | source/Target/PathMappingList.cpp | 92 | 
1 files changed, 54 insertions, 38 deletions
diff --git a/source/Target/PathMappingList.cpp b/source/Target/PathMappingList.cpp index c2249cce89b9..58c2c43c9368 100644 --- a/source/Target/PathMappingList.cpp +++ b/source/Target/PathMappingList.cpp @@ -1,9 +1,8 @@  //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//  // -//                     The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception  //  //===----------------------------------------------------------------------===// @@ -31,15 +30,13 @@ namespace {    // with the raw path pair, which doesn't work anymore because the paths have    // been normalized when the debug info was loaded. So we need to store    // nomalized path pairs to ensure things match up. -  ConstString NormalizePath(const ConstString &path) { +  ConstString NormalizePath(ConstString path) {      // If we use "path" to construct a FileSpec, it will normalize the path for      // us. We then grab the string and turn it back into a ConstString.      return ConstString(FileSpec(path.GetStringRef()).GetPath());    }  } -//----------------------------------------------------------------------  // PathMappingList constructor -//----------------------------------------------------------------------  PathMappingList::PathMappingList()      : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {} @@ -63,8 +60,8 @@ const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {  PathMappingList::~PathMappingList() = default; -void PathMappingList::Append(const ConstString &path, -                             const ConstString &replacement, bool notify) { +void PathMappingList::Append(ConstString path, +                             ConstString replacement, bool notify) {    ++m_mod_id;    m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));    if (notify && m_callback) @@ -82,8 +79,8 @@ void PathMappingList::Append(const PathMappingList &rhs, bool notify) {    }  } -void PathMappingList::Insert(const ConstString &path, -                             const ConstString &replacement, uint32_t index, +void PathMappingList::Insert(ConstString path, +                             ConstString replacement, uint32_t index,                               bool notify) {    ++m_mod_id;    iterator insert_iter; @@ -97,8 +94,8 @@ void PathMappingList::Insert(const ConstString &path,      m_callback(*this, m_callback_baton);  } -bool PathMappingList::Replace(const ConstString &path, -                              const ConstString &replacement, uint32_t index, +bool PathMappingList::Replace(ConstString path, +                              ConstString replacement, uint32_t index,                                bool notify) {    if (index >= m_pairs.size())      return false; @@ -147,7 +144,7 @@ void PathMappingList::Clear(bool notify) {      m_callback(*this, m_callback_baton);  } -bool PathMappingList::RemapPath(const ConstString &path, +bool PathMappingList::RemapPath(ConstString path,                                  ConstString &new_path) const {    std::string remapped;    if (RemapPath(path.GetStringRef(), remapped)) { @@ -202,32 +199,51 @@ bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) co  bool PathMappingList::FindFile(const FileSpec &orig_spec,                                 FileSpec &new_spec) const { -  if (!m_pairs.empty()) { -    char orig_path[PATH_MAX]; -    const size_t orig_path_len = -        orig_spec.GetPath(orig_path, sizeof(orig_path)); -    if (orig_path_len > 0) { -      const_iterator pos, end = m_pairs.end(); -      for (pos = m_pairs.begin(); pos != end; ++pos) { -        const size_t prefix_len = pos->first.GetLength(); - -        if (orig_path_len >= prefix_len) { -          if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) { -            new_spec.SetFile(pos->second.GetCString(), FileSpec::Style::native); -            new_spec.AppendPathComponent(orig_path + prefix_len); -            if (FileSystem::Instance().Exists(new_spec)) -              return true; -          } -        } -      } +  if (m_pairs.empty()) +    return false; +   +  std::string orig_path = orig_spec.GetPath(); +     +  if (orig_path.empty()) +    return false; +       +  bool orig_is_relative = orig_spec.IsRelative(); + +  for (auto entry : m_pairs) { +    llvm::StringRef orig_ref(orig_path); +    llvm::StringRef prefix_ref = entry.first.GetStringRef(); +    if (orig_ref.size() < prefix_ref.size()) +      continue; +    // We consider a relative prefix or one of just "." to +    // mean "only apply to relative paths". +    bool prefix_is_relative = false; +     +    if (prefix_ref == ".") { +      prefix_is_relative = true; +      // Remove the "." since it will have been removed from the +      // FileSpec paths already. +      prefix_ref = prefix_ref.drop_front(); +    } else { +      FileSpec prefix_spec(prefix_ref, FileSpec::Style::native); +      prefix_is_relative = prefix_spec.IsRelative(); +    } +    if (prefix_is_relative != orig_is_relative) +      continue; + +    if (orig_ref.consume_front(prefix_ref)) { +      new_spec.SetFile(entry.second.GetCString(), FileSpec::Style::native); +      new_spec.AppendPathComponent(orig_ref); +      if (FileSystem::Instance().Exists(new_spec)) +        return true;      }    } +      new_spec.Clear();    return false;  } -bool PathMappingList::Replace(const ConstString &path, -                              const ConstString &new_path, bool notify) { +bool PathMappingList::Replace(ConstString path, +                              ConstString new_path, bool notify) {    uint32_t idx = FindIndexForPath(path);    if (idx < m_pairs.size()) {      ++m_mod_id; @@ -239,7 +255,7 @@ bool PathMappingList::Replace(const ConstString &path,    return false;  } -bool PathMappingList::Remove(const ConstString &path, bool notify) { +bool PathMappingList::Remove(ConstString path, bool notify) {    iterator pos = FindIteratorForPath(path);    if (pos != m_pairs.end()) {      ++m_mod_id; @@ -252,7 +268,7 @@ bool PathMappingList::Remove(const ConstString &path, bool notify) {  }  PathMappingList::const_iterator -PathMappingList::FindIteratorForPath(const ConstString &path) const { +PathMappingList::FindIteratorForPath(ConstString path) const {    const_iterator pos;    const_iterator begin = m_pairs.begin();    const_iterator end = m_pairs.end(); @@ -265,7 +281,7 @@ PathMappingList::FindIteratorForPath(const ConstString &path) const {  }  PathMappingList::iterator -PathMappingList::FindIteratorForPath(const ConstString &path) { +PathMappingList::FindIteratorForPath(ConstString path) {    iterator pos;    iterator begin = m_pairs.begin();    iterator end = m_pairs.end(); @@ -287,7 +303,7 @@ bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,    return false;  } -uint32_t PathMappingList::FindIndexForPath(const ConstString &orig_path) const { +uint32_t PathMappingList::FindIndexForPath(ConstString orig_path) const {    const ConstString path = NormalizePath(orig_path);    const_iterator pos;    const_iterator begin = m_pairs.begin();  | 
