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,
88 if (index >= m_pairs.size())
91 m_pairs[index] = pair(path, replacement);
92 if (notify && m_callback)
93 m_callback(*this, m_callback_baton);
97 bool PathMappingList::Remove(size_t index, bool notify) {
98 if (index >= m_pairs.size())
102 iterator iter = m_pairs.begin() + index;
104 if (notify && m_callback)
105 m_callback(*this, m_callback_baton);
109 // For clients which do not need the pair index dumped, pass a pair_index >= 0
110 // to only dump the indicated pair.
111 void PathMappingList::Dump(Stream *s, int pair_index) {
112 unsigned int numPairs = m_pairs.size();
114 if (pair_index < 0) {
116 for (index = 0; index < numPairs; ++index)
117 s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
118 m_pairs[index].first.GetCString(),
119 m_pairs[index].second.GetCString());
121 if (static_cast<unsigned int>(pair_index) < numPairs)
122 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
123 m_pairs[pair_index].second.GetCString());
127 void PathMappingList::Clear(bool notify) {
128 if (!m_pairs.empty())
131 if (notify && m_callback)
132 m_callback(*this, m_callback_baton);
135 bool PathMappingList::RemapPath(const ConstString &path,
136 ConstString &new_path) const {
137 const char *path_cstr = path.GetCString();
138 // CLEANUP: Convert this function to use StringRefs internally instead
143 const_iterator pos, end = m_pairs.end();
144 for (pos = m_pairs.begin(); pos != end; ++pos) {
145 const size_t prefixLen = pos->first.GetLength();
147 if (::strncmp(pos->first.GetCString(), path_cstr, prefixLen) == 0) {
148 std::string new_path_str(pos->second.GetCString());
149 new_path_str.append(path.GetCString() + prefixLen);
150 new_path.SetCString(new_path_str.c_str());
157 bool PathMappingList::RemapPath(llvm::StringRef path,
158 std::string &new_path) const {
159 if (m_pairs.empty() || path.empty())
162 const_iterator pos, end = m_pairs.end();
163 for (pos = m_pairs.begin(); pos != end; ++pos) {
164 if (!path.consume_front(pos->first.GetStringRef()))
167 new_path = pos->second.GetStringRef();
168 new_path.append(path);
174 bool PathMappingList::ReverseRemapPath(const ConstString &path,
175 ConstString &new_path) const {
176 const char *path_cstr = path.GetCString();
180 for (const auto &it : m_pairs) {
181 // FIXME: This should be using FileSpec API's to do the path appending.
182 const size_t prefixLen = it.second.GetLength();
183 if (::strncmp(it.second.GetCString(), path_cstr, prefixLen) == 0) {
184 std::string new_path_str(it.first.GetCString());
185 new_path_str.append(path.GetCString() + prefixLen);
186 new_path.SetCString(new_path_str.c_str());
193 bool PathMappingList::FindFile(const FileSpec &orig_spec,
194 FileSpec &new_spec) const {
195 if (!m_pairs.empty()) {
196 char orig_path[PATH_MAX];
197 const size_t orig_path_len =
198 orig_spec.GetPath(orig_path, sizeof(orig_path));
199 if (orig_path_len > 0) {
200 const_iterator pos, end = m_pairs.end();
201 for (pos = m_pairs.begin(); pos != end; ++pos) {
202 const size_t prefix_len = pos->first.GetLength();
204 if (orig_path_len >= prefix_len) {
205 if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) {
206 new_spec.SetFile(pos->second.GetCString(), false);
207 new_spec.AppendPathComponent(orig_path + prefix_len);
208 if (new_spec.Exists())
219 bool PathMappingList::Replace(const ConstString &path,
220 const ConstString &new_path, bool notify) {
221 uint32_t idx = FindIndexForPath(path);
222 if (idx < m_pairs.size()) {
224 m_pairs[idx].second = new_path;
225 if (notify && m_callback)
226 m_callback(*this, m_callback_baton);
232 bool PathMappingList::Remove(const ConstString &path, bool notify) {
233 iterator pos = FindIteratorForPath(path);
234 if (pos != m_pairs.end()) {
237 if (notify && m_callback)
238 m_callback(*this, m_callback_baton);
244 PathMappingList::const_iterator
245 PathMappingList::FindIteratorForPath(const ConstString &path) const {
247 const_iterator begin = m_pairs.begin();
248 const_iterator end = m_pairs.end();
250 for (pos = begin; pos != end; ++pos) {
251 if (pos->first == path)
257 PathMappingList::iterator
258 PathMappingList::FindIteratorForPath(const ConstString &path) {
260 iterator begin = m_pairs.begin();
261 iterator end = m_pairs.end();
263 for (pos = begin; pos != end; ++pos) {
264 if (pos->first == path)
270 bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
271 ConstString &new_path) const {
272 if (idx < m_pairs.size()) {
273 path = m_pairs[idx].first;
274 new_path = m_pairs[idx].second;
280 uint32_t PathMappingList::FindIndexForPath(const ConstString &path) const {
282 const_iterator begin = m_pairs.begin();
283 const_iterator end = m_pairs.end();
285 for (pos = begin; pos != end; ++pos) {
286 if (pos->first == path)
287 return std::distance(begin, pos);