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