PathMappingList.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef LLDB_TARGET_PATHMAPPINGLIST_H
  9. #define LLDB_TARGET_PATHMAPPINGLIST_H
  10. #include <map>
  11. #include <vector>
  12. #include "lldb/Utility/ConstString.h"
  13. #include "lldb/Utility/Status.h"
  14. namespace lldb_private {
  15. class PathMappingList {
  16. public:
  17. typedef void (*ChangedCallback)(const PathMappingList &path_list,
  18. void *baton);
  19. // Constructors and Destructors
  20. PathMappingList();
  21. PathMappingList(ChangedCallback callback, void *callback_baton);
  22. PathMappingList(const PathMappingList &rhs);
  23. ~PathMappingList();
  24. const PathMappingList &operator=(const PathMappingList &rhs);
  25. void Append(ConstString path, ConstString replacement,
  26. bool notify);
  27. void Append(const PathMappingList &rhs, bool notify);
  28. void Clear(bool notify);
  29. // By default, dump all pairs.
  30. void Dump(Stream *s, int pair_index = -1);
  31. bool IsEmpty() const { return m_pairs.empty(); }
  32. size_t GetSize() const { return m_pairs.size(); }
  33. bool GetPathsAtIndex(uint32_t idx, ConstString &path,
  34. ConstString &new_path) const;
  35. void Insert(ConstString path, ConstString replacement,
  36. uint32_t insert_idx, bool notify);
  37. bool Remove(size_t index, bool notify);
  38. bool Remove(ConstString path, bool notify);
  39. bool Replace(ConstString path, ConstString replacement,
  40. bool notify);
  41. bool Replace(ConstString path, ConstString replacement,
  42. uint32_t index, bool notify);
  43. bool RemapPath(ConstString path, ConstString &new_path) const;
  44. /// Remaps a source file given \a path into \a new_path.
  45. ///
  46. /// Remaps \a path if any source remappings match. This function
  47. /// does NOT stat the file system so it can be used in tight loops
  48. /// where debug info is being parsed.
  49. ///
  50. /// \param[in] path
  51. /// The original source file path to try and remap.
  52. ///
  53. /// \param[out] new_path
  54. /// The newly remapped filespec that is may or may not exist.
  55. ///
  56. /// \return
  57. /// /b true if \a path was successfully located and \a new_path
  58. /// is filled in with a new source path, \b false otherwise.
  59. bool RemapPath(llvm::StringRef path, std::string &new_path) const;
  60. bool RemapPath(const char *, std::string &) const = delete;
  61. bool ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const;
  62. /// Finds a source file given a file spec using the path remappings.
  63. ///
  64. /// Tries to resolve \a orig_spec by checking the path remappings.
  65. /// It makes sure the file exists by checking with the file system,
  66. /// so this call can be expensive if the remappings are on a network
  67. /// or are even on the local file system, so use this function
  68. /// sparingly (not in a tight debug info parsing loop).
  69. ///
  70. /// \param[in] orig_spec
  71. /// The original source file path to try and remap.
  72. ///
  73. /// \param[out] new_spec
  74. /// The newly remapped filespec that is guaranteed to exist.
  75. ///
  76. /// \return
  77. /// /b true if \a orig_spec was successfully located and
  78. /// \a new_spec is filled in with an existing file spec,
  79. /// \b false otherwise.
  80. bool FindFile(const FileSpec &orig_spec, FileSpec &new_spec) const;
  81. uint32_t FindIndexForPath(ConstString path) const;
  82. uint32_t GetModificationID() const { return m_mod_id; }
  83. protected:
  84. typedef std::pair<ConstString, ConstString> pair;
  85. typedef std::vector<pair> collection;
  86. typedef collection::iterator iterator;
  87. typedef collection::const_iterator const_iterator;
  88. iterator FindIteratorForPath(ConstString path);
  89. const_iterator FindIteratorForPath(ConstString path) const;
  90. collection m_pairs;
  91. ChangedCallback m_callback;
  92. void *m_callback_baton;
  93. uint32_t m_mod_id; // Incremented anytime anything is added or removed.
  94. };
  95. } // namespace lldb_private
  96. #endif // LLDB_TARGET_PATHMAPPINGLIST_H