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 <stdint.h>
17 
18 using namespace lldb_private;
19 using namespace std;
20 
FileSpecList()21 FileSpecList::FileSpecList() : m_files() {}
22 
23 FileSpecList::~FileSpecList() = default;
24 
25 // Append the "file_spec" to the end of the file spec list.
Append(const FileSpec & file_spec)26 void FileSpecList::Append(const FileSpec &file_spec) {
27   m_files.push_back(file_spec);
28 }
29 
30 // Only append the "file_spec" if this list doesn't already contain it.
31 //
32 // Returns true if "file_spec" was added, false if this list already contained
33 // a copy of "file_spec".
AppendIfUnique(const FileSpec & 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);
38     return true;
39   }
40   return false;
41 }
42 
43 // Clears the file list.
Clear()44 void FileSpecList::Clear() { m_files.clear(); }
45 
46 // Dumps the file list to the supplied stream pointer "s".
Dump(Stream * s,const char * separator_cstr) const47 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);
53   }
54 }
55 
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.
58 //
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.
FindFileIndex(size_t start_idx,const FileSpec & file_spec,bool full) const61 size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
62                                    bool full) const {
63   const size_t num_files = m_files.size();
64 
65   // When looking for files, we will compare only the filename if the FILE_SPEC
66   // argument is empty
67   bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
68 
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()))
74         return idx;
75     } else {
76       if (FileSpec::Equal(m_files[idx], file_spec, full))
77         return idx;
78     }
79   }
80 
81   // We didn't find the file, return an invalid index
82   return UINT32_MAX;
83 }
84 
85 // Returns the FileSpec object at index "idx". If "idx" is out of range, then
86 // an empty FileSpec object will be returned.
GetFileSpecAtIndex(size_t idx) const87 const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
88   if (idx < m_files.size())
89     return m_files[idx];
90   static FileSpec g_empty_file_spec;
91   return g_empty_file_spec;
92 }
93 
GetFileSpecPointerAtIndex(size_t idx) const94 const FileSpec *FileSpecList::GetFileSpecPointerAtIndex(size_t idx) const {
95   if (idx < m_files.size())
96     return &m_files[idx];
97   return nullptr;
98 }
99 
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.
MemorySize() const104 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();
109   }
110 
111   return mem_size;
112 }
113 
114 // Return the number of files in the file spec list.
GetSize() const115 size_t FileSpecList::GetSize() const { return m_files.size(); }
116 
GetFilesMatchingPartialPath(const char * path,bool dir_okay,FileSpecList & matches)117 size_t FileSpecList::GetFilesMatchingPartialPath(const char *path,
118                                                  bool dir_okay,
119                                                  FileSpecList &matches) {
120   return 0;
121 }
122