Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- FileSpecList.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_CORE_FILESPECLIST_H
0010 #define LLDB_CORE_FILESPECLIST_H
0011 
0012 #include "lldb/Utility/FileSpec.h"
0013 #include "lldb/Utility/SupportFile.h"
0014 #include "lldb/lldb-forward.h"
0015 
0016 #include <cstddef>
0017 #include <vector>
0018 
0019 namespace lldb_private {
0020 class Stream;
0021 
0022 /// A list of support files for a CompileUnit.
0023 class SupportFileList {
0024 public:
0025   SupportFileList(){};
0026   SupportFileList(const SupportFileList &) = delete;
0027   SupportFileList(SupportFileList &&other) = default;
0028 
0029   typedef std::vector<std::shared_ptr<SupportFile>> collection;
0030   typedef collection::const_iterator const_iterator;
0031   const_iterator begin() const { return m_files.begin(); }
0032   const_iterator end() const { return m_files.end(); }
0033 
0034   void Append(const FileSpec &file) {
0035     return Append(std::make_shared<SupportFile>(file));
0036   }
0037   void Append(std::shared_ptr<SupportFile> &&file) {
0038     m_files.push_back(std::move(file));
0039   }
0040   // FIXME: Only used by SymbolFilePDB. Replace with a DenseSet at call site.
0041   bool AppendIfUnique(const FileSpec &file);
0042   size_t GetSize() const { return m_files.size(); }
0043   const FileSpec &GetFileSpecAtIndex(size_t idx) const;
0044   lldb::SupportFileSP GetSupportFileAtIndex(size_t idx) const;
0045   size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
0046   /// Find a compatible file index.
0047   ///
0048   /// Find the index of a compatible file in the file spec list that matches \a
0049   /// file starting \a idx entries into the file spec list. A file is considered
0050   /// compatible if:
0051   /// - The file matches exactly (only filename if \a file has no directory)
0052   /// - If \a file is relative and any file in the list has this same suffix
0053   /// - If any file in the list is relative and the relative path is a suffix
0054   ///   of \a file
0055   ///
0056   /// This is used to implement better matching for setting breakpoints in
0057   /// source files where an IDE might specify a full path when setting the
0058   /// breakpoint and debug info contains relative paths, if a user specifies
0059   /// a relative path when setting a breakpoint.
0060   ///
0061   /// \param[in] idx
0062   ///     An index into the file list.
0063   ///
0064   /// \param[in] file
0065   ///     The file specification to search for.
0066   ///
0067   /// \param[in] realpath_prefixes
0068   ///     Paths that start with one of the prefixes in this list will be
0069   ///     realpath'ed to resolve any symlinks.
0070   ///
0071   /// \return
0072   ///     The index of the file that matches \a file if it is found,
0073   ///     else UINT32_MAX is returned.
0074   size_t
0075   FindCompatibleIndex(size_t idx, const FileSpec &file,
0076                       RealpathPrefixes *realpath_prefixes = nullptr) const;
0077 
0078   template <class... Args> void EmplaceBack(Args &&...args) {
0079     m_files.push_back(
0080         std::make_shared<SupportFile>(std::forward<Args>(args)...));
0081   }
0082 
0083 protected:
0084   collection m_files; ///< A collection of FileSpec objects.
0085 };
0086 
0087 /// \class FileSpecList FileSpecList.h "lldb/Utility/FileSpecList.h"
0088 /// A file collection class.
0089 ///
0090 /// A class that contains a mutable list of FileSpec objects.
0091 class FileSpecList {
0092 public:
0093   typedef std::vector<FileSpec> collection;
0094   typedef collection::const_iterator const_iterator;
0095 
0096   /// Default constructor.
0097   ///
0098   /// Initialize this object with an empty file list.
0099   FileSpecList();
0100 
0101   /// Copy constructor.
0102   FileSpecList(const FileSpecList &rhs) = default;
0103 
0104   /// Move constructor
0105   FileSpecList(FileSpecList &&rhs) = default;
0106 
0107   /// Initialize this object from a vector of FileSpecs
0108   FileSpecList(std::vector<FileSpec> &&rhs) : m_files(std::move(rhs)) {}
0109 
0110   /// Destructor.
0111   ~FileSpecList();
0112 
0113   /// Assignment operator.
0114   ///
0115   /// Replace the file list in this object with the file list from \a rhs.
0116   ///
0117   /// \param[in] rhs
0118   ///     A file list object to copy.
0119   ///
0120   /// \return
0121   ///     A const reference to this object.
0122   FileSpecList &operator=(const FileSpecList &rhs) = default;
0123 
0124   /// Move-assignment operator.
0125   FileSpecList &operator=(FileSpecList &&rhs) = default;
0126 
0127   /// Append a FileSpec object to the list.
0128   ///
0129   /// Appends \a file to the end of the file list.
0130   ///
0131   /// \param[in] file
0132   ///     A new file to append to this file list.
0133   void Append(const FileSpec &file);
0134 
0135   /// Append a FileSpec object if unique.
0136   ///
0137   /// Appends \a file to the end of the file list if it doesn't already exist
0138   /// in the file list.
0139   ///
0140   /// \param[in] file
0141   ///     A new file to append to this file list.
0142   ///
0143   /// \return
0144   ///     \b true if the file was appended, \b false otherwise.
0145   bool AppendIfUnique(const FileSpec &file);
0146 
0147   /// Inserts a new FileSpec into the FileSpecList constructed in-place with
0148   /// the given arguments.
0149   ///
0150   /// \param[in] args
0151   ///     Arguments to create the FileSpec
0152   template <class... Args> void EmplaceBack(Args &&...args) {
0153     m_files.emplace_back(std::forward<Args>(args)...);
0154   }
0155 
0156   /// Clears the file list.
0157   void Clear();
0158 
0159   /// Dumps the file list to the supplied stream pointer "s".
0160   ///
0161   /// \param[in] s
0162   ///     The stream that will be used to dump the object description.
0163   void Dump(Stream *s, const char *separator_cstr = "\n") const;
0164 
0165   /// Find a file index.
0166   ///
0167   /// Find the index of the file in the file spec list that matches \a file
0168   /// starting \a idx entries into the file spec list.
0169   ///
0170   /// \param[in] idx
0171   ///     An index into the file list.
0172   ///
0173   /// \param[in] file
0174   ///     The file specification to search for.
0175   ///
0176   /// \param[in] full
0177   ///     Should FileSpec::Equal be called with "full" true or false.
0178   ///
0179   /// \return
0180   ///     The index of the file that matches \a file if it is found,
0181   ///     else UINT32_MAX is returned.
0182   size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
0183 
0184   /// Get file at index.
0185   ///
0186   /// Gets a file from the file list. If \a idx is not a valid index, an empty
0187   /// FileSpec object will be returned. The file objects that are returned can
0188   /// be tested using FileSpec::operator void*().
0189   ///
0190   /// \param[in] idx
0191   ///     An index into the file list.
0192   ///
0193   /// \return
0194   ///     A copy of the FileSpec object at index \a idx. If \a idx
0195   ///     is out of range, then an empty FileSpec object will be
0196   ///     returned.
0197   const FileSpec &GetFileSpecAtIndex(size_t idx) const;
0198 
0199   /// Get the memory cost of this object.
0200   ///
0201   /// Return the size in bytes that this object takes in memory. This returns
0202   /// the size in bytes of this object, not any shared string values it may
0203   /// refer to.
0204   ///
0205   /// \return
0206   ///     The number of bytes that this object occupies in memory.
0207   size_t MemorySize() const;
0208 
0209   bool IsEmpty() const { return m_files.empty(); }
0210 
0211   /// Get the number of files in the file list.
0212   ///
0213   /// \return
0214   ///     The number of files in the file spec list.
0215   size_t GetSize() const;
0216 
0217   bool Insert(size_t idx, const FileSpec &file) {
0218     if (idx < m_files.size()) {
0219       m_files.insert(m_files.begin() + idx, file);
0220       return true;
0221     } else if (idx == m_files.size()) {
0222       m_files.push_back(file);
0223       return true;
0224     }
0225     return false;
0226   }
0227 
0228   bool Replace(size_t idx, const FileSpec &file) {
0229     if (idx < m_files.size()) {
0230       m_files[idx] = file;
0231       return true;
0232     }
0233     return false;
0234   }
0235 
0236   bool Remove(size_t idx) {
0237     if (idx < m_files.size()) {
0238       m_files.erase(m_files.begin() + idx);
0239       return true;
0240     }
0241     return false;
0242   }
0243 
0244   const_iterator begin() const { return m_files.begin(); }
0245   const_iterator end() const { return m_files.end(); }
0246 
0247   llvm::iterator_range<const_iterator> files() const {
0248     return llvm::make_range(begin(), end());
0249   }
0250 
0251 protected:
0252   collection m_files; ///< A collection of FileSpec objects.
0253 };
0254 
0255 } // namespace lldb_private
0256 
0257 #endif // LLDB_CORE_FILESPECLIST_H