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 12 #include <map> 13 #include <vector> 14 #include "lldb/Utility/ConstString.h" 15 #include "lldb/Utility/Status.h" 16 17 namespace lldb_private { 18 19 class PathMappingList { 20 public: 21 typedef void (*ChangedCallback)(const PathMappingList &path_list, 22 void *baton); 23 24 // Constructors and Destructors 25 PathMappingList(); 26 27 PathMappingList(ChangedCallback callback, void *callback_baton); 28 29 PathMappingList(const PathMappingList &rhs); 30 31 ~PathMappingList(); 32 33 const PathMappingList &operator=(const PathMappingList &rhs); 34 35 void Append(ConstString path, ConstString replacement, 36 bool notify); 37 38 void Append(const PathMappingList &rhs, bool notify); 39 40 void Clear(bool notify); 41 42 // By default, dump all pairs. 43 void Dump(Stream *s, int pair_index = -1); 44 45 bool IsEmpty() const { return m_pairs.empty(); } 46 47 size_t GetSize() const { return m_pairs.size(); } 48 49 bool GetPathsAtIndex(uint32_t idx, ConstString &path, 50 ConstString &new_path) const; 51 52 void Insert(ConstString path, ConstString replacement, 53 uint32_t insert_idx, bool notify); 54 55 bool Remove(size_t index, bool notify); 56 57 bool Remove(ConstString path, bool notify); 58 59 bool Replace(ConstString path, ConstString replacement, 60 bool notify); 61 62 bool Replace(ConstString path, ConstString replacement, 63 uint32_t index, bool notify); 64 bool RemapPath(ConstString path, ConstString &new_path) const; 65 66 /// Remaps a source file given \a path into \a new_path. 67 /// 68 /// Remaps \a path if any source remappings match. This function 69 /// does NOT stat the file system so it can be used in tight loops 70 /// where debug info is being parsed. 71 /// 72 /// \param[in] path 73 /// The original source file path to try and remap. 74 /// 75 /// \param[in] only_if_exists 76 /// If \b true, besides matching \p path with the remapping rules, this 77 /// tries to check with the filesystem that the remapped file exists. If 78 /// no valid file is found, \b None is returned. This might be expensive, 79 /// specially on a network. 80 /// 81 /// If \b false, then the existence of the returned remapping is not 82 /// checked. 83 /// 84 /// \return 85 /// The remapped filespec that may or may not exist on disk. 86 llvm::Optional<FileSpec> RemapPath(llvm::StringRef path, 87 bool only_if_exists = false) const; 88 bool RemapPath(const char *, std::string &) const = delete; 89 90 bool ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const; 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 /// \return 104 /// The newly remapped filespec that is guaranteed to exist. 105 llvm::Optional<FileSpec> FindFile(const FileSpec &orig_spec) const; 106 107 uint32_t FindIndexForPath(ConstString path) const; 108 109 uint32_t GetModificationID() const { return m_mod_id; } 110 111 protected: 112 typedef std::pair<ConstString, ConstString> pair; 113 typedef std::vector<pair> collection; 114 typedef collection::iterator iterator; 115 typedef collection::const_iterator const_iterator; 116 117 iterator FindIteratorForPath(ConstString path); 118 119 const_iterator FindIteratorForPath(ConstString path) const; 120 121 collection m_pairs; 122 ChangedCallback m_callback = nullptr; 123 void *m_callback_baton = nullptr; 124 uint32_t m_mod_id = 0; // Incremented anytime anything is added or removed. 125 }; 126 127 } // namespace lldb_private 128 129 #endif // LLDB_TARGET_PATHMAPPINGLIST_H 130