LLDB  mainline
PathMappingList.h
Go to the documentation of this file.
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 
13 #include "lldb/Utility/Status.h"
14 #include "llvm/Support/JSON.h"
15 #include <map>
16 #include <optional>
17 #include <vector>
18 
19 namespace lldb_private {
20 
22 public:
23  typedef void (*ChangedCallback)(const PathMappingList &path_list,
24  void *baton);
25 
26  // Constructors and Destructors
28 
29  PathMappingList(ChangedCallback callback, void *callback_baton);
30 
31  PathMappingList(const PathMappingList &rhs);
32 
34 
35  const PathMappingList &operator=(const PathMappingList &rhs);
36 
37  void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify);
38 
39  void Append(const PathMappingList &rhs, bool notify);
40 
41  /// Append <path, replacement> pair without duplication.
42  /// \return whether appending suceeds without duplication or not.
43  bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement,
44  bool notify);
45 
46  void Clear(bool notify);
47 
48  // By default, dump all pairs.
49  void Dump(Stream *s, int pair_index = -1);
50 
51  llvm::json::Value ToJSON();
52 
53  bool IsEmpty() const { return m_pairs.empty(); }
54 
55  size_t GetSize() const { return m_pairs.size(); }
56 
57  bool GetPathsAtIndex(uint32_t idx, ConstString &path,
58  ConstString &new_path) const;
59 
60  void Insert(llvm::StringRef path, llvm::StringRef replacement,
61  uint32_t insert_idx, bool notify);
62 
63  bool Remove(size_t index, bool notify);
64 
65  bool Remove(ConstString path, bool notify);
66 
67  bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify);
68 
69  bool Replace(llvm::StringRef path, llvm::StringRef replacement,
70  uint32_t index, bool notify);
71  bool RemapPath(ConstString path, ConstString &new_path) const;
72 
73  /// Remaps a source file given \a path into \a new_path.
74  ///
75  /// Remaps \a path if any source remappings match. This function
76  /// does NOT stat the file system so it can be used in tight loops
77  /// where debug info is being parsed.
78  ///
79  /// \param[in] path
80  /// The original source file path to try and remap.
81  ///
82  /// \param[in] only_if_exists
83  /// If \b true, besides matching \p path with the remapping rules, this
84  /// tries to check with the filesystem that the remapped file exists. If
85  /// no valid file is found, \b std::nullopt is returned. This might be
86  /// expensive, specially on a network.
87  ///
88  /// If \b false, then the existence of the returned remapping is not
89  /// checked.
90  ///
91  /// \return
92  /// The remapped filespec that may or may not exist on disk.
93  std::optional<FileSpec> RemapPath(llvm::StringRef path,
94  bool only_if_exists = false) const;
95  bool RemapPath(const char *, std::string &) const = delete;
96 
97  /// Perform reverse source path remap for input \a file.
98  /// Source maps contains a list of <from_original_path, to_new_path> mappings.
99  /// Reverse remap means locating a matching entry prefix using "to_new_path"
100  /// part and replacing it with "from_original_path" part if found.
101  ///
102  /// \param[in] file
103  /// The source path to reverse remap.
104  /// \param[in] fixed
105  /// The reversed mapped new path.
106  ///
107  /// \return
108  /// std::nullopt if no remapping happens, otherwise, the matching source
109  /// map entry's ""to_new_pathto"" part (which is the prefix of \a file) is
110  /// returned.
111  std::optional<llvm::StringRef> ReverseRemapPath(const FileSpec &file,
112  FileSpec &fixed) const;
113 
114  /// Finds a source file given a file spec using the path remappings.
115  ///
116  /// Tries to resolve \a orig_spec by checking the path remappings.
117  /// It makes sure the file exists by checking with the file system,
118  /// so this call can be expensive if the remappings are on a network
119  /// or are even on the local file system, so use this function
120  /// sparingly (not in a tight debug info parsing loop).
121  ///
122  /// \param[in] orig_spec
123  /// The original source file path to try and remap.
124  ///
125  /// \return
126  /// The newly remapped filespec that is guaranteed to exist.
127  std::optional<FileSpec> FindFile(const FileSpec &orig_spec) const;
128 
129  uint32_t FindIndexForPath(llvm::StringRef path) const;
130 
131  uint32_t GetModificationID() const { return m_mod_id; }
132 
133 protected:
134  typedef std::pair<ConstString, ConstString> pair;
135  typedef std::vector<pair> collection;
136  typedef collection::iterator iterator;
137  typedef collection::const_iterator const_iterator;
138 
140 
142 
145  void *m_callback_baton = nullptr;
146  uint32_t m_mod_id = 0; // Incremented anytime anything is added or removed.
147 };
148 
149 } // namespace lldb_private
150 
151 #endif // LLDB_TARGET_PATHMAPPINGLIST_H
lldb_private::PathMappingList::iterator
collection::iterator iterator
Definition: PathMappingList.h:136
lldb_private::PathMappingList::const_iterator
collection::const_iterator const_iterator
Definition: PathMappingList.h:137
lldb_private::PathMappingList::GetPathsAtIndex
bool GetPathsAtIndex(uint32_t idx, ConstString &path, ConstString &new_path) const
Definition: PathMappingList.cpp:304
lldb_private::PathMappingList::FindFile
std::optional< FileSpec > FindFile(const FileSpec &orig_spec) const
Finds a source file given a file spec using the path remappings.
Definition: PathMappingList.cpp:242
lldb_private::PathMappingList::collection
std::vector< pair > collection
Definition: PathMappingList.h:135
lldb_private::PathMappingList::GetSize
size_t GetSize() const
Definition: PathMappingList.h:55
lldb_private::PathMappingList::AppendUnique
bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Append <path, replacement> pair without duplication.
Definition: PathMappingList.cpp:80
lldb_private::PathMappingList::FindIndexForPath
uint32_t FindIndexForPath(llvm::StringRef path) const
Definition: PathMappingList.cpp:314
lldb_private::Stream
Definition: Stream.h:28
lldb_private::PathMappingList::Insert
void Insert(llvm::StringRef path, llvm::StringRef replacement, uint32_t insert_idx, bool notify)
Definition: PathMappingList.cpp:93
lldb_private::PathMappingList::~PathMappingList
~PathMappingList()
lldb_private::PathMappingList::PathMappingList
PathMappingList()
Definition: PathMappingList.cpp:41
lldb_private::PathMappingList::ChangedCallback
void(* ChangedCallback)(const PathMappingList &path_list, void *baton)
Definition: PathMappingList.h:23
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::PathMappingList::m_mod_id
uint32_t m_mod_id
Definition: PathMappingList.h:146
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::PathMappingList::ToJSON
llvm::json::Value ToJSON()
Definition: PathMappingList.cpp:148
lldb_private::PathMappingList::Replace
bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Definition: PathMappingList.cpp:253
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::PathMappingList::Append
void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Definition: PathMappingList.cpp:61
lldb_private::PathMappingList::m_pairs
collection m_pairs
Definition: PathMappingList.h:143
lldb_private::PathMappingList::operator=
const PathMappingList & operator=(const PathMappingList &rhs)
Definition: PathMappingList.cpp:49
uint32_t
lldb_private::PathMappingList
Definition: PathMappingList.h:21
lldb_private::PathMappingList::m_callback
ChangedCallback m_callback
Definition: PathMappingList.h:144
lldb_private::PathMappingList::GetModificationID
uint32_t GetModificationID() const
Definition: PathMappingList.h:131
lldb_private::PathMappingList::pair
std::pair< ConstString, ConstString > pair
Definition: PathMappingList.h:134
lldb_private::PathMappingList::ReverseRemapPath
std::optional< llvm::StringRef > ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const
Perform reverse source path remap for input file.
Definition: PathMappingList.cpp:224
Status.h
lldb_private::PathMappingList::m_callback_baton
void * m_callback_baton
Definition: PathMappingList.h:145
lldb_private::PathMappingList::IsEmpty
bool IsEmpty() const
Definition: PathMappingList.h:53
lldb_private::PathMappingList::Dump
void Dump(Stream *s, int pair_index=-1)
Definition: PathMappingList.cpp:132
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::PathMappingList::FindIteratorForPath
iterator FindIteratorForPath(ConstString path)
Definition: PathMappingList.cpp:292
ConstString.h
lldb_private::PathMappingList::Remove
bool Remove(size_t index, bool notify)
Definition: PathMappingList.cpp:118
lldb_private::PathMappingList::Clear
void Clear(bool notify)
Definition: PathMappingList.cpp:158
lldb_private::PathMappingList::RemapPath
bool RemapPath(ConstString path, ConstString &new_path) const
Definition: PathMappingList.cpp:166