1 //===-- SBFileSpec.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/API/SBFileSpec.h"
10 #include "SBReproducerPrivate.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"
18 #include "llvm/ADT/SmallString.h"
24 using namespace lldb_private
;
26 SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
27 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpec
);
30 SBFileSpec::SBFileSpec(const SBFileSpec
&rhs
) : m_opaque_up() {
31 LLDB_RECORD_CONSTRUCTOR(SBFileSpec
, (const lldb::SBFileSpec
&), rhs
);
33 m_opaque_up
= clone(rhs
.m_opaque_up
);
36 SBFileSpec::SBFileSpec(const lldb_private::FileSpec
&fspec
)
37 : m_opaque_up(new lldb_private::FileSpec(fspec
)) {}
40 SBFileSpec::SBFileSpec(const char *path
) : m_opaque_up(new FileSpec(path
)) {
41 LLDB_RECORD_CONSTRUCTOR(SBFileSpec
, (const char *), path
);
43 FileSystem::Instance().Resolve(*m_opaque_up
);
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
);
51 FileSystem::Instance().Resolve(*m_opaque_up
);
54 SBFileSpec::~SBFileSpec() {}
56 const SBFileSpec
&SBFileSpec::operator=(const SBFileSpec
&rhs
) {
57 LLDB_RECORD_METHOD(const lldb::SBFileSpec
&,
58 SBFileSpec
, operator=,(const lldb::SBFileSpec
&), rhs
);
61 m_opaque_up
= clone(rhs
.m_opaque_up
);
62 return LLDB_RECORD_RESULT(*this);
65 bool SBFileSpec::operator==(const SBFileSpec
&rhs
) const {
66 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec
, operator==,(const SBFileSpec
&rhs
),
69 return ref() == rhs
.ref();
72 bool SBFileSpec::operator!=(const SBFileSpec
&rhs
) const {
73 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec
, operator!=,(const SBFileSpec
&rhs
),
76 return !(*this == rhs
);
79 bool SBFileSpec::IsValid() const {
80 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec
, IsValid
);
81 return this->operator bool();
83 SBFileSpec::operator bool() const {
84 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec
, operator bool);
86 return m_opaque_up
->operator bool();
89 bool SBFileSpec::Exists() const {
90 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec
, Exists
);
92 return FileSystem::Instance().Exists(*m_opaque_up
);
95 bool SBFileSpec::ResolveExecutableLocation() {
96 LLDB_RECORD_METHOD_NO_ARGS(bool, SBFileSpec
, ResolveExecutableLocation
);
98 return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up
);
101 int SBFileSpec::ResolvePath(const char *src_path
, char *dst_path
,
103 LLDB_RECORD_STATIC_METHOD(int, SBFileSpec
, ResolvePath
,
104 (const char *, char *, size_t), src_path
, dst_path
,
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());
113 const char *SBFileSpec::GetFilename() const {
114 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec
, GetFilename
);
116 return m_opaque_up
->GetFilename().AsCString();
119 const char *SBFileSpec::GetDirectory() const {
120 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec
, GetDirectory
);
122 FileSpec directory
{*m_opaque_up
};
123 directory
.GetFilename().Clear();
124 return directory
.GetCString();
127 void SBFileSpec::SetFilename(const char *filename
) {
128 LLDB_RECORD_METHOD(void, SBFileSpec
, SetFilename
, (const char *), filename
);
130 if (filename
&& filename
[0])
131 m_opaque_up
->GetFilename().SetCString(filename
);
133 m_opaque_up
->GetFilename().Clear();
136 void SBFileSpec::SetDirectory(const char *directory
) {
137 LLDB_RECORD_METHOD(void, SBFileSpec
, SetDirectory
, (const char *), directory
);
139 if (directory
&& directory
[0])
140 m_opaque_up
->GetDirectory().SetCString(directory
);
142 m_opaque_up
->GetDirectory().Clear();
145 uint32_t SBFileSpec::GetPath(char *dst_path
, size_t dst_len
) const {
146 LLDB_RECORD_DUMMY(uint32_t, SBFileSpec
, GetPath
, (char *, size_t),
149 uint32_t result
= m_opaque_up
->GetPath(dst_path
, dst_len
);
151 if (result
== 0 && dst_path
&& dst_len
> 0)
156 const lldb_private::FileSpec
*SBFileSpec::operator->() const {
157 return m_opaque_up
.get();
160 const lldb_private::FileSpec
*SBFileSpec::get() const {
161 return m_opaque_up
.get();
164 const lldb_private::FileSpec
&SBFileSpec::operator*() const {
168 const lldb_private::FileSpec
&SBFileSpec::ref() const { return *m_opaque_up
; }
170 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec
&fs
) {
174 bool SBFileSpec::GetDescription(SBStream
&description
) const {
175 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec
, GetDescription
, (lldb::SBStream
&),
178 Stream
&strm
= description
.ref();
180 if (m_opaque_up
->GetPath(path
, sizeof(path
)))
181 strm
.PutCString(path
);
185 void SBFileSpec::AppendPathComponent(const char *fn
) {
186 LLDB_RECORD_METHOD(void, SBFileSpec
, AppendPathComponent
, (const char *), fn
);
188 m_opaque_up
->AppendPathComponent(fn
);
191 namespace lldb_private
{
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(uint32_t, SBFileSpec
, GetPath
, (char *, size_t));
217 LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec
, GetDescription
,
219 LLDB_REGISTER_METHOD(void, SBFileSpec
, AppendPathComponent
, (const char *));