SBFileSpec.cpp revision 360784
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
23using namespace lldb;
24using namespace lldb_private;
25
26SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
27  LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpec);
28}
29
30SBFileSpec::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
36SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
37    : m_opaque_up(new lldb_private::FileSpec(fspec)) {}
38
39// Deprecated!!!
40SBFileSpec::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
46SBFileSpec::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
54SBFileSpec::~SBFileSpec() {}
55
56const 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
65bool 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
72bool 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
79bool SBFileSpec::IsValid() const {
80  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, IsValid);
81  return this->operator bool();
82}
83SBFileSpec::operator bool() const {
84  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, operator bool);
85
86  return m_opaque_up->operator bool();
87}
88
89bool SBFileSpec::Exists() const {
90  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, Exists);
91
92  return FileSystem::Instance().Exists(*m_opaque_up);
93}
94
95bool SBFileSpec::ResolveExecutableLocation() {
96  LLDB_RECORD_METHOD_NO_ARGS(bool, SBFileSpec, ResolveExecutableLocation);
97
98  return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
99}
100
101int 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
113const 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
119const 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
127void 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
136void 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
145uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
146  LLDB_RECORD_DUMMY(uint32_t, SBFileSpec, GetPath, (char *, size_t),
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
156const lldb_private::FileSpec *SBFileSpec::operator->() const {
157  return m_opaque_up.get();
158}
159
160const lldb_private::FileSpec *SBFileSpec::get() const {
161  return m_opaque_up.get();
162}
163
164const lldb_private::FileSpec &SBFileSpec::operator*() const {
165  return *m_opaque_up;
166}
167
168const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
169
170void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
171  *m_opaque_up = fs;
172}
173
174bool 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
185void SBFileSpec::AppendPathComponent(const char *fn) {
186  LLDB_RECORD_METHOD(void, SBFileSpec, AppendPathComponent, (const char *), fn);
187
188  m_opaque_up->AppendPathComponent(fn);
189}
190
191namespace lldb_private {
192namespace repro {
193
194template <>
195void 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,
218                             (lldb::SBStream &));
219  LLDB_REGISTER_METHOD(void, SBFileSpec, AppendPathComponent, (const char *));
220}
221
222}
223}
224