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