blob: 4fbaee98da6157e18c511c3bba1c867e0529db38 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11// C++ Includes
Eugene Zelenko9394d7722016-02-18 00:10:17 +000012#include <climits>
13#include <cstring>
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// Other libraries and framework includes
Greg Claytond804d282012-03-15 21:01:31 +000016// Project includes
Zachary Turnerf343968f2016-08-09 23:06:08 +000017#include "lldb/Host/PosixApi.h"
Eli Friedman48862d42010-06-09 09:32:42 +000018#include "lldb/Target/PathMappingList.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/Error.h"
Zachary Turner5713a052017-03-22 18:40:07 +000020#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000021#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------
27// PathMappingList constructor
28//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000029PathMappingList::PathMappingList()
30 : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
Greg Claytond804d282012-03-15 21:01:31 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
33 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
34 m_mod_id(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036PathMappingList::PathMappingList(const PathMappingList &rhs)
37 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
38 m_mod_id(0) {}
Greg Clayton7e14f912011-04-23 02:04:55 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
41 if (this != &rhs) {
42 m_pairs = rhs.m_pairs;
43 m_callback = nullptr;
44 m_callback_baton = nullptr;
45 m_mod_id = rhs.m_mod_id;
46 }
47 return *this;
Greg Clayton7e14f912011-04-23 02:04:55 +000048}
49
Eugene Zelenko9394d7722016-02-18 00:10:17 +000050PathMappingList::~PathMappingList() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052void PathMappingList::Append(const ConstString &path,
53 const ConstString &replacement, bool notify) {
54 ++m_mod_id;
55 m_pairs.push_back(pair(path, replacement));
56 if (notify && m_callback)
57 m_callback(*this, m_callback_baton);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
61 ++m_mod_id;
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);
68 }
Greg Claytond804d282012-03-15 21:01:31 +000069}
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071void PathMappingList::Insert(const ConstString &path,
72 const ConstString &replacement, uint32_t index,
73 bool notify) {
74 ++m_mod_id;
75 iterator insert_iter;
76 if (index >= m_pairs.size())
77 insert_iter = m_pairs.end();
78 else
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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083}
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085bool PathMappingList::Replace(const ConstString &path,
86 const ConstString &replacement, uint32_t index,
87 bool notify) {
88 iterator insert_iter;
89 if (index >= m_pairs.size())
90 return false;
91 ++m_mod_id;
92 m_pairs[index] = pair(path, replacement);
93 if (notify && m_callback)
94 m_callback(*this, m_callback_baton);
95 return true;
Greg Clayton67cc0632012-08-22 17:17:09 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098bool PathMappingList::Remove(size_t index, bool notify) {
99 if (index >= m_pairs.size())
100 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 ++m_mod_id;
103 iterator iter = m_pairs.begin() + index;
104 m_pairs.erase(iter);
105 if (notify && m_callback)
106 m_callback(*this, m_callback_baton);
107 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108}
109
Johnny Chen64bab482011-12-12 21:59:28 +0000110// For clients which do not need the pair index dumped, pass a pair_index >= 0
111// to only dump the indicated pair.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112void PathMappingList::Dump(Stream *s, int pair_index) {
113 unsigned int numPairs = m_pairs.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (pair_index < 0) {
116 unsigned int index;
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());
121 } else {
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());
125 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126}
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128void PathMappingList::Clear(bool notify) {
129 if (!m_pairs.empty())
130 ++m_mod_id;
131 m_pairs.clear();
132 if (notify && m_callback)
133 m_callback(*this, m_callback_baton);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136bool PathMappingList::RemapPath(const ConstString &path,
137 ConstString &new_path) const {
138 const char *path_cstr = path.GetCString();
Zachary Turnera498f0e2016-09-23 18:42:38 +0000139 // CLEANUP: Convert this function to use StringRefs internally instead
140 // of raw c-strings.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 if (!path_cstr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143
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();
147
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());
152 return true;
153 }
154 }
155 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
Greg Clayton7e14f912011-04-23 02:04:55 +0000157
Zachary Turnera498f0e2016-09-23 18:42:38 +0000158bool PathMappingList::RemapPath(llvm::StringRef path,
159 std::string &new_path) const {
160 if (m_pairs.empty() || path.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 return false;
Greg Claytonf9be6932012-03-19 22:22:41 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 const_iterator pos, end = m_pairs.end();
164 for (pos = m_pairs.begin(); pos != end; ++pos) {
Zachary Turnera498f0e2016-09-23 18:42:38 +0000165 if (!path.consume_front(pos->first.GetStringRef()))
166 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167
Zachary Turnera498f0e2016-09-23 18:42:38 +0000168 new_path = pos->second.GetStringRef();
169 new_path.append(path);
170 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 }
172 return false;
173}
174
175bool PathMappingList::ReverseRemapPath(const ConstString &path,
176 ConstString &new_path) const {
177 const char *path_cstr = path.GetCString();
178 if (!path_cstr)
179 return false;
180
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());
188 return true;
189 }
190 }
191 return false;
192}
193
194bool 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) {
Greg Claytonf9be6932012-03-19 22:22:41 +0000203 const size_t prefix_len = pos->first.GetLength();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204
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())
210 return true;
211 }
Greg Claytonf9be6932012-03-19 22:22:41 +0000212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 }
Greg Claytonf9be6932012-03-19 22:22:41 +0000214 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 }
216 new_spec.Clear();
217 return false;
Greg Claytonf9be6932012-03-19 22:22:41 +0000218}
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220bool PathMappingList::Replace(const ConstString &path,
221 const ConstString &new_path, bool notify) {
222 uint32_t idx = FindIndexForPath(path);
223 if (idx < m_pairs.size()) {
224 ++m_mod_id;
225 m_pairs[idx].second = new_path;
226 if (notify && m_callback)
227 m_callback(*this, m_callback_baton);
228 return true;
229 }
230 return false;
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233bool PathMappingList::Remove(const ConstString &path, bool notify) {
234 iterator pos = FindIteratorForPath(path);
235 if (pos != m_pairs.end()) {
236 ++m_mod_id;
237 m_pairs.erase(pos);
238 if (notify && m_callback)
239 m_callback(*this, m_callback_baton);
240 return true;
241 }
242 return false;
Greg Clayton7e14f912011-04-23 02:04:55 +0000243}
244
245PathMappingList::const_iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246PathMappingList::FindIteratorForPath(const ConstString &path) const {
247 const_iterator pos;
248 const_iterator begin = m_pairs.begin();
249 const_iterator end = m_pairs.end();
250
251 for (pos = begin; pos != end; ++pos) {
252 if (pos->first == path)
253 break;
254 }
255 return pos;
Greg Clayton7e14f912011-04-23 02:04:55 +0000256}
257
258PathMappingList::iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259PathMappingList::FindIteratorForPath(const ConstString &path) {
260 iterator pos;
261 iterator begin = m_pairs.begin();
262 iterator end = m_pairs.end();
263
264 for (pos = begin; pos != end; ++pos) {
265 if (pos->first == path)
266 break;
267 }
268 return pos;
Greg Clayton7e14f912011-04-23 02:04:55 +0000269}
270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271bool 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;
276 return true;
277 }
278 return false;
Greg Clayton7e14f912011-04-23 02:04:55 +0000279}
280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281uint32_t PathMappingList::FindIndexForPath(const ConstString &path) const {
282 const_iterator pos;
283 const_iterator begin = m_pairs.begin();
284 const_iterator end = m_pairs.end();
285
286 for (pos = begin; pos != end; ++pos) {
287 if (pos->first == path)
288 return std::distance(begin, pos);
289 }
290 return UINT32_MAX;
Greg Clayton7e14f912011-04-23 02:04:55 +0000291}