LLDB mainline
FileSpecList.cpp
Go to the documentation of this file.
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
11#include "lldb/Utility/Stream.h"
12
13#include <cstdint>
14#include <utility>
15
16using namespace lldb_private;
17
19
21
22// Append the "file_spec" to the end of the file spec list.
23void FileSpecList::Append(const FileSpec &file_spec) {
24 m_files.push_back(file_spec);
25}
26
27// Only append the "file_spec" if this list doesn't already contain it.
28//
29// Returns true if "file_spec" was added, false if this list already contained
30// a copy of "file_spec".
31bool 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);
35 return true;
36 }
37 return false;
38}
39
40// Clears the file list.
41void FileSpecList::Clear() { m_files.clear(); }
42
43// Dumps the file list to the supplied stream pointer "s".
44void 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);
50 }
51}
52
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.
55//
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.
58size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
59 bool full) const {
60 const size_t num_files = m_files.size();
61
62 // When looking for files, we will compare only the filename if the FILE_SPEC
63 // argument is empty
64 bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
65
66 for (size_t idx = start_idx; idx < num_files; ++idx) {
67 if (compare_filename_only) {
69 m_files[idx].GetFilename(), file_spec.GetFilename(),
70 file_spec.IsCaseSensitive() || m_files[idx].IsCaseSensitive()))
71 return idx;
72 } else {
73 if (FileSpec::Equal(m_files[idx], file_spec, full))
74 return idx;
75 }
76 }
77
78 // We didn't find the file, return an invalid index
79 return UINT32_MAX;
80}
81
82size_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)
86 return UINT32_MAX;
87
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();
93
94 for (size_t idx = start_idx; idx < num_files; ++idx) {
95 const FileSpec &curr_file = m_files[idx];
96
97 // Always start by matching the filename first
98 if (!curr_file.FileEquals(file_spec))
99 continue;
100
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)) {
105 return idx;
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
110
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("/");
121 return false;
122 };
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))
127 return idx;
128 }
129 }
130
131 // We didn't find the file, return an invalid index
132 return UINT32_MAX;
133}
134// Returns the FileSpec object at index "idx". If "idx" is out of range, then
135// an empty FileSpec object will be returned.
137 if (idx < m_files.size())
138 return m_files[idx];
139 static FileSpec g_empty_file_spec;
140 return g_empty_file_spec;
141}
142
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.
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();
152 }
153
154 return mem_size;
155}
156
157// Return the number of files in the file spec list.
158size_t FileSpecList::GetSize() const { return m_files.size(); }
static bool Equals(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Equal to operator.
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:293
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:191
const_iterator end() const
Definition: FileSpecList.h:204
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
size_t FindCompatibleIndex(size_t idx, const FileSpec &file) const
Find a compatible file index.
void Clear()
Clears the file list.
FileSpecList()
Default constructor.
void Append(const FileSpec &file)
Append a FileSpec object to the list.
size_t GetSize() const
Get the number of files in the file list.
size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const
Find a file index.
size_t MemorySize() const
Get the memory cost of this object.
bool AppendIfUnique(const FileSpec &file)
Append a FileSpec object if unique.
void Dump(Stream *s, const char *separator_cstr="\n") const
Dumps the file list to the supplied stream pointer "s".
collection m_files
A collection of FileSpec objects.
Definition: FileSpecList.h:207
A file utility class.
Definition: FileSpec.h:57
static bool Equal(const FileSpec &a, const FileSpec &b, bool full)
Definition: FileSpec.cpp:297
bool IsRelative() const
Returns true if the filespec represents a relative path.
Definition: FileSpec.cpp:510
bool FileEquals(const FileSpec &other) const
Definition: FileSpec.cpp:236
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:245
const ConstString & GetDirectory() const
Directory string const get accessor.
Definition: FileSpec.h:228
bool IsCaseSensitive() const
Case sensitivity of path.
Definition: FileSpec.h:210
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14