1254721Semaste//===-- PathMappingList.h ---------------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#ifndef liblldb_PathMappingList_h_
10254721Semaste#define liblldb_PathMappingList_h_
11254721Semaste
12254721Semaste#include <map>
13254721Semaste#include <vector>
14321369Sdim#include "lldb/Utility/ConstString.h"
15321369Sdim#include "lldb/Utility/Status.h"
16254721Semaste
17254721Semastenamespace lldb_private {
18254721Semaste
19314564Sdimclass PathMappingList {
20254721Semastepublic:
21314564Sdim  typedef void (*ChangedCallback)(const PathMappingList &path_list,
22314564Sdim                                  void *baton);
23254721Semaste
24314564Sdim  // Constructors and Destructors
25314564Sdim  PathMappingList();
26254721Semaste
27314564Sdim  PathMappingList(ChangedCallback callback, void *callback_baton);
28254721Semaste
29314564Sdim  PathMappingList(const PathMappingList &rhs);
30254721Semaste
31314564Sdim  ~PathMappingList();
32254721Semaste
33314564Sdim  const PathMappingList &operator=(const PathMappingList &rhs);
34254721Semaste
35353358Sdim  void Append(ConstString path, ConstString replacement,
36314564Sdim              bool notify);
37254721Semaste
38314564Sdim  void Append(const PathMappingList &rhs, bool notify);
39254721Semaste
40314564Sdim  void Clear(bool notify);
41254721Semaste
42314564Sdim  // By default, dump all pairs.
43314564Sdim  void Dump(Stream *s, int pair_index = -1);
44254721Semaste
45314564Sdim  bool IsEmpty() const { return m_pairs.empty(); }
46254721Semaste
47314564Sdim  size_t GetSize() const { return m_pairs.size(); }
48254721Semaste
49314564Sdim  bool GetPathsAtIndex(uint32_t idx, ConstString &path,
50314564Sdim                       ConstString &new_path) const;
51254721Semaste
52353358Sdim  void Insert(ConstString path, ConstString replacement,
53314564Sdim              uint32_t insert_idx, bool notify);
54254721Semaste
55314564Sdim  bool Remove(size_t index, bool notify);
56254721Semaste
57353358Sdim  bool Remove(ConstString path, bool notify);
58254721Semaste
59353358Sdim  bool Replace(ConstString path, ConstString replacement,
60314564Sdim               bool notify);
61254721Semaste
62353358Sdim  bool Replace(ConstString path, ConstString replacement,
63314564Sdim               uint32_t index, bool notify);
64353358Sdim  bool RemapPath(ConstString path, ConstString &new_path) const;
65254721Semaste
66314564Sdim  /// Remaps a source file given \a path into \a new_path.
67314564Sdim  ///
68314564Sdim  /// Remaps \a path if any source remappings match. This function
69314564Sdim  /// does NOT stat the file system so it can be used in tight loops
70314564Sdim  /// where debug info is being parsed.
71314564Sdim  ///
72353358Sdim  /// \param[in] path
73314564Sdim  ///     The original source file path to try and remap.
74314564Sdim  ///
75353358Sdim  /// \param[out] new_path
76314564Sdim  ///     The newly remapped filespec that is may or may not exist.
77314564Sdim  ///
78353358Sdim  /// \return
79314564Sdim  ///     /b true if \a path was successfully located and \a new_path
80314564Sdim  ///     is filled in with a new source path, \b false otherwise.
81314564Sdim  bool RemapPath(llvm::StringRef path, std::string &new_path) const;
82314564Sdim  bool RemapPath(const char *, std::string &) const = delete;
83254721Semaste
84341825Sdim  bool ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const;
85309124Sdim
86314564Sdim  /// Finds a source file given a file spec using the path remappings.
87314564Sdim  ///
88314564Sdim  /// Tries to resolve \a orig_spec by checking the path remappings.
89314564Sdim  /// It makes sure the file exists by checking with the file system,
90314564Sdim  /// so this call can be expensive if the remappings are on a network
91314564Sdim  /// or are even on the local file system, so use this function
92314564Sdim  /// sparingly (not in a tight debug info parsing loop).
93314564Sdim  ///
94353358Sdim  /// \param[in] orig_spec
95314564Sdim  ///     The original source file path to try and remap.
96314564Sdim  ///
97353358Sdim  /// \param[out] new_spec
98314564Sdim  ///     The newly remapped filespec that is guaranteed to exist.
99314564Sdim  ///
100353358Sdim  /// \return
101314564Sdim  ///     /b true if \a orig_spec was successfully located and
102314564Sdim  ///     \a new_spec is filled in with an existing file spec,
103314564Sdim  ///     \b false otherwise.
104314564Sdim  bool FindFile(const FileSpec &orig_spec, FileSpec &new_spec) const;
105254721Semaste
106353358Sdim  uint32_t FindIndexForPath(ConstString path) const;
107254721Semaste
108314564Sdim  uint32_t GetModificationID() const { return m_mod_id; }
109314564Sdim
110254721Semasteprotected:
111314564Sdim  typedef std::pair<ConstString, ConstString> pair;
112314564Sdim  typedef std::vector<pair> collection;
113314564Sdim  typedef collection::iterator iterator;
114314564Sdim  typedef collection::const_iterator const_iterator;
115254721Semaste
116353358Sdim  iterator FindIteratorForPath(ConstString path);
117254721Semaste
118353358Sdim  const_iterator FindIteratorForPath(ConstString path) const;
119314564Sdim
120314564Sdim  collection m_pairs;
121314564Sdim  ChangedCallback m_callback;
122314564Sdim  void *m_callback_baton;
123314564Sdim  uint32_t m_mod_id; // Incremented anytime anything is added or removed.
124254721Semaste};
125254721Semaste
126254721Semaste} // namespace lldb_private
127254721Semaste
128314564Sdim#endif // liblldb_PathMappingList_h_
129