LLDB  mainline
SBFileSpec.cpp
Go to the documentation of this file.
1 //===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===//
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 
26 SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
28 }
29 
30 SBFileSpec::SBFileSpec(const SBFileSpec &rhs) : m_opaque_up() {
32 
33  m_opaque_up = clone(rhs.m_opaque_up);
34 }
35 
37  : m_opaque_up(new lldb_private::FileSpec(fspec)) {}
38 
39 // Deprecated!!!
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 
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 
55 
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 
65 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 
72 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 
79 bool SBFileSpec::IsValid() const {
81  return this->operator bool();
82 }
83 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 
89 bool SBFileSpec::Exists() const {
91 
92  return FileSystem::Instance().Exists(*m_opaque_up);
93 }
94 
97 
98  return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
99 }
100 
101 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
102  size_t dst_len) {
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 
113 const char *SBFileSpec::GetFilename() const {
115 
116  return m_opaque_up->GetFilename().AsCString();
117 }
118 
119 const char *SBFileSpec::GetDirectory() const {
121 
122  FileSpec directory{*m_opaque_up};
123  directory.GetFilename().Clear();
124  return directory.GetCString();
125 }
126 
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 
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 
145 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
147  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 
156 const lldb_private::FileSpec *SBFileSpec::operator->() const {
157  return m_opaque_up.get();
158 }
159 
160 const lldb_private::FileSpec *SBFileSpec::get() const {
161  return m_opaque_up.get();
162 }
163 
165  return *m_opaque_up;
166 }
167 
168 const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
169 
170 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
171  *m_opaque_up = fs;
172 }
173 
174 bool SBFileSpec::GetDescription(SBStream &description) const {
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 
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 <>
198  LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *));
199  LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *, bool));
201  SBFileSpec, operator=,(const lldb::SBFileSpec &));
203  SBFileSpec, operator==,(const lldb::SBFileSpec &));
205  SBFileSpec, operator!=,(const lldb::SBFileSpec &));
207  LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, operator bool, ());
211  (const char *, char *, size_t));
214  LLDB_REGISTER_METHOD(void, SBFileSpec, SetFilename, (const char *));
215  LLDB_REGISTER_METHOD(void, SBFileSpec, SetDirectory, (const char *));
218  (lldb::SBStream &));
219  LLDB_REGISTER_METHOD(void, SBFileSpec, AppendPathComponent, (const char *));
220 }
221 
222 }
223 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
The registry contains a unique mapping between functions and their ID.
const SBFileSpec & operator=(const lldb::SBFileSpec &rhs)
Definition: SBFileSpec.cpp:56
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
void SetDirectory(const char *directory)
Definition: SBFileSpec.cpp:136
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
const char * GetFilename() const
Definition: SBFileSpec.cpp:113
#define LLDB_RECORD_STATIC_METHOD(Result, Class, Method, Signature,...)
void SetFilename(const char *filename)
Definition: SBFileSpec.cpp:127
A file utility class.
Definition: FileSpec.h:55
bool ResolveExecutableLocation()
Definition: SBFileSpec.cpp:95
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
const Scalar operator*(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2161
bool GetDescription(lldb::SBStream &description) const
Definition: SBFileSpec.cpp:174
bool IsValid() const
Definition: SBFileSpec.cpp:79
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
bool Exists() const
Definition: SBFileSpec.cpp:89
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
void RegisterMethods< SBFileSpec >(Registry &R)
Definition: SBFileSpec.cpp:195
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
bool operator!=(const SBFileSpec &rhs) const
Definition: SBFileSpec.cpp:72
lldb_private::Stream & ref()
Definition: SBStream.cpp:162
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
bool operator==(const SBFileSpec &rhs) const
Definition: SBFileSpec.cpp:65
uint32_t GetPath(char *dst_path, size_t dst_len) const
Definition: SBFileSpec.cpp:145
Definition: SBAddress.h:15
#define PATH_MAX
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
static int ResolvePath(const char *src_path, char *dst_path, size_t dst_len)
Definition: SBFileSpec.cpp:101
const char * GetDirectory() const
Definition: SBFileSpec.cpp:119
void AppendPathComponent(const char *file_or_directory)
Definition: SBFileSpec.cpp:185
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)