1//===-- PathMappingList.h ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_PathMappingList_h_
10#define liblldb_PathMappingList_h_
11
12#include <map>
13#include <vector>
14#include "lldb/Utility/ConstString.h"
15#include "lldb/Utility/Status.h"
16
17namespace lldb_private {
18
19class PathMappingList {
20public:
21  typedef void (*ChangedCallback)(const PathMappingList &path_list,
22                                  void *baton);
23
24  // Constructors and Destructors
25  PathMappingList();
26
27  PathMappingList(ChangedCallback callback, void *callback_baton);
28
29  PathMappingList(const PathMappingList &rhs);
30
31  ~PathMappingList();
32
33  const PathMappingList &operator=(const PathMappingList &rhs);
34
35  void Append(ConstString path, ConstString replacement,
36              bool notify);
37
38  void Append(const PathMappingList &rhs, bool notify);
39
40  void Clear(bool notify);
41
42  // By default, dump all pairs.
43  void Dump(Stream *s, int pair_index = -1);
44
45  bool IsEmpty() const { return m_pairs.empty(); }
46
47  size_t GetSize() const { return m_pairs.size(); }
48
49  bool GetPathsAtIndex(uint32_t idx, ConstString &path,
50                       ConstString &new_path) const;
51
52  void Insert(ConstString path, ConstString replacement,
53              uint32_t insert_idx, bool notify);
54
55  bool Remove(size_t index, bool notify);
56
57  bool Remove(ConstString path, bool notify);
58
59  bool Replace(ConstString path, ConstString replacement,
60               bool notify);
61
62  bool Replace(ConstString path, ConstString replacement,
63               uint32_t index, bool notify);
64  bool RemapPath(ConstString path, ConstString &new_path) const;
65
66  /// Remaps a source file given \a path into \a new_path.
67  ///
68  /// Remaps \a path if any source remappings match. This function
69  /// does NOT stat the file system so it can be used in tight loops
70  /// where debug info is being parsed.
71  ///
72  /// \param[in] path
73  ///     The original source file path to try and remap.
74  ///
75  /// \param[out] new_path
76  ///     The newly remapped filespec that is may or may not exist.
77  ///
78  /// \return
79  ///     /b true if \a path was successfully located and \a new_path
80  ///     is filled in with a new source path, \b false otherwise.
81  bool RemapPath(llvm::StringRef path, std::string &new_path) const;
82  bool RemapPath(const char *, std::string &) const = delete;
83
84  bool ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const;
85
86  /// Finds a source file given a file spec using the path remappings.
87  ///
88  /// Tries to resolve \a orig_spec by checking the path remappings.
89  /// It makes sure the file exists by checking with the file system,
90  /// so this call can be expensive if the remappings are on a network
91  /// or are even on the local file system, so use this function
92  /// sparingly (not in a tight debug info parsing loop).
93  ///
94  /// \param[in] orig_spec
95  ///     The original source file path to try and remap.
96  ///
97  /// \param[out] new_spec
98  ///     The newly remapped filespec that is guaranteed to exist.
99  ///
100  /// \return
101  ///     /b true if \a orig_spec was successfully located and
102  ///     \a new_spec is filled in with an existing file spec,
103  ///     \b false otherwise.
104  bool FindFile(const FileSpec &orig_spec, FileSpec &new_spec) const;
105
106  uint32_t FindIndexForPath(ConstString path) const;
107
108  uint32_t GetModificationID() const { return m_mod_id; }
109
110protected:
111  typedef std::pair<ConstString, ConstString> pair;
112  typedef std::vector<pair> collection;
113  typedef collection::iterator iterator;
114  typedef collection::const_iterator const_iterator;
115
116  iterator FindIteratorForPath(ConstString path);
117
118  const_iterator FindIteratorForPath(ConstString path) const;
119
120  collection m_pairs;
121  ChangedCallback m_callback;
122  void *m_callback_baton;
123  uint32_t m_mod_id; // Incremented anytime anything is added or removed.
124};
125
126} // namespace lldb_private
127
128#endif // liblldb_PathMappingList_h_
129