Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:53

0001 //===-- PathMappingList.h ---------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_TARGET_PATHMAPPINGLIST_H
0010 #define LLDB_TARGET_PATHMAPPINGLIST_H
0011 
0012 #include "lldb/Utility/ConstString.h"
0013 #include "lldb/Utility/Status.h"
0014 #include "llvm/Support/Error.h"
0015 #include "llvm/Support/JSON.h"
0016 #include <map>
0017 #include <mutex>
0018 #include <optional>
0019 #include <vector>
0020 
0021 namespace lldb_private {
0022 
0023 class PathMappingList {
0024 public:
0025   typedef void (*ChangedCallback)(const PathMappingList &path_list,
0026                                   void *baton);
0027 
0028   PathMappingList();
0029 
0030   PathMappingList(ChangedCallback callback, void *callback_baton);
0031 
0032   PathMappingList(const PathMappingList &rhs);
0033 
0034   ~PathMappingList();
0035 
0036   const PathMappingList &operator=(const PathMappingList &rhs);
0037 
0038   void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify);
0039 
0040   void Append(const PathMappingList &rhs, bool notify);
0041 
0042   /// Append <path, replacement> pair without duplication.
0043   /// \return whether appending suceeds without duplication or not.
0044   bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement,
0045                     bool notify);
0046 
0047   void Clear(bool notify);
0048 
0049   // By default, dump all pairs.
0050   void Dump(Stream *s, int pair_index = -1);
0051 
0052   llvm::json::Value ToJSON();
0053 
0054   bool IsEmpty() const {
0055     std::lock_guard<std::mutex> lock(m_pairs_mutex);
0056     return m_pairs.empty();
0057   }
0058 
0059   size_t GetSize() const {
0060     std::lock_guard<std::mutex> lock(m_pairs_mutex);
0061     return m_pairs.size();
0062   }
0063 
0064   bool GetPathsAtIndex(uint32_t idx, ConstString &path,
0065                        ConstString &new_path) const;
0066 
0067   void Insert(llvm::StringRef path, llvm::StringRef replacement,
0068               uint32_t insert_idx, bool notify);
0069 
0070   bool Remove(size_t index, bool notify);
0071 
0072   bool Remove(ConstString path, bool notify);
0073 
0074   bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify);
0075 
0076   bool Replace(llvm::StringRef path, llvm::StringRef replacement,
0077                uint32_t index, bool notify);
0078   bool RemapPath(ConstString path, ConstString &new_path) const;
0079 
0080   /// Remaps a source file given \a path into \a new_path.
0081   ///
0082   /// Remaps \a path if any source remappings match. This function
0083   /// does NOT stat the file system so it can be used in tight loops
0084   /// where debug info is being parsed.
0085   ///
0086   /// \param[in] path
0087   ///     The original source file path to try and remap.
0088   ///
0089   /// \param[in] only_if_exists
0090   ///     If \b true, besides matching \p path with the remapping rules, this
0091   ///     tries to check with the filesystem that the remapped file exists. If
0092   ///     no valid file is found, \b std::nullopt is returned. This might be
0093   ///     expensive, specially on a network.
0094   ///
0095   ///     If \b false, then the existence of the returned remapping is not
0096   ///     checked.
0097   ///
0098   /// \return
0099   ///     The remapped filespec that may or may not exist on disk.
0100   std::optional<FileSpec> RemapPath(llvm::StringRef path,
0101                                     bool only_if_exists = false) const;
0102   bool RemapPath(const char *, std::string &) const = delete;
0103 
0104   /// Perform reverse source path remap for input \a file.
0105   /// Source maps contains a list of <from_original_path, to_new_path> mappings.
0106   /// Reverse remap means locating a matching entry prefix using "to_new_path"
0107   /// part and replacing it with "from_original_path" part if found.
0108   ///
0109   /// \param[in] file
0110   ///     The source path to reverse remap.
0111   /// \param[in] fixed
0112   ///     The reversed mapped new path.
0113   ///
0114   /// \return
0115   ///     std::nullopt if no remapping happens, otherwise, the matching source
0116   ///     map entry's ""to_new_pathto"" part (which is the prefix of \a file) is
0117   ///     returned.
0118   std::optional<llvm::StringRef> ReverseRemapPath(const FileSpec &file,
0119                                                   FileSpec &fixed) const;
0120 
0121   /// Finds a source file given a file spec using the path remappings.
0122   ///
0123   /// Tries to resolve \a orig_spec by checking the path remappings.
0124   /// It makes sure the file exists by checking with the file system,
0125   /// so this call can be expensive if the remappings are on a network
0126   /// or are even on the local file system, so use this function
0127   /// sparingly (not in a tight debug info parsing loop).
0128   ///
0129   /// \param[in] orig_spec
0130   ///     The original source file path to try and remap.
0131   ///
0132   /// \return
0133   ///     The newly remapped filespec that is guaranteed to exist.
0134   std::optional<FileSpec> FindFile(const FileSpec &orig_spec) const;
0135 
0136   uint32_t GetModificationID() const {
0137     std::lock_guard<std::mutex> lock(m_pairs_mutex);
0138     return m_mod_id;
0139   }
0140 
0141 protected:
0142   typedef std::pair<ConstString, ConstString> pair;
0143   typedef std::vector<pair> collection;
0144   typedef collection::iterator iterator;
0145   typedef collection::const_iterator const_iterator;
0146 
0147   void AppendNoLock(llvm::StringRef path, llvm::StringRef replacement);
0148   uint32_t FindIndexForPathNoLock(llvm::StringRef path) const;
0149   void Notify(bool notify) const;
0150 
0151   iterator FindIteratorForPath(ConstString path);
0152 
0153   const_iterator FindIteratorForPath(ConstString path) const;
0154 
0155   collection m_pairs;
0156   mutable std::mutex m_pairs_mutex;
0157 
0158   ChangedCallback m_callback = nullptr;
0159   void *m_callback_baton = nullptr;
0160   mutable std::mutex m_callback_mutex;
0161 
0162   /// Incremented anytime anything is added to or removed from m_pairs. Also
0163   /// protected by m_pairs_mutex.
0164   uint32_t m_mod_id = 0;
0165 };
0166 
0167 } // namespace lldb_private
0168 
0169 #endif // LLDB_TARGET_PATHMAPPINGLIST_H