1 //===-- SBFileSpec.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/API/SBFileSpec.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Host/PosixApi.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/Stream.h"
17
18 #include "llvm/ADT/SmallString.h"
19
20 #include <inttypes.h>
21 #include <limits.h>
22
23 using namespace lldb;
24 using namespace lldb_private;
25
SBFileSpec()26 SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
27 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpec);
28 }
29
SBFileSpec(const SBFileSpec & rhs)30 SBFileSpec::SBFileSpec(const SBFileSpec &rhs) : m_opaque_up() {
31 LLDB_RECORD_CONSTRUCTOR(SBFileSpec, (const lldb::SBFileSpec &), rhs);
32
33 m_opaque_up = clone(rhs.m_opaque_up);
34 }
35
SBFileSpec(const lldb_private::FileSpec & fspec)36 SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
37 : m_opaque_up(new lldb_private::FileSpec(fspec)) {}
38
39 // Deprecated!!!
SBFileSpec(const char * path)40 SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
41 LLDB_RECORD_CONSTRUCTOR(SBFileSpec, (const char *), path);
42
43 FileSystem::Instance().Resolve(*m_opaque_up);
44 }
45
SBFileSpec(const char * path,bool resolve)46 SBFileSpec::SBFileSpec(const char *path, bool resolve)
47 : m_opaque_up(new FileSpec(path)) {
48 LLDB_RECORD_CONSTRUCTOR(SBFileSpec, (const char *, bool), path, resolve);
49
50 if (resolve)
51 FileSystem::Instance().Resolve(*m_opaque_up);
52 }
53
54 SBFileSpec::~SBFileSpec() = default;
55
operator =(const SBFileSpec & rhs)56 const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
57 LLDB_RECORD_METHOD(const lldb::SBFileSpec &,
58 SBFileSpec, operator=,(const lldb::SBFileSpec &), rhs);
59
60 if (this != &rhs)
61 m_opaque_up = clone(rhs.m_opaque_up);
62 return LLDB_RECORD_RESULT(*this);
63 }
64
operator ==(const SBFileSpec & rhs) const65 bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
66 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, operator==,(const SBFileSpec &rhs),
67 rhs);
68
69 return ref() == rhs.ref();
70 }
71
operator !=(const SBFileSpec & rhs) const72 bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
73 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, operator!=,(const SBFileSpec &rhs),
74 rhs);
75
76 return !(*this == rhs);
77 }
78
IsValid() const79 bool SBFileSpec::IsValid() const {
80 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, IsValid);
81 return this->operator bool();
82 }
operator bool() const83 SBFileSpec::operator bool() const {
84 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, operator bool);
85
86 return m_opaque_up->operator bool();
87 }
88
Exists() const89 bool SBFileSpec::Exists() const {
90 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, Exists);
91
92 return FileSystem::Instance().Exists(*m_opaque_up);
93 }
94
ResolveExecutableLocation()95 bool SBFileSpec::ResolveExecutableLocation() {
96 LLDB_RECORD_METHOD_NO_ARGS(bool, SBFileSpec, ResolveExecutableLocation);
97
98 return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
99 }
100
ResolvePath(const char * src_path,char * dst_path,size_t dst_len)101 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
102 size_t dst_len) {
103 LLDB_RECORD_STATIC_METHOD(int, SBFileSpec, ResolvePath,
104 (const char *, char *, size_t), src_path, dst_path,
105 dst_len);
106
107 llvm::SmallString<64> result(src_path);
108 FileSystem::Instance().Resolve(result);
109 ::snprintf(dst_path, dst_len, "%s", result.c_str());
110 return std::min(dst_len - 1, result.size());
111 }
112
GetFilename() const113 const char *SBFileSpec::GetFilename() const {
114 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec, GetFilename);
115
116 return m_opaque_up->GetFilename().AsCString();
117 }
118
GetDirectory() const119 const char *SBFileSpec::GetDirectory() const {
120 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec, GetDirectory);
121
122 FileSpec directory{*m_opaque_up};
123 directory.GetFilename().Clear();
124 return directory.GetCString();
125 }
126
SetFilename(const char * filename)127 void SBFileSpec::SetFilename(const char *filename) {
128 LLDB_RECORD_METHOD(void, SBFileSpec, SetFilename, (const char *), filename);
129
130 if (filename && filename[0])
131 m_opaque_up->GetFilename().SetCString(filename);
132 else
133 m_opaque_up->GetFilename().Clear();
134 }
135
SetDirectory(const char * directory)136 void SBFileSpec::SetDirectory(const char *directory) {
137 LLDB_RECORD_METHOD(void, SBFileSpec, SetDirectory, (const char *), directory);
138
139 if (directory && directory[0])
140 m_opaque_up->GetDirectory().SetCString(directory);
141 else
142 m_opaque_up->GetDirectory().Clear();
143 }
144
GetPath(char * dst_path,size_t dst_len) const145 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
146 LLDB_RECORD_CHAR_PTR_METHOD_CONST(uint32_t, SBFileSpec, GetPath,
147 (char *, size_t), dst_path, "", dst_len);
148
149 uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
150
151 if (result == 0 && dst_path && dst_len > 0)
152 *dst_path = '\0';
153 return result;
154 }
155
operator ->() const156 const lldb_private::FileSpec *SBFileSpec::operator->() const {
157 return m_opaque_up.get();
158 }
159
get() const160 const lldb_private::FileSpec *SBFileSpec::get() const {
161 return m_opaque_up.get();
162 }
163
operator *() const164 const lldb_private::FileSpec &SBFileSpec::operator*() const {
165 return *m_opaque_up;
166 }
167
ref() const168 const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
169
SetFileSpec(const lldb_private::FileSpec & fs)170 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
171 *m_opaque_up = fs;
172 }
173
GetDescription(SBStream & description) const174 bool SBFileSpec::GetDescription(SBStream &description) const {
175 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, GetDescription, (lldb::SBStream &),
176 description);
177
178 Stream &strm = description.ref();
179 char path[PATH_MAX];
180 if (m_opaque_up->GetPath(path, sizeof(path)))
181 strm.PutCString(path);
182 return true;
183 }
184
AppendPathComponent(const char * fn)185 void SBFileSpec::AppendPathComponent(const char *fn) {
186 LLDB_RECORD_METHOD(void, SBFileSpec, AppendPathComponent, (const char *), fn);
187
188 m_opaque_up->AppendPathComponent(fn);
189 }
190
191 namespace lldb_private {
192 namespace repro {
193
194 template <>
RegisterMethods(Registry & R)195 void RegisterMethods<SBFileSpec>(Registry &R) {
196 LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, ());
197 LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const lldb::SBFileSpec &));
198 LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *));
199 LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *, bool));
200 LLDB_REGISTER_METHOD(const lldb::SBFileSpec &,
201 SBFileSpec, operator=,(const lldb::SBFileSpec &));
202 LLDB_REGISTER_METHOD_CONST(bool,
203 SBFileSpec, operator==,(const lldb::SBFileSpec &));
204 LLDB_REGISTER_METHOD_CONST(bool,
205 SBFileSpec, operator!=,(const lldb::SBFileSpec &));
206 LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, IsValid, ());
207 LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, operator bool, ());
208 LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, Exists, ());
209 LLDB_REGISTER_METHOD(bool, SBFileSpec, ResolveExecutableLocation, ());
210 LLDB_REGISTER_STATIC_METHOD(int, SBFileSpec, ResolvePath,
211 (const char *, char *, size_t));
212 LLDB_REGISTER_METHOD_CONST(const char *, SBFileSpec, GetFilename, ());
213 LLDB_REGISTER_METHOD_CONST(const char *, SBFileSpec, GetDirectory, ());
214 LLDB_REGISTER_METHOD(void, SBFileSpec, SetFilename, (const char *));
215 LLDB_REGISTER_METHOD(void, SBFileSpec, SetDirectory, (const char *));
216 LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, GetDescription,
217 (lldb::SBStream &));
218 LLDB_REGISTER_METHOD(void, SBFileSpec, AppendPathComponent, (const char *));
219 LLDB_REGISTER_CHAR_PTR_METHOD_CONST(uint32_t, SBFileSpec, GetPath);
220 }
221
222 }
223 }
224