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 LLDB_TARGET_PATHMAPPINGLIST_H
10 #define LLDB_TARGET_PATHMAPPINGLIST_H
11 
12 #include <map>
13 #include <vector>
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/Utility/Status.h"
16 
17 namespace lldb_private {
18 
19 class PathMappingList {
20 public:
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(llvm::StringRef path, llvm::StringRef replacement, bool notify);
36 
37   void Append(const PathMappingList &rhs, bool notify);
38 
39   void Clear(bool notify);
40 
41   // By default, dump all pairs.
42   void Dump(Stream *s, int pair_index = -1);
43 
44   bool IsEmpty() const { return m_pairs.empty(); }
45 
46   size_t GetSize() const { return m_pairs.size(); }
47 
48   bool GetPathsAtIndex(uint32_t idx, ConstString &path,
49                        ConstString &new_path) const;
50 
51   void Insert(llvm::StringRef path, llvm::StringRef replacement,
52               uint32_t insert_idx, bool notify);
53 
54   bool Remove(size_t index, bool notify);
55 
56   bool Remove(ConstString path, bool notify);
57 
58   bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify);
59 
60   bool Replace(llvm::StringRef path, llvm::StringRef replacement,
61                uint32_t index, bool notify);
62   bool RemapPath(ConstString path, ConstString &new_path) const;
63 
64   /// Remaps a source file given \a path into \a new_path.
65   ///
66   /// Remaps \a path if any source remappings match. This function
67   /// does NOT stat the file system so it can be used in tight loops
68   /// where debug info is being parsed.
69   ///
70   /// \param[in] path
71   ///     The original source file path to try and remap.
72   ///
73   /// \param[in] only_if_exists
74   ///     If \b true, besides matching \p path with the remapping rules, this
75   ///     tries to check with the filesystem that the remapped file exists. If
76   ///     no valid file is found, \b None is returned. This might be expensive,
77   ///     specially on a network.
78   ///
79   ///     If \b false, then the existence of the returned remapping is not
80   ///     checked.
81   ///
82   /// \return
83   ///     The remapped filespec that may or may not exist on disk.
84   llvm::Optional<FileSpec> RemapPath(llvm::StringRef path,
85                                      bool only_if_exists = false) const;
86   bool RemapPath(const char *, std::string &) const = delete;
87 
88   bool ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const;
89 
90   /// Finds a source file given a file spec using the path remappings.
91   ///
92   /// Tries to resolve \a orig_spec by checking the path remappings.
93   /// It makes sure the file exists by checking with the file system,
94   /// so this call can be expensive if the remappings are on a network
95   /// or are even on the local file system, so use this function
96   /// sparingly (not in a tight debug info parsing loop).
97   ///
98   /// \param[in] orig_spec
99   ///     The original source file path to try and remap.
100   ///
101   /// \return
102   ///     The newly remapped filespec that is guaranteed to exist.
103   llvm::Optional<FileSpec> FindFile(const FileSpec &orig_spec) const;
104 
105   uint32_t FindIndexForPath(llvm::StringRef path) const;
106 
107   uint32_t GetModificationID() const { return m_mod_id; }
108 
109 protected:
110   typedef std::pair<ConstString, ConstString> pair;
111   typedef std::vector<pair> collection;
112   typedef collection::iterator iterator;
113   typedef collection::const_iterator const_iterator;
114 
115   iterator FindIteratorForPath(ConstString path);
116 
117   const_iterator FindIteratorForPath(ConstString path) const;
118 
119   collection m_pairs;
120   ChangedCallback m_callback = nullptr;
121   void *m_callback_baton = nullptr;
122   uint32_t m_mod_id = 0; // Incremented anytime anything is added or removed.
123 };
124 
125 } // namespace lldb_private
126 
127 #endif // LLDB_TARGET_PATHMAPPINGLIST_H
128