1 //===-- FileSpecList.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/FileSpecList.h"
10 #include "lldb/Utility/ConstString.h"
11 #include "lldb/Utility/Stream.h"
16 using namespace lldb_private
;
18 FileSpecList::FileSpecList() : m_files() {}
20 FileSpecList::~FileSpecList() = default;
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
);
27 // Only append the "file_spec" if this list doesn't already contain it.
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
);
40 // Clears the file list.
41 void FileSpecList::Clear() { m_files
.clear(); }
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
);
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.
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
,
60 const size_t num_files
= m_files
.size();
62 // When looking for files, we will compare only the filename if the FILE_SPEC
64 bool compare_filename_only
= file_spec
.GetDirectory().IsEmpty();
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()))
73 if (FileSpec::Equal(m_files
[idx
], file_spec
, full
))
78 // We didn't find the file, return an invalid index
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
)
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();
94 for (size_t idx
= start_idx
; idx
< num_files
; ++idx
) {
95 const FileSpec
&curr_file
= m_files
[idx
];
97 // Always start by matching the filename first
98 if (!curr_file
.FileEquals(file_spec
))
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
)) {
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
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("/");
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
))
131 // We didn't find the file, return an invalid index
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())
139 static FileSpec g_empty_file_spec
;
140 return g_empty_file_spec
;
143 // Return the size in bytes that this object takes in memory. This returns the
144 // size in bytes of this object's member variables and any FileSpec objects its
145 // member variables contain, the result doesn't not include the string values
146 // for the directories any filenames as those are in shared string pools.
147 size_t FileSpecList::MemorySize() const {
148 size_t mem_size
= sizeof(FileSpecList
);
149 collection::const_iterator pos
, end
= m_files
.end();
150 for (pos
= m_files
.begin(); pos
!= end
; ++pos
) {
151 mem_size
+= pos
->MemorySize();
157 // Return the number of files in the file spec list.
158 size_t FileSpecList::GetSize() const { return m_files
.size(); }