xref: /freebsd-src/contrib/llvm-project/lldb/source/Utility/FileSpecList.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
106c3fb27SDimitry Andric //===-- FileSpecList.cpp --------------------------------------------------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric 
906c3fb27SDimitry Andric #include "lldb/Utility/FileSpecList.h"
1006c3fb27SDimitry Andric #include "lldb/Utility/ConstString.h"
1106c3fb27SDimitry Andric #include "lldb/Utility/Stream.h"
1206c3fb27SDimitry Andric 
1306c3fb27SDimitry Andric #include <cstdint>
1406c3fb27SDimitry Andric #include <utility>
1506c3fb27SDimitry Andric 
1606c3fb27SDimitry Andric using namespace lldb_private;
1706c3fb27SDimitry Andric 
FileSpecList()1806c3fb27SDimitry Andric FileSpecList::FileSpecList() : m_files() {}
1906c3fb27SDimitry Andric 
2006c3fb27SDimitry Andric FileSpecList::~FileSpecList() = default;
2106c3fb27SDimitry Andric 
2206c3fb27SDimitry Andric // Append the "file_spec" to the end of the file spec list.
Append(const FileSpec & file_spec)2306c3fb27SDimitry Andric void FileSpecList::Append(const FileSpec &file_spec) {
2406c3fb27SDimitry Andric   m_files.push_back(file_spec);
2506c3fb27SDimitry Andric }
2606c3fb27SDimitry Andric 
2706c3fb27SDimitry Andric // Only append the "file_spec" if this list doesn't already contain it.
2806c3fb27SDimitry Andric //
2906c3fb27SDimitry Andric // Returns true if "file_spec" was added, false if this list already contained
3006c3fb27SDimitry Andric // a copy of "file_spec".
AppendIfUnique(const FileSpec & file_spec)3106c3fb27SDimitry Andric bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
3206c3fb27SDimitry Andric   collection::iterator end = m_files.end();
3306c3fb27SDimitry Andric   if (find(m_files.begin(), end, file_spec) == end) {
3406c3fb27SDimitry Andric     m_files.push_back(file_spec);
3506c3fb27SDimitry Andric     return true;
3606c3fb27SDimitry Andric   }
3706c3fb27SDimitry Andric   return false;
3806c3fb27SDimitry Andric }
3906c3fb27SDimitry Andric 
401db9f3b2SDimitry Andric // FIXME: Replace this with a DenseSet at the call site. It is inefficient.
AppendIfUnique(const FileSpec & file_spec)411db9f3b2SDimitry Andric bool SupportFileList::AppendIfUnique(const FileSpec &file_spec) {
421db9f3b2SDimitry Andric   collection::iterator end = m_files.end();
431db9f3b2SDimitry Andric   if (find_if(m_files.begin(), end,
44*7a6dacacSDimitry Andric               [&](const std::shared_ptr<SupportFile> &support_file) {
451db9f3b2SDimitry Andric                 return support_file->GetSpecOnly() == file_spec;
461db9f3b2SDimitry Andric               }) == end) {
471db9f3b2SDimitry Andric     Append(file_spec);
481db9f3b2SDimitry Andric     return true;
491db9f3b2SDimitry Andric   }
501db9f3b2SDimitry Andric   return false;
511db9f3b2SDimitry Andric }
521db9f3b2SDimitry Andric 
5306c3fb27SDimitry Andric // Clears the file list.
Clear()5406c3fb27SDimitry Andric void FileSpecList::Clear() { m_files.clear(); }
5506c3fb27SDimitry Andric 
5606c3fb27SDimitry Andric // Dumps the file list to the supplied stream pointer "s".
Dump(Stream * s,const char * separator_cstr) const5706c3fb27SDimitry Andric void FileSpecList::Dump(Stream *s, const char *separator_cstr) const {
5806c3fb27SDimitry Andric   collection::const_iterator pos, end = m_files.end();
5906c3fb27SDimitry Andric   for (pos = m_files.begin(); pos != end; ++pos) {
6006c3fb27SDimitry Andric     pos->Dump(s->AsRawOstream());
6106c3fb27SDimitry Andric     if (separator_cstr && ((pos + 1) != end))
6206c3fb27SDimitry Andric       s->PutCString(separator_cstr);
6306c3fb27SDimitry Andric   }
6406c3fb27SDimitry Andric }
6506c3fb27SDimitry Andric 
6606c3fb27SDimitry Andric // Find the index of the file in the file spec list that matches "file_spec"
6706c3fb27SDimitry Andric // starting "start_idx" entries into the file spec list.
6806c3fb27SDimitry Andric //
6906c3fb27SDimitry Andric // Returns the valid index of the file that matches "file_spec" if it is found,
7006c3fb27SDimitry 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)711db9f3b2SDimitry Andric static size_t FindFileIndex(size_t start_idx, const FileSpec &file_spec,
721db9f3b2SDimitry Andric                             bool full, size_t num_files,
731db9f3b2SDimitry Andric                             std::function<const FileSpec &(size_t)> get_ith) {
7406c3fb27SDimitry Andric   // When looking for files, we will compare only the filename if the FILE_SPEC
7506c3fb27SDimitry Andric   // argument is empty
7606c3fb27SDimitry Andric   bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
7706c3fb27SDimitry Andric 
7806c3fb27SDimitry Andric   for (size_t idx = start_idx; idx < num_files; ++idx) {
791db9f3b2SDimitry Andric     const FileSpec &ith = get_ith(idx);
8006c3fb27SDimitry Andric     if (compare_filename_only) {
811db9f3b2SDimitry Andric       if (ConstString::Equals(ith.GetFilename(), file_spec.GetFilename(),
821db9f3b2SDimitry Andric                               file_spec.IsCaseSensitive() ||
831db9f3b2SDimitry Andric                                   ith.IsCaseSensitive()))
8406c3fb27SDimitry Andric         return idx;
8506c3fb27SDimitry Andric     } else {
861db9f3b2SDimitry Andric       if (FileSpec::Equal(ith, file_spec, full))
8706c3fb27SDimitry Andric         return idx;
8806c3fb27SDimitry Andric     }
8906c3fb27SDimitry Andric   }
9006c3fb27SDimitry Andric 
9106c3fb27SDimitry Andric   // We didn't find the file, return an invalid index
9206c3fb27SDimitry Andric   return UINT32_MAX;
9306c3fb27SDimitry Andric }
9406c3fb27SDimitry Andric 
FindFileIndex(size_t start_idx,const FileSpec & file_spec,bool full) const951db9f3b2SDimitry Andric size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
961db9f3b2SDimitry Andric                                    bool full) const {
971db9f3b2SDimitry Andric   return ::FindFileIndex(
981db9f3b2SDimitry Andric       start_idx, file_spec, full, m_files.size(),
991db9f3b2SDimitry Andric       [&](size_t idx) -> const FileSpec & { return m_files[idx]; });
1001db9f3b2SDimitry Andric }
1011db9f3b2SDimitry Andric 
FindFileIndex(size_t start_idx,const FileSpec & file_spec,bool full) const1021db9f3b2SDimitry Andric size_t SupportFileList::FindFileIndex(size_t start_idx,
1031db9f3b2SDimitry Andric                                       const FileSpec &file_spec,
1041db9f3b2SDimitry Andric                                       bool full) const {
1051db9f3b2SDimitry Andric   return ::FindFileIndex(start_idx, file_spec, full, m_files.size(),
1061db9f3b2SDimitry Andric                          [&](size_t idx) -> const FileSpec & {
1071db9f3b2SDimitry Andric                            return m_files[idx]->GetSpecOnly();
1081db9f3b2SDimitry Andric                          });
1091db9f3b2SDimitry Andric }
1101db9f3b2SDimitry Andric 
FindCompatibleIndex(size_t start_idx,const FileSpec & file_spec) const1111db9f3b2SDimitry Andric size_t SupportFileList::FindCompatibleIndex(size_t start_idx,
11206c3fb27SDimitry Andric                                             const FileSpec &file_spec) const {
11306c3fb27SDimitry Andric   const size_t num_files = m_files.size();
11406c3fb27SDimitry Andric   if (start_idx >= num_files)
11506c3fb27SDimitry Andric     return UINT32_MAX;
11606c3fb27SDimitry Andric 
11706c3fb27SDimitry Andric   const bool file_spec_relative = file_spec.IsRelative();
11806c3fb27SDimitry Andric   const bool file_spec_case_sensitive = file_spec.IsCaseSensitive();
11906c3fb27SDimitry Andric   // When looking for files, we will compare only the filename if the directory
12006c3fb27SDimitry Andric   // argument is empty in file_spec
12106c3fb27SDimitry Andric   const bool full = !file_spec.GetDirectory().IsEmpty();
12206c3fb27SDimitry Andric 
12306c3fb27SDimitry Andric   for (size_t idx = start_idx; idx < num_files; ++idx) {
1241db9f3b2SDimitry Andric     const FileSpec &curr_file = m_files[idx]->GetSpecOnly();
12506c3fb27SDimitry Andric 
12606c3fb27SDimitry Andric     // Always start by matching the filename first
12706c3fb27SDimitry Andric     if (!curr_file.FileEquals(file_spec))
12806c3fb27SDimitry Andric       continue;
12906c3fb27SDimitry Andric 
13006c3fb27SDimitry Andric     // Only compare the full name if the we were asked to and if the current
13106c3fb27SDimitry Andric     // file entry has the a directory. If it doesn't have a directory then we
13206c3fb27SDimitry Andric     // only compare the filename.
13306c3fb27SDimitry Andric     if (FileSpec::Equal(curr_file, file_spec, full)) {
13406c3fb27SDimitry Andric       return idx;
13506c3fb27SDimitry Andric     } else if (curr_file.IsRelative() || file_spec_relative) {
13606c3fb27SDimitry Andric       llvm::StringRef curr_file_dir = curr_file.GetDirectory().GetStringRef();
13706c3fb27SDimitry Andric       if (curr_file_dir.empty())
13806c3fb27SDimitry Andric         return idx; // Basename match only for this file in the list
13906c3fb27SDimitry Andric 
14006c3fb27SDimitry Andric       // Check if we have a relative path in our file list, or if "file_spec" is
14106c3fb27SDimitry Andric       // relative, if so, check if either ends with the other.
14206c3fb27SDimitry Andric       llvm::StringRef file_spec_dir = file_spec.GetDirectory().GetStringRef();
14306c3fb27SDimitry Andric       // We have a relative path in our file list, it matches if the
14406c3fb27SDimitry Andric       // specified path ends with this path, but we must ensure the full
14506c3fb27SDimitry Andric       // component matches (we don't want "foo/bar.cpp" to match "oo/bar.cpp").
14606c3fb27SDimitry Andric       auto is_suffix = [](llvm::StringRef a, llvm::StringRef b,
14706c3fb27SDimitry Andric                           bool case_sensitive) -> bool {
14806c3fb27SDimitry Andric         if (case_sensitive ? a.consume_back(b) : a.consume_back_insensitive(b))
1495f757f3fSDimitry Andric           return a.empty() || a.ends_with("/");
15006c3fb27SDimitry Andric         return false;
15106c3fb27SDimitry Andric       };
15206c3fb27SDimitry Andric       const bool case_sensitive =
15306c3fb27SDimitry Andric           file_spec_case_sensitive || curr_file.IsCaseSensitive();
15406c3fb27SDimitry Andric       if (is_suffix(curr_file_dir, file_spec_dir, case_sensitive) ||
15506c3fb27SDimitry Andric           is_suffix(file_spec_dir, curr_file_dir, case_sensitive))
15606c3fb27SDimitry Andric         return idx;
15706c3fb27SDimitry Andric     }
15806c3fb27SDimitry Andric   }
15906c3fb27SDimitry Andric 
16006c3fb27SDimitry Andric   // We didn't find the file, return an invalid index
16106c3fb27SDimitry Andric   return UINT32_MAX;
16206c3fb27SDimitry Andric }
16306c3fb27SDimitry Andric // Returns the FileSpec object at index "idx". If "idx" is out of range, then
16406c3fb27SDimitry Andric // an empty FileSpec object will be returned.
GetFileSpecAtIndex(size_t idx) const16506c3fb27SDimitry Andric const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
16606c3fb27SDimitry Andric   if (idx < m_files.size())
16706c3fb27SDimitry Andric     return m_files[idx];
16806c3fb27SDimitry Andric   static FileSpec g_empty_file_spec;
16906c3fb27SDimitry Andric   return g_empty_file_spec;
17006c3fb27SDimitry Andric }
17106c3fb27SDimitry Andric 
GetFileSpecAtIndex(size_t idx) const1721db9f3b2SDimitry Andric const FileSpec &SupportFileList::GetFileSpecAtIndex(size_t idx) const {
1731db9f3b2SDimitry Andric   if (idx < m_files.size())
1741db9f3b2SDimitry Andric     return m_files[idx]->Materialize();
1751db9f3b2SDimitry Andric   static FileSpec g_empty_file_spec;
1761db9f3b2SDimitry Andric   return g_empty_file_spec;
1771db9f3b2SDimitry Andric }
1781db9f3b2SDimitry Andric 
179*7a6dacacSDimitry Andric std::shared_ptr<SupportFile>
GetSupportFileAtIndex(size_t idx) const180*7a6dacacSDimitry Andric SupportFileList::GetSupportFileAtIndex(size_t idx) const {
181*7a6dacacSDimitry Andric   if (idx < m_files.size())
182*7a6dacacSDimitry Andric     return m_files[idx];
183*7a6dacacSDimitry Andric   return {};
184*7a6dacacSDimitry Andric }
185*7a6dacacSDimitry Andric 
18606c3fb27SDimitry Andric // Return the size in bytes that this object takes in memory. This returns the
18706c3fb27SDimitry Andric // size in bytes of this object's member variables and any FileSpec objects its
18806c3fb27SDimitry Andric // member variables contain, the result doesn't not include the string values
18906c3fb27SDimitry Andric // for the directories any filenames as those are in shared string pools.
MemorySize() const19006c3fb27SDimitry Andric size_t FileSpecList::MemorySize() const {
19106c3fb27SDimitry Andric   size_t mem_size = sizeof(FileSpecList);
19206c3fb27SDimitry Andric   collection::const_iterator pos, end = m_files.end();
19306c3fb27SDimitry Andric   for (pos = m_files.begin(); pos != end; ++pos) {
19406c3fb27SDimitry Andric     mem_size += pos->MemorySize();
19506c3fb27SDimitry Andric   }
19606c3fb27SDimitry Andric 
19706c3fb27SDimitry Andric   return mem_size;
19806c3fb27SDimitry Andric }
19906c3fb27SDimitry Andric 
20006c3fb27SDimitry Andric // Return the number of files in the file spec list.
GetSize() const20106c3fb27SDimitry Andric size_t FileSpecList::GetSize() const { return m_files.size(); }
202