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