LLDB  mainline
SBFileSpec.cpp
Go to the documentation of this file.
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 <cinttypes>
21 #include <climits>
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 
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 
54 SBFileSpec::~SBFileSpec() = default;
55 
58  SBFileSpec, operator=,(const lldb::SBFileSpec &), rhs);
59 
60  if (this != &rhs)
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  (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 
157  return m_opaque_up.get();
158 }
159 
161  return m_opaque_up.get();
162 }
163 
165  return *m_opaque_up;
166 }
167 
169 
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 &));
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 *));
220 }
221 
222 }
223 }
lldb::SBFileSpec::get
const lldb_private::FileSpec * get() const
Definition: SBFileSpec.cpp:160
lldb::SBFileSpec::m_opaque_up
std::unique_ptr< lldb_private::FileSpec > m_opaque_up
Definition: SBFileSpec.h:90
lldb::SBFileSpec::SetDirectory
void SetDirectory(const char *directory)
Definition: SBFileSpec.cpp:136
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
FileSystem.h
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
LLDB_RECORD_CHAR_PTR_METHOD_CONST
#define LLDB_RECORD_CHAR_PTR_METHOD_CONST(Result, Class, Method, Signature, StrOut,...)
Definition: ReproducerInstrumentation.h:201
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb_private::repro::RegisterMethods< SBFileSpec >
void RegisterMethods< SBFileSpec >(Registry &R)
Definition: SBFileSpec.cpp:195
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
lldb::SBFileSpec::operator*
const lldb_private::FileSpec & operator*() const
Definition: SBFileSpec.cpp:164
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
lldb_private::Stream
Definition: Stream.h:28
lldb::SBFileSpec::operator==
bool operator==(const SBFileSpec &rhs) const
Definition: SBFileSpec.cpp:65
lldb::SBFileSpec::~SBFileSpec
~SBFileSpec()
SBReproducerPrivate.h
lldb::SBFileSpec::operator=
const SBFileSpec & operator=(const lldb::SBFileSpec &rhs)
Definition: SBFileSpec.cpp:56
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBFileSpec::SetFileSpec
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:170
lldb::SBFileSpec::GetDescription
bool GetDescription(lldb::SBStream &description) const
Definition: SBFileSpec.cpp:174
R
#define R(base)
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb::SBFileSpec::operator!=
bool operator!=(const SBFileSpec &rhs) const
Definition: SBFileSpec.cpp:72
SBStream.h
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Utils.h
SBFileSpec.h
lldb::SBFileSpec::ref
const lldb_private::FileSpec & ref() const
Definition: SBFileSpec.cpp:168
lldb::SBFileSpec::IsValid
bool IsValid() const
Definition: SBFileSpec.cpp:79
LLDB_REGISTER_STATIC_METHOD
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:103
lldb::SBFileSpec::SBFileSpec
SBFileSpec()
Definition: SBFileSpec.cpp:26
lldb::SBFileSpec::operator->
const lldb_private::FileSpec * operator->() const
Definition: SBFileSpec.cpp:156
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb::SBFileSpec::AppendPathComponent
void AppendPathComponent(const char *file_or_directory)
Definition: SBFileSpec.cpp:185
uint32_t
lldb::SBFileSpec::ResolveExecutableLocation
bool ResolveExecutableLocation()
Definition: SBFileSpec.cpp:95
lldb::SBFileSpec::GetPath
uint32_t GetPath(char *dst_path, size_t dst_len) const
Definition: SBFileSpec.cpp:145
lldb::SBFileSpec::GetFilename
const char * GetFilename() const
Definition: SBFileSpec.cpp:113
LLDB_REGISTER_CHAR_PTR_METHOD_CONST
#define LLDB_REGISTER_CHAR_PTR_METHOD_CONST(Result, Class, Method)
Definition: ReproducerInstrumentation.h:121
lldb::SBFileSpec::GetDirectory
const char * GetDirectory() const
Definition: SBFileSpec.cpp:119
lldb::SBFileSpec
Definition: SBFileSpec.h:16
lldb::SBFileSpec::Exists
bool Exists() const
Definition: SBFileSpec.cpp:89
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
PosixApi.h
FileSpec.h
LLDB_RECORD_STATIC_METHOD
#define LLDB_RECORD_STATIC_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:171
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb::SBFileSpec::SetFilename
void SetFilename(const char *filename)
Definition: SBFileSpec.cpp:127
lldb::SBStream
Definition: SBStream.h:18
LLDB_RECORD_METHOD_CONST
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:161
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb
Definition: SBAddress.h:15
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93
lldb::SBFileSpec::ResolvePath
static int ResolvePath(const char *src_path, char *dst_path, size_t dst_len)
Definition: SBFileSpec.cpp:101