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