1//===-- FileSpecList.cpp --------------------------------------------------===//
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#include "lldb/Utility/FileSpecList.h"
10#include "lldb/Utility/ConstString.h"
11#include "lldb/Utility/Stream.h"
12
13#include <cstdint>
14#include <utility>
15
16using namespace lldb_private;
17
18FileSpecList::FileSpecList() : m_files() {}
19
20FileSpecList::~FileSpecList() = default;
21
22// Append the "file_spec" to the end of the file spec list.
23void FileSpecList::Append(const FileSpec &file_spec) {
24 m_files.push_back(x: file_spec);
25}
26
27// Only append the "file_spec" if this list doesn't already contain it.
28//
29// Returns true if "file_spec" was added, false if this list already contained
30// a copy of "file_spec".
31bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
32 collection::iterator end = m_files.end();
33 if (find(first: m_files.begin(), last: end, val: file_spec) == end) {
34 m_files.push_back(x: file_spec);
35 return true;
36 }
37 return false;
38}
39
40// FIXME: Replace this with a DenseSet at the call site. It is inefficient.
41bool SupportFileList::AppendIfUnique(const FileSpec &file_spec) {
42 collection::iterator end = m_files.end();
43 if (find_if(first: m_files.begin(), last: end,
44 pred: [&](const std::shared_ptr<SupportFile> &support_file) {
45 return support_file->GetSpecOnly() == file_spec;
46 }) == end) {
47 Append(file: file_spec);
48 return true;
49 }
50 return false;
51}
52
53// Clears the file list.
54void FileSpecList::Clear() { m_files.clear(); }
55
56// Dumps the file list to the supplied stream pointer "s".
57void FileSpecList::Dump(Stream *s, const char *separator_cstr) const {
58 collection::const_iterator pos, end = m_files.end();
59 for (pos = m_files.begin(); pos != end; ++pos) {
60 pos->Dump(s&: s->AsRawOstream());
61 if (separator_cstr && ((pos + 1) != end))
62 s->PutCString(cstr: separator_cstr);
63 }
64}
65
66// Find the index of the file in the file spec list that matches "file_spec"
67// starting "start_idx" entries into the file spec list.
68//
69// Returns the valid index of the file that matches "file_spec" if it is found,
70// else std::numeric_limits<uint32_t>::max() is returned.
71static size_t FindFileIndex(size_t start_idx, const FileSpec &file_spec,
72 bool full, size_t num_files,
73 std::function<const FileSpec &(size_t)> get_ith) {
74 // When looking for files, we will compare only the filename if the FILE_SPEC
75 // argument is empty
76 bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
77
78 for (size_t idx = start_idx; idx < num_files; ++idx) {
79 const FileSpec &ith = get_ith(idx);
80 if (compare_filename_only) {
81 if (ConstString::Equals(lhs: ith.GetFilename(), rhs: file_spec.GetFilename(),
82 case_sensitive: file_spec.IsCaseSensitive() ||
83 ith.IsCaseSensitive()))
84 return idx;
85 } else {
86 if (FileSpec::Equal(a: ith, b: file_spec, full))
87 return idx;
88 }
89 }
90
91 // We didn't find the file, return an invalid index
92 return UINT32_MAX;
93}
94
95size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
96 bool full) const {
97 return ::FindFileIndex(
98 start_idx, file_spec, full, num_files: m_files.size(),
99 get_ith: [&](size_t idx) -> const FileSpec & { return m_files[idx]; });
100}
101
102size_t SupportFileList::FindFileIndex(size_t start_idx,
103 const FileSpec &file_spec,
104 bool full) const {
105 return ::FindFileIndex(start_idx, file_spec, full, num_files: m_files.size(),
106 get_ith: [&](size_t idx) -> const FileSpec & {
107 return m_files[idx]->GetSpecOnly();
108 });
109}
110
111size_t SupportFileList::FindCompatibleIndex(size_t start_idx,
112 const FileSpec &file_spec) const {
113 const size_t num_files = m_files.size();
114 if (start_idx >= num_files)
115 return UINT32_MAX;
116
117 const bool file_spec_relative = file_spec.IsRelative();
118 const bool file_spec_case_sensitive = file_spec.IsCaseSensitive();
119 // When looking for files, we will compare only the filename if the directory
120 // argument is empty in file_spec
121 const bool full = !file_spec.GetDirectory().IsEmpty();
122
123 for (size_t idx = start_idx; idx < num_files; ++idx) {
124 const FileSpec &curr_file = m_files[idx]->GetSpecOnly();
125
126 // Always start by matching the filename first
127 if (!curr_file.FileEquals(other: file_spec))
128 continue;
129
130 // Only compare the full name if the we were asked to and if the current
131 // file entry has the a directory. If it doesn't have a directory then we
132 // only compare the filename.
133 if (FileSpec::Equal(a: curr_file, b: file_spec, full)) {
134 return idx;
135 } else if (curr_file.IsRelative() || file_spec_relative) {
136 llvm::StringRef curr_file_dir = curr_file.GetDirectory().GetStringRef();
137 if (curr_file_dir.empty())
138 return idx; // Basename match only for this file in the list
139
140 // Check if we have a relative path in our file list, or if "file_spec" is
141 // relative, if so, check if either ends with the other.
142 llvm::StringRef file_spec_dir = file_spec.GetDirectory().GetStringRef();
143 // We have a relative path in our file list, it matches if the
144 // specified path ends with this path, but we must ensure the full
145 // component matches (we don't want "foo/bar.cpp" to match "oo/bar.cpp").
146 auto is_suffix = [](llvm::StringRef a, llvm::StringRef b,
147 bool case_sensitive) -> bool {
148 if (case_sensitive ? a.consume_back(Suffix: b) : a.consume_back_insensitive(Suffix: b))
149 return a.empty() || a.ends_with(Suffix: "/");
150 return false;
151 };
152 const bool case_sensitive =
153 file_spec_case_sensitive || curr_file.IsCaseSensitive();
154 if (is_suffix(curr_file_dir, file_spec_dir, case_sensitive) ||
155 is_suffix(file_spec_dir, curr_file_dir, case_sensitive))
156 return idx;
157 }
158 }
159
160 // We didn't find the file, return an invalid index
161 return UINT32_MAX;
162}
163// Returns the FileSpec object at index "idx". If "idx" is out of range, then
164// an empty FileSpec object will be returned.
165const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
166 if (idx < m_files.size())
167 return m_files[idx];
168 static FileSpec g_empty_file_spec;
169 return g_empty_file_spec;
170}
171
172const FileSpec &SupportFileList::GetFileSpecAtIndex(size_t idx) const {
173 if (idx < m_files.size())
174 return m_files[idx]->Materialize();
175 static FileSpec g_empty_file_spec;
176 return g_empty_file_spec;
177}
178
179std::shared_ptr<SupportFile>
180SupportFileList::GetSupportFileAtIndex(size_t idx) const {
181 if (idx < m_files.size())
182 return m_files[idx];
183 return {};
184}
185
186// Return the size in bytes that this object takes in memory. This returns the
187// size in bytes of this object's member variables and any FileSpec objects its
188// member variables contain, the result doesn't not include the string values
189// for the directories any filenames as those are in shared string pools.
190size_t FileSpecList::MemorySize() const {
191 size_t mem_size = sizeof(FileSpecList);
192 collection::const_iterator pos, end = m_files.end();
193 for (pos = m_files.begin(); pos != end; ++pos) {
194 mem_size += pos->MemorySize();
195 }
196
197 return mem_size;
198}
199
200// Return the number of files in the file spec list.
201size_t FileSpecList::GetSize() const { return m_files.size(); }
202

source code of lldb/source/Utility/FileSpecList.cpp