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