LLDB mainline
FileSpecList.h
Go to the documentation of this file.
1//===-- FileSpecList.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_UTILITY_FILESPECLIST_H
10#define LLDB_UTILITY_FILESPECLIST_H
11
14#include "lldb/lldb-forward.h"
15
16#include <cstddef>
17#include <vector>
18
19namespace lldb_private {
20class Stream;
21
22/// A list of support files for a CompileUnit.
24public:
27 SupportFileList(SupportFileList &&other) = default;
28
29 typedef std::vector<std::shared_ptr<SupportFile>> collection;
30 typedef collection::const_iterator const_iterator;
31 const_iterator begin() const { return m_files.begin(); }
32 const_iterator end() const { return m_files.end(); }
33
34 void Append(const FileSpec &file) {
35 return Append(std::make_shared<SupportFile>(file));
36 }
37 void Append(std::shared_ptr<SupportFile> &&file) {
38 m_files.push_back(std::move(file));
39 }
40 // FIXME: Only used by SymbolFilePDB. Replace with a DenseSet at call site.
41 bool AppendIfUnique(const FileSpec &file);
42 size_t GetSize() const { return m_files.size(); }
43 const FileSpec &GetFileSpecAtIndex(size_t idx) const;
44 SupportFileNSP GetSupportFileAtIndex(size_t idx) const;
45 size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
46 /// Find a compatible file index.
47 ///
48 /// Find the index of a compatible file in the file spec list that matches \a
49 /// file starting \a idx entries into the file spec list. A file is considered
50 /// compatible if:
51 /// - The file matches exactly (only filename if \a file has no directory)
52 /// - If \a file is relative and any file in the list has this same suffix
53 /// - If any file in the list is relative and the relative path is a suffix
54 /// of \a file
55 ///
56 /// This is used to implement better matching for setting breakpoints in
57 /// source files where an IDE might specify a full path when setting the
58 /// breakpoint and debug info contains relative paths, if a user specifies
59 /// a relative path when setting a breakpoint.
60 ///
61 /// \param[in] idx
62 /// An index into the file list.
63 ///
64 /// \param[in] file
65 /// The file specification to search for.
66 ///
67 /// \param[in] realpath_prefixes
68 /// Paths that start with one of the prefixes in this list will be
69 /// realpath'ed to resolve any symlinks.
70 ///
71 /// \return
72 /// The index of the file that matches \a file if it is found,
73 /// else UINT32_MAX is returned.
74 size_t
75 FindCompatibleIndex(size_t idx, const FileSpec &file,
76 RealpathPrefixes *realpath_prefixes = nullptr) const;
77
78 template <class... Args> void EmplaceBack(Args &&...args) {
79 m_files.push_back(
80 std::make_shared<SupportFile>(std::forward<Args>(args)...));
81 }
82
83protected:
84 collection m_files; ///< A collection of FileSpec objects.
85};
86
87/// \class FileSpecList FileSpecList.h "lldb/Utility/FileSpecList.h"
88/// A file collection class.
89///
90/// A class that contains a mutable list of FileSpec objects.
92public:
93 typedef std::vector<FileSpec> collection;
94 typedef collection::const_iterator const_iterator;
95
96 /// Default constructor.
97 ///
98 /// Initialize this object with an empty file list.
100
101 /// Copy constructor.
102 FileSpecList(const FileSpecList &rhs) = default;
103
104 /// Move constructor
105 FileSpecList(FileSpecList &&rhs) = default;
106
107 /// Initialize this object from a vector of FileSpecs
108 FileSpecList(std::vector<FileSpec> &&rhs) : m_files(std::move(rhs)) {}
109
110 /// Destructor.
112
113 /// Assignment operator.
114 ///
115 /// Replace the file list in this object with the file list from \a rhs.
116 ///
117 /// \param[in] rhs
118 /// A file list object to copy.
119 ///
120 /// \return
121 /// A const reference to this object.
122 FileSpecList &operator=(const FileSpecList &rhs) = default;
123
124 /// Move-assignment operator.
126
127 /// Append a FileSpec object to the list.
128 ///
129 /// Appends \a file to the end of the file list.
130 ///
131 /// \param[in] file
132 /// A new file to append to this file list.
133 void Append(const FileSpec &file);
134
135 /// Appends all elements of \c other to the end of this list
136 /// (regardless of whether a \c FileSpec already exists in the list).
137 void Append(const FileSpecList &other) {
138 m_files.insert(end(), std::begin(other), std::end(other));
139 }
140
141 /// Append a FileSpec object if unique.
142 ///
143 /// Appends \a file to the end of the file list if it doesn't already exist
144 /// in the file list.
145 ///
146 /// \param[in] file
147 /// A new file to append to this file list.
148 ///
149 /// \return
150 /// \b true if the file was appended, \b false otherwise.
151 bool AppendIfUnique(const FileSpec &file);
152
153 /// Inserts a new FileSpec into the FileSpecList constructed in-place with
154 /// the given arguments.
155 ///
156 /// \param[in] args
157 /// Arguments to create the FileSpec
158 template <class... Args> void EmplaceBack(Args &&...args) {
159 m_files.emplace_back(std::forward<Args>(args)...);
160 }
161
162 /// Clears the file list.
163 void Clear();
164
165 /// Dumps the file list to the supplied stream pointer "s".
166 ///
167 /// \param[in] s
168 /// The stream that will be used to dump the object description.
169 void Dump(Stream *s, const char *separator_cstr = "\n") const;
170
171 /// Find a file index.
172 ///
173 /// Find the index of the file in the file spec list that matches \a file
174 /// starting \a idx entries into the file spec list.
175 ///
176 /// \param[in] idx
177 /// An index into the file list.
178 ///
179 /// \param[in] file
180 /// The file specification to search for.
181 ///
182 /// \param[in] full
183 /// Should FileSpec::Equal be called with "full" true or false.
184 ///
185 /// \return
186 /// The index of the file that matches \a file if it is found,
187 /// else UINT32_MAX is returned.
188 size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
189
190 /// Get file at index.
191 ///
192 /// Gets a file from the file list. If \a idx is not a valid index, an empty
193 /// FileSpec object will be returned. The file objects that are returned can
194 /// be tested using FileSpec::operator void*().
195 ///
196 /// \param[in] idx
197 /// An index into the file list.
198 ///
199 /// \return
200 /// A copy of the FileSpec object at index \a idx. If \a idx
201 /// is out of range, then an empty FileSpec object will be
202 /// returned.
203 const FileSpec &GetFileSpecAtIndex(size_t idx) const;
204
205 /// Get the memory cost of this object.
206 ///
207 /// Return the size in bytes that this object takes in memory. This returns
208 /// the size in bytes of this object, not any shared string values it may
209 /// refer to.
210 ///
211 /// \return
212 /// The number of bytes that this object occupies in memory.
213 size_t MemorySize() const;
214
215 bool IsEmpty() const { return m_files.empty(); }
216
217 /// Get the number of files in the file list.
218 ///
219 /// \return
220 /// The number of files in the file spec list.
221 size_t GetSize() const;
222
223 bool Insert(size_t idx, const FileSpec &file) {
224 if (idx < m_files.size()) {
225 m_files.insert(m_files.begin() + idx, file);
226 return true;
227 } else if (idx == m_files.size()) {
228 m_files.push_back(file);
229 return true;
230 }
231 return false;
232 }
233
234 bool Replace(size_t idx, const FileSpec &file) {
235 if (idx < m_files.size()) {
236 m_files[idx] = file;
237 return true;
238 }
239 return false;
240 }
241
242 bool Remove(size_t idx) {
243 if (idx < m_files.size()) {
244 m_files.erase(m_files.begin() + idx);
245 return true;
246 }
247 return false;
248 }
249
250 const_iterator begin() const { return m_files.begin(); }
251 const_iterator end() const { return m_files.end(); }
252
253 llvm::iterator_range<const_iterator> files() const {
254 return llvm::make_range(begin(), end());
255 }
256
257protected:
258 collection m_files; ///< A collection of FileSpec objects.
259};
260
261} // namespace lldb_private
262
263#endif // LLDB_UTILITY_FILESPECLIST_H
A command line argument class.
Definition Args.h:33
std::vector< FileSpec > collection
const_iterator begin() const
llvm::iterator_range< const_iterator > files() const
const_iterator end() const
bool Insert(size_t idx, const FileSpec &file)
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
void EmplaceBack(Args &&...args)
Inserts a new FileSpec into the FileSpecList constructed in-place with the given arguments.
void Append(const FileSpecList &other)
Appends all elements of other to the end of this list (regardless of whether a FileSpec already exist...
void Clear()
Clears the file list.
FileSpecList()
Default constructor.
void Append(const FileSpec &file)
Append a FileSpec object to the list.
size_t GetSize() const
Get the number of files in the file list.
size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const
Find a file index.
FileSpecList & operator=(FileSpecList &&rhs)=default
Move-assignment operator.
bool Replace(size_t idx, const FileSpec &file)
size_t MemorySize() const
Get the memory cost of this object.
bool AppendIfUnique(const FileSpec &file)
Append a FileSpec object if unique.
FileSpecList(const FileSpecList &rhs)=default
Copy constructor.
FileSpecList(std::vector< FileSpec > &&rhs)
Initialize this object from a vector of FileSpecs.
void Dump(Stream *s, const char *separator_cstr="\n") const
Dumps the file list to the supplied stream pointer "s".
FileSpecList(FileSpecList &&rhs)=default
Move constructor.
collection m_files
A collection of FileSpec objects.
collection::const_iterator const_iterator
FileSpecList & operator=(const FileSpecList &rhs)=default
Assignment operator.
A file utility class.
Definition FileSpec.h:57
A stream class that can stream formatted output to a file.
Definition Stream.h:28
const FileSpec & GetFileSpecAtIndex(size_t idx) const
void Append(const FileSpec &file)
const_iterator begin() const
size_t FindCompatibleIndex(size_t idx, const FileSpec &file, RealpathPrefixes *realpath_prefixes=nullptr) const
Find a compatible file index.
SupportFileList(SupportFileList &&other)=default
collection m_files
A collection of FileSpec objects.
void Append(std::shared_ptr< SupportFile > &&file)
std::vector< std::shared_ptr< SupportFile > > collection
bool AppendIfUnique(const FileSpec &file)
size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const
const_iterator end() const
SupportFileList(const SupportFileList &)=delete
collection::const_iterator const_iterator
SupportFileNSP GetSupportFileAtIndex(size_t idx) const
void EmplaceBack(Args &&...args)
A class that represents a running process on the host machine.
NonNullSharedPtr< lldb_private::SupportFile > SupportFileNSP
Definition SupportFile.h:80