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 
84 // Returns the FileSpec object at index "idx". If "idx" is out of range, then
85 // an empty FileSpec object will be returned.
GetFileSpecAtIndex(size_t idx) const86 const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
87   if (idx < m_files.size())
88     return m_files[idx];
89   static FileSpec g_empty_file_spec;
90   return g_empty_file_spec;
91 }
92 
GetFileSpecPointerAtIndex(size_t idx) const93 const FileSpec *FileSpecList::GetFileSpecPointerAtIndex(size_t idx) const {
94   if (idx < m_files.size())
95     return &m_files[idx];
96   return nullptr;
97 }
98 
99 // Return the size in bytes that this object takes in memory. This returns the
100 // size in bytes of this object's member variables and any FileSpec objects its
101 // member variables contain, the result doesn't not include the string values
102 // for the directories any filenames as those are in shared string pools.
MemorySize() const103 size_t FileSpecList::MemorySize() const {
104   size_t mem_size = sizeof(FileSpecList);
105   collection::const_iterator pos, end = m_files.end();
106   for (pos = m_files.begin(); pos != end; ++pos) {
107     mem_size += pos->MemorySize();
108   }
109 
110   return mem_size;
111 }
112 
113 // Return the number of files in the file spec list.
GetSize() const114 size_t FileSpecList::GetSize() const { return m_files.size(); }
115 
GetFilesMatchingPartialPath(const char * path,bool dir_okay,FileSpecList & matches)116 size_t FileSpecList::GetFilesMatchingPartialPath(const char *path,
117                                                  bool dir_okay,
118                                                  FileSpecList &matches) {
119   return 0;
120 }
121