1 //===-- FileSpecList.cpp ----------------------------------------*- C++ -*-===//
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/Core/FileSpecList.h"
11 #include "lldb/Utility/ConstString.h"
12 #include "lldb/Utility/Stream.h"
18 using namespace lldb_private;
21 FileSpecList::FileSpecList() : m_files() {}
23 FileSpecList::~FileSpecList() = default;
25 // Append the "file_spec" to the end of the file spec list.
26 void FileSpecList::Append(const FileSpec &file_spec) {
27 m_files.push_back(file_spec);
30 // Only append the "file_spec" if this list doesn't already contain it.
32 // Returns true if "file_spec" was added, false if this list already contained
33 // a copy of "file_spec".
34 bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
35 collection::iterator end = m_files.end();
36 if (find(m_files.begin(), end, file_spec) == end) {
37 m_files.push_back(file_spec);
43 // Clears the file list.
44 void FileSpecList::Clear() { m_files.clear(); }
46 // Dumps the file list to the supplied stream pointer "s".
47 void FileSpecList::Dump(Stream *s, const char *separator_cstr) const {
48 collection::const_iterator pos, end = m_files.end();
49 for (pos = m_files.begin(); pos != end; ++pos) {
50 pos->Dump(s->AsRawOstream());
51 if (separator_cstr && ((pos + 1) != end))
52 s->PutCString(separator_cstr);
56 // Find the index of the file in the file spec list that matches "file_spec"
57 // starting "start_idx" entries into the file spec list.
59 // Returns the valid index of the file that matches "file_spec" if it is found,
60 // else std::numeric_limits<uint32_t>::max() is returned.
61 size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
63 const size_t num_files = m_files.size();
65 // When looking for files, we will compare only the filename if the FILE_SPEC
67 bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
69 for (size_t idx = start_idx; idx < num_files; ++idx) {
70 if (compare_filename_only) {
71 if (ConstString::Equals(
72 m_files[idx].GetFilename(), file_spec.GetFilename(),
73 file_spec.IsCaseSensitive() || m_files[idx].IsCaseSensitive()))
76 if (FileSpec::Equal(m_files[idx], file_spec, full))
81 // We didn't find the file, return an invalid index
85 // Returns the FileSpec object at index "idx". If "idx" is out of range, then
86 // an empty FileSpec object will be returned.
87 const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
88 if (idx < m_files.size())
90 static FileSpec g_empty_file_spec;
91 return g_empty_file_spec;
94 const FileSpec *FileSpecList::GetFileSpecPointerAtIndex(size_t idx) const {
95 if (idx < m_files.size())
100 // Return the size in bytes that this object takes in memory. This returns the
101 // size in bytes of this object's member variables and any FileSpec objects its
102 // member variables contain, the result doesn't not include the string values
103 // for the directories any filenames as those are in shared string pools.
104 size_t FileSpecList::MemorySize() const {
105 size_t mem_size = sizeof(FileSpecList);
106 collection::const_iterator pos, end = m_files.end();
107 for (pos = m_files.begin(); pos != end; ++pos) {
108 mem_size += pos->MemorySize();
114 // Return the number of files in the file spec list.
115 size_t FileSpecList::GetSize() const { return m_files.size(); }
117 size_t FileSpecList::GetFilesMatchingPartialPath(const char *path,
119 FileSpecList &matches) {