15ffd83dbSDimitry Andric //===-- PathMappingList.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include <climits>
100b57cec5SDimitry Andric #include <cstring>
11bdd1243dSDimitry Andric #include <optional>
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
140b57cec5SDimitry Andric #include "lldb/Host/PosixApi.h"
150b57cec5SDimitry Andric #include "lldb/Target/PathMappingList.h"
160b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
170b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
180b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
190b57cec5SDimitry Andric #include "lldb/lldb-private-enumerations.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace lldb;
220b57cec5SDimitry Andric using namespace lldb_private;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace {
250b57cec5SDimitry Andric   // We must normalize our path pairs that we store because if we don't then
260b57cec5SDimitry Andric   // things won't always work. We found a case where if we did:
270b57cec5SDimitry Andric   // (lldb) settings set target.source-map . /tmp
280b57cec5SDimitry Andric   // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
290b57cec5SDimitry Andric   // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
300b57cec5SDimitry Andric   // When PathMappingList::RemapPath() is called, it expects the path to start
310b57cec5SDimitry Andric   // with the raw path pair, which doesn't work anymore because the paths have
320b57cec5SDimitry Andric   // been normalized when the debug info was loaded. So we need to store
330b57cec5SDimitry Andric   // nomalized path pairs to ensure things match up.
NormalizePath(llvm::StringRef path)34349cc55cSDimitry Andric std::string NormalizePath(llvm::StringRef path) {
350b57cec5SDimitry Andric   // If we use "path" to construct a FileSpec, it will normalize the path for
36349cc55cSDimitry Andric   // us. We then grab the string.
37349cc55cSDimitry Andric   return FileSpec(path).GetPath();
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric // PathMappingList constructor
PathMappingList()41fe6060f1SDimitry Andric PathMappingList::PathMappingList() : m_pairs() {}
420b57cec5SDimitry Andric 
PathMappingList(ChangedCallback callback,void * callback_baton)430b57cec5SDimitry Andric PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
4481ad6265SDimitry Andric     : m_pairs(), m_callback(callback), m_callback_baton(callback_baton) {}
450b57cec5SDimitry Andric 
PathMappingList(const PathMappingList & rhs)460b57cec5SDimitry Andric PathMappingList::PathMappingList(const PathMappingList &rhs)
4781ad6265SDimitry Andric     : m_pairs(rhs.m_pairs) {}
480b57cec5SDimitry Andric 
operator =(const PathMappingList & rhs)490b57cec5SDimitry Andric const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
500b57cec5SDimitry Andric   if (this != &rhs) {
5106c3fb27SDimitry Andric     std::scoped_lock<std::recursive_mutex, std::recursive_mutex> locks(m_mutex, rhs.m_mutex);
520b57cec5SDimitry Andric     m_pairs = rhs.m_pairs;
530b57cec5SDimitry Andric     m_callback = nullptr;
540b57cec5SDimitry Andric     m_callback_baton = nullptr;
550b57cec5SDimitry Andric     m_mod_id = rhs.m_mod_id;
560b57cec5SDimitry Andric   }
570b57cec5SDimitry Andric   return *this;
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric PathMappingList::~PathMappingList() = default;
610b57cec5SDimitry Andric 
Append(llvm::StringRef path,llvm::StringRef replacement,bool notify)62349cc55cSDimitry Andric void PathMappingList::Append(llvm::StringRef path, llvm::StringRef replacement,
63349cc55cSDimitry Andric                              bool notify) {
6406c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
650b57cec5SDimitry Andric   ++m_mod_id;
660b57cec5SDimitry Andric   m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
670b57cec5SDimitry Andric   if (notify && m_callback)
680b57cec5SDimitry Andric     m_callback(*this, m_callback_baton);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
Append(const PathMappingList & rhs,bool notify)710b57cec5SDimitry Andric void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
7206c3fb27SDimitry Andric   std::scoped_lock<std::recursive_mutex, std::recursive_mutex> locks(m_mutex, rhs.m_mutex);
730b57cec5SDimitry Andric   ++m_mod_id;
740b57cec5SDimitry Andric   if (!rhs.m_pairs.empty()) {
750b57cec5SDimitry Andric     const_iterator pos, end = rhs.m_pairs.end();
760b57cec5SDimitry Andric     for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
770b57cec5SDimitry Andric       m_pairs.push_back(*pos);
780b57cec5SDimitry Andric     if (notify && m_callback)
790b57cec5SDimitry Andric       m_callback(*this, m_callback_baton);
800b57cec5SDimitry Andric   }
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
AppendUnique(llvm::StringRef path,llvm::StringRef replacement,bool notify)83bdd1243dSDimitry Andric bool PathMappingList::AppendUnique(llvm::StringRef path,
84bdd1243dSDimitry Andric                                    llvm::StringRef replacement, bool notify) {
85bdd1243dSDimitry Andric   auto normalized_path = NormalizePath(path);
86bdd1243dSDimitry Andric   auto normalized_replacement = NormalizePath(replacement);
8706c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
88bdd1243dSDimitry Andric   for (const auto &pair : m_pairs) {
89bdd1243dSDimitry Andric     if (pair.first.GetStringRef().equals(normalized_path) &&
90bdd1243dSDimitry Andric         pair.second.GetStringRef().equals(normalized_replacement))
91bdd1243dSDimitry Andric       return false;
92bdd1243dSDimitry Andric   }
93bdd1243dSDimitry Andric   Append(path, replacement, notify);
94bdd1243dSDimitry Andric   return true;
95bdd1243dSDimitry Andric }
96bdd1243dSDimitry Andric 
Insert(llvm::StringRef path,llvm::StringRef replacement,uint32_t index,bool notify)97349cc55cSDimitry Andric void PathMappingList::Insert(llvm::StringRef path, llvm::StringRef replacement,
98349cc55cSDimitry Andric                              uint32_t index, bool notify) {
9906c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
1000b57cec5SDimitry Andric   ++m_mod_id;
1010b57cec5SDimitry Andric   iterator insert_iter;
1020b57cec5SDimitry Andric   if (index >= m_pairs.size())
1030b57cec5SDimitry Andric     insert_iter = m_pairs.end();
1040b57cec5SDimitry Andric   else
1050b57cec5SDimitry Andric     insert_iter = m_pairs.begin() + index;
1060b57cec5SDimitry Andric   m_pairs.emplace(insert_iter, pair(NormalizePath(path),
1070b57cec5SDimitry Andric                                     NormalizePath(replacement)));
1080b57cec5SDimitry Andric   if (notify && m_callback)
1090b57cec5SDimitry Andric     m_callback(*this, m_callback_baton);
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
Replace(llvm::StringRef path,llvm::StringRef replacement,uint32_t index,bool notify)112349cc55cSDimitry Andric bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef replacement,
113349cc55cSDimitry Andric                               uint32_t index, bool notify) {
11406c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
1150b57cec5SDimitry Andric   if (index >= m_pairs.size())
1160b57cec5SDimitry Andric     return false;
1170b57cec5SDimitry Andric   ++m_mod_id;
1180b57cec5SDimitry Andric   m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
1190b57cec5SDimitry Andric   if (notify && m_callback)
1200b57cec5SDimitry Andric     m_callback(*this, m_callback_baton);
1210b57cec5SDimitry Andric   return true;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
Remove(size_t index,bool notify)1240b57cec5SDimitry Andric bool PathMappingList::Remove(size_t index, bool notify) {
12506c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
1260b57cec5SDimitry Andric   if (index >= m_pairs.size())
1270b57cec5SDimitry Andric     return false;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   ++m_mod_id;
1300b57cec5SDimitry Andric   iterator iter = m_pairs.begin() + index;
1310b57cec5SDimitry Andric   m_pairs.erase(iter);
1320b57cec5SDimitry Andric   if (notify && m_callback)
1330b57cec5SDimitry Andric     m_callback(*this, m_callback_baton);
1340b57cec5SDimitry Andric   return true;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric // For clients which do not need the pair index dumped, pass a pair_index >= 0
1380b57cec5SDimitry Andric // to only dump the indicated pair.
Dump(Stream * s,int pair_index)1390b57cec5SDimitry Andric void PathMappingList::Dump(Stream *s, int pair_index) {
14006c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
1410b57cec5SDimitry Andric   unsigned int numPairs = m_pairs.size();
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   if (pair_index < 0) {
1440b57cec5SDimitry Andric     unsigned int index;
1450b57cec5SDimitry Andric     for (index = 0; index < numPairs; ++index)
1460b57cec5SDimitry Andric       s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
1470b57cec5SDimitry Andric                 m_pairs[index].first.GetCString(),
1480b57cec5SDimitry Andric                 m_pairs[index].second.GetCString());
1490b57cec5SDimitry Andric   } else {
1500b57cec5SDimitry Andric     if (static_cast<unsigned int>(pair_index) < numPairs)
1510b57cec5SDimitry Andric       s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
1520b57cec5SDimitry Andric                 m_pairs[pair_index].second.GetCString());
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
ToJSON()156bdd1243dSDimitry Andric llvm::json::Value PathMappingList::ToJSON() {
157bdd1243dSDimitry Andric   llvm::json::Array entries;
15806c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
159bdd1243dSDimitry Andric   for (const auto &pair : m_pairs) {
160bdd1243dSDimitry Andric     llvm::json::Array entry{pair.first.GetStringRef().str(),
161bdd1243dSDimitry Andric                             pair.second.GetStringRef().str()};
162bdd1243dSDimitry Andric     entries.emplace_back(std::move(entry));
163bdd1243dSDimitry Andric   }
164bdd1243dSDimitry Andric   return entries;
165bdd1243dSDimitry Andric }
166bdd1243dSDimitry Andric 
Clear(bool notify)1670b57cec5SDimitry Andric void PathMappingList::Clear(bool notify) {
16806c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
1690b57cec5SDimitry Andric   if (!m_pairs.empty())
1700b57cec5SDimitry Andric     ++m_mod_id;
1710b57cec5SDimitry Andric   m_pairs.clear();
1720b57cec5SDimitry Andric   if (notify && m_callback)
1730b57cec5SDimitry Andric     m_callback(*this, m_callback_baton);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
RemapPath(ConstString path,ConstString & new_path) const1760b57cec5SDimitry Andric bool PathMappingList::RemapPath(ConstString path,
1770b57cec5SDimitry Andric                                 ConstString &new_path) const {
178bdd1243dSDimitry Andric   if (std::optional<FileSpec> remapped = RemapPath(path.GetStringRef())) {
179fe6060f1SDimitry Andric     new_path.SetString(remapped->GetPath());
1800b57cec5SDimitry Andric     return true;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric   return false;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
185fe6060f1SDimitry Andric /// Append components to path, applying style.
AppendPathComponents(FileSpec & path,llvm::StringRef components,llvm::sys::path::Style style)186fe6060f1SDimitry Andric static void AppendPathComponents(FileSpec &path, llvm::StringRef components,
187fe6060f1SDimitry Andric                                  llvm::sys::path::Style style) {
188fe6060f1SDimitry Andric   auto component = llvm::sys::path::begin(components, style);
189fe6060f1SDimitry Andric   auto e = llvm::sys::path::end(components);
190fe6060f1SDimitry Andric   while (component != e &&
191fe6060f1SDimitry Andric          llvm::sys::path::is_separator(*component->data(), style))
192fe6060f1SDimitry Andric     ++component;
193fe6060f1SDimitry Andric   for (; component != e; ++component)
194fe6060f1SDimitry Andric     path.AppendPathComponent(*component);
195fe6060f1SDimitry Andric }
196fe6060f1SDimitry Andric 
RemapPath(llvm::StringRef mapping_path,bool only_if_exists) const197bdd1243dSDimitry Andric std::optional<FileSpec> PathMappingList::RemapPath(llvm::StringRef mapping_path,
198fe6060f1SDimitry Andric                                                    bool only_if_exists) const {
19906c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
200fe6060f1SDimitry Andric   if (m_pairs.empty() || mapping_path.empty())
201fe6060f1SDimitry Andric     return {};
2020b57cec5SDimitry Andric   LazyBool path_is_relative = eLazyBoolCalculate;
203fe6060f1SDimitry Andric 
2040b57cec5SDimitry Andric   for (const auto &it : m_pairs) {
205fe6060f1SDimitry Andric     llvm::StringRef prefix = it.first.GetStringRef();
206fe6060f1SDimitry Andric     // We create a copy of mapping_path because StringRef::consume_from
207fe6060f1SDimitry Andric     // effectively modifies the instance itself.
208fe6060f1SDimitry Andric     llvm::StringRef path = mapping_path;
2090b57cec5SDimitry Andric     if (!path.consume_front(prefix)) {
2100b57cec5SDimitry Andric       // Relative paths won't have a leading "./" in them unless "." is the
2110b57cec5SDimitry Andric       // only thing in the relative path so we need to work around "."
2120b57cec5SDimitry Andric       // carefully.
2130b57cec5SDimitry Andric       if (prefix != ".")
2140b57cec5SDimitry Andric         continue;
2150b57cec5SDimitry Andric       // We need to figure out if the "path" argument is relative. If it is,
2160b57cec5SDimitry Andric       // then we should remap, else skip this entry.
2170b57cec5SDimitry Andric       if (path_is_relative == eLazyBoolCalculate) {
2180b57cec5SDimitry Andric         path_is_relative =
2190b57cec5SDimitry Andric             FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo;
2200b57cec5SDimitry Andric       }
2210b57cec5SDimitry Andric       if (!path_is_relative)
2220b57cec5SDimitry Andric         continue;
2230b57cec5SDimitry Andric     }
2240b57cec5SDimitry Andric     FileSpec remapped(it.second.GetStringRef());
22581ad6265SDimitry Andric     auto orig_style = FileSpec::GuessPathStyle(prefix).value_or(
226fe6060f1SDimitry Andric         llvm::sys::path::Style::native);
227fe6060f1SDimitry Andric     AppendPathComponents(remapped, path, orig_style);
228fe6060f1SDimitry Andric     if (!only_if_exists || FileSystem::Instance().Exists(remapped))
229fe6060f1SDimitry Andric       return remapped;
2300b57cec5SDimitry Andric   }
231fe6060f1SDimitry Andric   return {};
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
234bdd1243dSDimitry Andric std::optional<llvm::StringRef>
ReverseRemapPath(const FileSpec & file,FileSpec & fixed) const235bdd1243dSDimitry Andric PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
2360b57cec5SDimitry Andric   std::string path = file.GetPath();
2370b57cec5SDimitry Andric   llvm::StringRef path_ref(path);
23806c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
2390b57cec5SDimitry Andric   for (const auto &it : m_pairs) {
240bdd1243dSDimitry Andric     llvm::StringRef removed_prefix = it.second.GetStringRef();
2410b57cec5SDimitry Andric     if (!path_ref.consume_front(it.second.GetStringRef()))
2420b57cec5SDimitry Andric       continue;
243fe6060f1SDimitry Andric     auto orig_file = it.first.GetStringRef();
24481ad6265SDimitry Andric     auto orig_style = FileSpec::GuessPathStyle(orig_file).value_or(
245fe6060f1SDimitry Andric         llvm::sys::path::Style::native);
246fe6060f1SDimitry Andric     fixed.SetFile(orig_file, orig_style);
247fe6060f1SDimitry Andric     AppendPathComponents(fixed, path_ref, orig_style);
248bdd1243dSDimitry Andric     return removed_prefix;
2490b57cec5SDimitry Andric   }
250bdd1243dSDimitry Andric   return std::nullopt;
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
253bdd1243dSDimitry Andric std::optional<FileSpec>
FindFile(const FileSpec & orig_spec) const254bdd1243dSDimitry Andric PathMappingList::FindFile(const FileSpec &orig_spec) const {
255349cc55cSDimitry Andric   // We must normalize the orig_spec again using the host's path style,
256349cc55cSDimitry Andric   // otherwise there will be mismatch between the host and remote platform
257349cc55cSDimitry Andric   // if they use different path styles.
258349cc55cSDimitry Andric   if (auto remapped = RemapPath(NormalizePath(orig_spec.GetPath()),
259349cc55cSDimitry Andric                                 /*only_if_exists=*/true))
260fe6060f1SDimitry Andric     return remapped;
2610b57cec5SDimitry Andric 
262fe6060f1SDimitry Andric   return {};
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
Replace(llvm::StringRef path,llvm::StringRef new_path,bool notify)265349cc55cSDimitry Andric bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef new_path,
266349cc55cSDimitry Andric                               bool notify) {
26706c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
2680b57cec5SDimitry Andric   uint32_t idx = FindIndexForPath(path);
2690b57cec5SDimitry Andric   if (idx < m_pairs.size()) {
2700b57cec5SDimitry Andric     ++m_mod_id;
271349cc55cSDimitry Andric     m_pairs[idx].second = ConstString(new_path);
2720b57cec5SDimitry Andric     if (notify && m_callback)
2730b57cec5SDimitry Andric       m_callback(*this, m_callback_baton);
2740b57cec5SDimitry Andric     return true;
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric   return false;
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
Remove(ConstString path,bool notify)2790b57cec5SDimitry Andric bool PathMappingList::Remove(ConstString path, bool notify) {
28006c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
2810b57cec5SDimitry Andric   iterator pos = FindIteratorForPath(path);
2820b57cec5SDimitry Andric   if (pos != m_pairs.end()) {
2830b57cec5SDimitry Andric     ++m_mod_id;
2840b57cec5SDimitry Andric     m_pairs.erase(pos);
2850b57cec5SDimitry Andric     if (notify && m_callback)
2860b57cec5SDimitry Andric       m_callback(*this, m_callback_baton);
2870b57cec5SDimitry Andric     return true;
2880b57cec5SDimitry Andric   }
2890b57cec5SDimitry Andric   return false;
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric PathMappingList::const_iterator
FindIteratorForPath(ConstString path) const2930b57cec5SDimitry Andric PathMappingList::FindIteratorForPath(ConstString path) const {
29406c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
2950b57cec5SDimitry Andric   const_iterator pos;
2960b57cec5SDimitry Andric   const_iterator begin = m_pairs.begin();
2970b57cec5SDimitry Andric   const_iterator end = m_pairs.end();
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   for (pos = begin; pos != end; ++pos) {
3000b57cec5SDimitry Andric     if (pos->first == path)
3010b57cec5SDimitry Andric       break;
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric   return pos;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric PathMappingList::iterator
FindIteratorForPath(ConstString path)3070b57cec5SDimitry Andric PathMappingList::FindIteratorForPath(ConstString path) {
30806c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
3090b57cec5SDimitry Andric   iterator pos;
3100b57cec5SDimitry Andric   iterator begin = m_pairs.begin();
3110b57cec5SDimitry Andric   iterator end = m_pairs.end();
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   for (pos = begin; pos != end; ++pos) {
3140b57cec5SDimitry Andric     if (pos->first == path)
3150b57cec5SDimitry Andric       break;
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric   return pos;
3180b57cec5SDimitry Andric }
3190b57cec5SDimitry Andric 
GetPathsAtIndex(uint32_t idx,ConstString & path,ConstString & new_path) const3200b57cec5SDimitry Andric bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
3210b57cec5SDimitry Andric                                       ConstString &new_path) const {
32206c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
3230b57cec5SDimitry Andric   if (idx < m_pairs.size()) {
3240b57cec5SDimitry Andric     path = m_pairs[idx].first;
3250b57cec5SDimitry Andric     new_path = m_pairs[idx].second;
3260b57cec5SDimitry Andric     return true;
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric   return false;
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
FindIndexForPath(llvm::StringRef orig_path) const331349cc55cSDimitry Andric uint32_t PathMappingList::FindIndexForPath(llvm::StringRef orig_path) const {
332349cc55cSDimitry Andric   const ConstString path = ConstString(NormalizePath(orig_path));
33306c3fb27SDimitry Andric   std::lock_guard<std::recursive_mutex> lock(m_mutex);
3340b57cec5SDimitry Andric   const_iterator pos;
3350b57cec5SDimitry Andric   const_iterator begin = m_pairs.begin();
3360b57cec5SDimitry Andric   const_iterator end = m_pairs.end();
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   for (pos = begin; pos != end; ++pos) {
3390b57cec5SDimitry Andric     if (pos->first == path)
3400b57cec5SDimitry Andric       return std::distance(begin, pos);
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric   return UINT32_MAX;
3430b57cec5SDimitry Andric }
344