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