LLDB mainline
FileSpecList.cpp
Go to the documentation of this file.
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
11#include "lldb/Utility/Log.h"
13#include "lldb/Utility/Stream.h"
14
15#include <cstdint>
16#include <utility>
17
18using namespace lldb_private;
19
21
23
24// Append the "file_spec" to the end of the file spec list.
25void FileSpecList::Append(const FileSpec &file_spec) {
26 m_files.push_back(file_spec);
27}
28
29// Only append the "file_spec" if this list doesn't already contain it.
30//
31// Returns true if "file_spec" was added, false if this list already contained
32// a copy of "file_spec".
33bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
34 collection::iterator end = m_files.end();
35 if (find(m_files.begin(), end, file_spec) == end) {
36 m_files.push_back(file_spec);
37 return true;
38 }
39 return false;
40}
41
42// FIXME: Replace this with a DenseSet at the call site. It is inefficient.
44 collection::iterator end = m_files.end();
45 if (find_if(m_files.begin(), end, [&](const SupportFileNSP &support_file) {
46 return support_file->GetSpecOnly() == file_spec;
47 }) == end) {
48 Append(file_spec);
49 return true;
50 }
51 return false;
52}
53
54// Clears the file list.
55void FileSpecList::Clear() { m_files.clear(); }
56
57// Dumps the file list to the supplied stream pointer "s".
58void FileSpecList::Dump(Stream *s, const char *separator_cstr) const {
59 collection::const_iterator pos, end = m_files.end();
60 for (pos = m_files.begin(); pos != end; ++pos) {
61 pos->Dump(s->AsRawOstream());
62 if (separator_cstr && ((pos + 1) != end))
63 s->PutCString(separator_cstr);
64 }
65}
66
67// Find the index of the file in the file spec list that matches "file_spec"
68// starting "start_idx" entries into the file spec list.
69//
70// Returns the valid index of the file that matches "file_spec" if it is found,
71// else std::numeric_limits<uint32_t>::max() is returned.
72static size_t FindFileIndex(size_t start_idx, const FileSpec &file_spec,
73 bool full, size_t num_files,
74 std::function<const FileSpec &(size_t)> get_ith) {
75 // When looking for files, we will compare only the filename if the FILE_SPEC
76 // argument is empty
77 bool compare_filename_only = file_spec.GetDirectory().empty();
78
79 for (size_t idx = start_idx; idx < num_files; ++idx) {
80 const FileSpec &ith = get_ith(idx);
81 if (compare_filename_only) {
82 if (file_spec.FileEquals(ith))
83 return idx;
84 } else {
85 if (FileSpec::Equal(ith, file_spec, full))
86 return idx;
87 }
88 }
89
90 // We didn't find the file, return an invalid index
91 return UINT32_MAX;
92}
93
94size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
95 bool full) const {
96 return ::FindFileIndex(
97 start_idx, file_spec, full, m_files.size(),
98 [&](size_t idx) -> const FileSpec & { return m_files[idx]; });
99}
100
101size_t SupportFileList::FindFileIndex(size_t start_idx,
102 const FileSpec &file_spec,
103 bool full) const {
104 return ::FindFileIndex(start_idx, file_spec, full, m_files.size(),
105 [&](size_t idx) -> const FileSpec & {
106 return m_files[idx]->GetSpecOnly();
107 });
108}
109
115
117 const FileSpec &file_spec) {
118 const bool file_spec_relative = file_spec.IsRelative();
119 const bool file_spec_case_sensitive = file_spec.IsCaseSensitive();
120 // When looking for files, we will compare only the filename if the directory
121 // argument is empty in file_spec
122 const bool full = !file_spec.GetDirectory().empty();
123
124 // Always start by matching the filename first
125 if (!curr_file.FileEquals(file_spec))
127
128 // Only compare the full name if the we were asked to and if the current
129 // file entry has a directory. If it doesn't have a directory then we only
130 // compare the filename.
131 if (FileSpec::Equal(curr_file, file_spec, full)) {
133 } else if (curr_file.IsRelative() || file_spec_relative) {
134 llvm::StringRef curr_file_dir = curr_file.GetDirectory();
135 if (curr_file_dir.empty())
136 // Basename match only for this file in the list
138
139 // Check if we have a relative path in our file list, or if "file_spec" is
140 // relative, if so, check if either ends with the other.
141 llvm::StringRef file_spec_dir = file_spec.GetDirectory();
142 // We have a relative path in our file list, it matches if the
143 // specified path ends with this path, but we must ensure the full
144 // component matches (we don't want "foo/bar.cpp" to match "oo/bar.cpp").
145 auto is_suffix = [](llvm::StringRef a, llvm::StringRef b,
146 bool case_sensitive) -> bool {
147 if (case_sensitive ? a.consume_back(b) : a.consume_back_insensitive(b))
148 return a.empty() || a.ends_with("/");
149 return false;
150 };
151 const bool case_sensitive =
152 file_spec_case_sensitive || curr_file.IsCaseSensitive();
153 if (is_suffix(curr_file_dir, file_spec_dir, case_sensitive) ||
154 is_suffix(file_spec_dir, curr_file_dir, case_sensitive))
156 }
158}
159
161 size_t start_idx, const FileSpec &file_spec,
162 RealpathPrefixes *realpath_prefixes) const {
163 const size_t num_files = m_files.size();
164 if (start_idx >= num_files)
165 return UINT32_MAX;
166
167 for (size_t idx = start_idx; idx < num_files; ++idx) {
168 const FileSpec &curr_file = m_files[idx]->GetSpecOnly();
169
170 IsCompatibleResult result = IsCompatible(curr_file, file_spec);
172 return idx;
173
174 if (realpath_prefixes && result == IsCompatibleResult::kOnlyFileMatch) {
175 if (std::optional<FileSpec> resolved_curr_file =
176 realpath_prefixes->ResolveSymlinks(curr_file)) {
177 if (IsCompatible(*resolved_curr_file, file_spec) ==
179 // Stats and logging.
180 realpath_prefixes->IncreaseSourceRealpathCompatibleCount();
181 Log *log = GetLog(LLDBLog::Source);
182 LLDB_LOGF(log,
183 "Realpath'ed support file %s is compatible to input file",
184 resolved_curr_file->GetPath().c_str());
185 // We found a match
186 return idx;
187 }
188 }
189 }
190 }
191
192 // We didn't find the file, return an invalid index
193 return UINT32_MAX;
194}
195// Returns the FileSpec object at index "idx". If "idx" is out of range, then
196// an empty FileSpec object will be returned.
198 if (idx < m_files.size())
199 return m_files[idx];
200 static FileSpec g_empty_file_spec;
201 return g_empty_file_spec;
202}
203
205 if (idx < m_files.size())
206 return m_files[idx]->Materialize();
207 static FileSpec g_empty_file_spec;
208 return g_empty_file_spec;
209}
210
212 if (idx < m_files.size())
213 return m_files[idx];
214 return std::make_shared<SupportFile>();
215}
216
217// Return the number of files in the file spec list.
218size_t FileSpecList::GetSize() const { return m_files.size(); }
IsCompatibleResult IsCompatible(const FileSpec &curr_file, const FileSpec &file_spec)
static size_t FindFileIndex(size_t start_idx, const FileSpec &file_spec, bool full, size_t num_files, std::function< const FileSpec &(size_t)> get_ith)
IsCompatibleResult
@ kBothDirectoryAndFileMatch
@ kOnlyFileMatch
@ kNoMatch
#define LLDB_LOGF(log,...)
Definition Log.h:390
const_iterator end() const
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
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.
bool AppendIfUnique(const FileSpec &file)
Append a FileSpec object if unique.
void Dump(Stream *s, const char *separator_cstr="\n") const
Dumps the file list to the supplied stream pointer "s".
collection m_files
A collection of FileSpec objects.
A file utility class.
Definition FileSpec.h:57
static bool Equal(const FileSpec &a, const FileSpec &b, bool full)
Definition FileSpec.cpp:306
bool IsRelative() const
Returns true if the filespec represents a relative path.
Definition FileSpec.cpp:512
bool FileEquals(const FileSpec &other) const
Definition FileSpec.cpp:234
bool IsCaseSensitive() const
Case sensitivity of path.
Definition FileSpec.h:206
llvm::StringRef GetDirectory() const
Directory string const get accessor.
Definition FileSpec.h:234
std::optional< FileSpec > ResolveSymlinks(const FileSpec &file_spec)
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:405
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
const FileSpec & GetFileSpecAtIndex(size_t idx) const
void Append(const FileSpec &file)
size_t FindCompatibleIndex(size_t idx, const FileSpec &file, RealpathPrefixes *realpath_prefixes=nullptr) const
Find a compatible file index.
collection m_files
A collection of FileSpec objects.
bool AppendIfUnique(const FileSpec &file)
size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const
const_iterator end() const
SupportFileNSP GetSupportFileAtIndex(size_t idx) const
#define UINT32_MAX
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:339
NonNullSharedPtr< lldb_private::SupportFile > SupportFileNSP
Definition SupportFile.h:80