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