1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 // Other libraries and framework includes
17 #include "lldb/Host/PosixApi.h"
18 #include "lldb/Target/PathMappingList.h"
19 #include "lldb/Utility/FileSpec.h"
20 #include "lldb/Utility/Status.h"
21 #include "lldb/Utility/Stream.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // PathMappingList constructor
28 //----------------------------------------------------------------------
29 PathMappingList::PathMappingList()
30 : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
32 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
33 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
36 PathMappingList::PathMappingList(const PathMappingList &rhs)
37 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
40 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
42 m_pairs = rhs.m_pairs;
44 m_callback_baton = nullptr;
45 m_mod_id = rhs.m_mod_id;
50 PathMappingList::~PathMappingList() = default;
52 void PathMappingList::Append(const ConstString &path,
53 const ConstString &replacement, bool notify) {
55 m_pairs.push_back(pair(path, replacement));
56 if (notify && m_callback)
57 m_callback(*this, m_callback_baton);
60 void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
62 if (!rhs.m_pairs.empty()) {
63 const_iterator pos, end = rhs.m_pairs.end();
64 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
65 m_pairs.push_back(*pos);
66 if (notify && m_callback)
67 m_callback(*this, m_callback_baton);
71 void PathMappingList::Insert(const ConstString &path,
72 const ConstString &replacement, uint32_t index,
76 if (index >= m_pairs.size())
77 insert_iter = m_pairs.end();
79 insert_iter = m_pairs.begin() + index;
80 m_pairs.insert(insert_iter, pair(path, replacement));
81 if (notify && m_callback)
82 m_callback(*this, m_callback_baton);
85 bool PathMappingList::Replace(const ConstString &path,
86 const ConstString &replacement, uint32_t index,
89 if (index >= m_pairs.size())
92 m_pairs[index] = pair(path, replacement);
93 if (notify && m_callback)
94 m_callback(*this, m_callback_baton);
98 bool PathMappingList::Remove(size_t index, bool notify) {
99 if (index >= m_pairs.size())
103 iterator iter = m_pairs.begin() + index;
105 if (notify && m_callback)
106 m_callback(*this, m_callback_baton);
110 // For clients which do not need the pair index dumped, pass a pair_index >= 0
111 // to only dump the indicated pair.
112 void PathMappingList::Dump(Stream *s, int pair_index) {
113 unsigned int numPairs = m_pairs.size();
115 if (pair_index < 0) {
117 for (index = 0; index < numPairs; ++index)
118 s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
119 m_pairs[index].first.GetCString(),
120 m_pairs[index].second.GetCString());
122 if (static_cast<unsigned int>(pair_index) < numPairs)
123 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
124 m_pairs[pair_index].second.GetCString());
128 void PathMappingList::Clear(bool notify) {
129 if (!m_pairs.empty())
132 if (notify && m_callback)
133 m_callback(*this, m_callback_baton);
136 bool PathMappingList::RemapPath(const ConstString &path,
137 ConstString &new_path) const {
138 const char *path_cstr = path.GetCString();
139 // CLEANUP: Convert this function to use StringRefs internally instead
144 const_iterator pos, end = m_pairs.end();
145 for (pos = m_pairs.begin(); pos != end; ++pos) {
146 const size_t prefixLen = pos->first.GetLength();
148 if (::strncmp(pos->first.GetCString(), path_cstr, prefixLen) == 0) {
149 std::string new_path_str(pos->second.GetCString());
150 new_path_str.append(path.GetCString() + prefixLen);
151 new_path.SetCString(new_path_str.c_str());
158 bool PathMappingList::RemapPath(llvm::StringRef path,
159 std::string &new_path) const {
160 if (m_pairs.empty() || path.empty())
163 const_iterator pos, end = m_pairs.end();
164 for (pos = m_pairs.begin(); pos != end; ++pos) {
165 if (!path.consume_front(pos->first.GetStringRef()))
168 new_path = pos->second.GetStringRef();
169 new_path.append(path);
175 bool PathMappingList::ReverseRemapPath(const ConstString &path,
176 ConstString &new_path) const {
177 const char *path_cstr = path.GetCString();
181 for (const auto &it : m_pairs) {
182 // FIXME: This should be using FileSpec API's to do the path appending.
183 const size_t prefixLen = it.second.GetLength();
184 if (::strncmp(it.second.GetCString(), path_cstr, prefixLen) == 0) {
185 std::string new_path_str(it.first.GetCString());
186 new_path_str.append(path.GetCString() + prefixLen);
187 new_path.SetCString(new_path_str.c_str());
194 bool PathMappingList::FindFile(const FileSpec &orig_spec,
195 FileSpec &new_spec) const {
196 if (!m_pairs.empty()) {
197 char orig_path[PATH_MAX];
198 const size_t orig_path_len =
199 orig_spec.GetPath(orig_path, sizeof(orig_path));
200 if (orig_path_len > 0) {
201 const_iterator pos, end = m_pairs.end();
202 for (pos = m_pairs.begin(); pos != end; ++pos) {
203 const size_t prefix_len = pos->first.GetLength();
205 if (orig_path_len >= prefix_len) {
206 if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) {
207 new_spec.SetFile(pos->second.GetCString(), false);
208 new_spec.AppendPathComponent(orig_path + prefix_len);
209 if (new_spec.Exists())
220 bool PathMappingList::Replace(const ConstString &path,
221 const ConstString &new_path, bool notify) {
222 uint32_t idx = FindIndexForPath(path);
223 if (idx < m_pairs.size()) {
225 m_pairs[idx].second = new_path;
226 if (notify && m_callback)
227 m_callback(*this, m_callback_baton);
233 bool PathMappingList::Remove(const ConstString &path, bool notify) {
234 iterator pos = FindIteratorForPath(path);
235 if (pos != m_pairs.end()) {
238 if (notify && m_callback)
239 m_callback(*this, m_callback_baton);
245 PathMappingList::const_iterator
246 PathMappingList::FindIteratorForPath(const ConstString &path) const {
248 const_iterator begin = m_pairs.begin();
249 const_iterator end = m_pairs.end();
251 for (pos = begin; pos != end; ++pos) {
252 if (pos->first == path)
258 PathMappingList::iterator
259 PathMappingList::FindIteratorForPath(const ConstString &path) {
261 iterator begin = m_pairs.begin();
262 iterator end = m_pairs.end();
264 for (pos = begin; pos != end; ++pos) {
265 if (pos->first == path)
271 bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
272 ConstString &new_path) const {
273 if (idx < m_pairs.size()) {
274 path = m_pairs[idx].first;
275 new_path = m_pairs[idx].second;
281 uint32_t PathMappingList::FindIndexForPath(const ConstString &path) const {
283 const_iterator begin = m_pairs.begin();
284 const_iterator end = m_pairs.end();
286 for (pos = begin; pos != end; ++pos) {
287 if (pos->first == path)
288 return std::distance(begin, pos);