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