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 "Utils.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/PosixApi.h"
14 #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()) {
27  LLDB_INSTRUMENT_VA(this);
28 }
29 
31  LLDB_INSTRUMENT_VA(this, rhs);
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_INSTRUMENT_VA(this, 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_INSTRUMENT_VA(this, path, resolve);
49 
50  if (resolve)
51  FileSystem::Instance().Resolve(*m_opaque_up);
52 }
53 
54 SBFileSpec::~SBFileSpec() = default;
55 
57  LLDB_INSTRUMENT_VA(this, rhs);
58 
59  if (this != &rhs)
61  return *this;
62 }
63 
64 bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
65  LLDB_INSTRUMENT_VA(this, rhs);
66 
67  return ref() == rhs.ref();
68 }
69 
70 bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
71  LLDB_INSTRUMENT_VA(this, rhs);
72 
73  return !(*this == rhs);
74 }
75 
76 bool SBFileSpec::IsValid() const {
77  LLDB_INSTRUMENT_VA(this);
78  return this->operator bool();
79 }
80 SBFileSpec::operator bool() const {
81  LLDB_INSTRUMENT_VA(this);
82 
83  return m_opaque_up->operator bool();
84 }
85 
86 bool SBFileSpec::Exists() const {
87  LLDB_INSTRUMENT_VA(this);
88 
89  return FileSystem::Instance().Exists(*m_opaque_up);
90 }
91 
93  LLDB_INSTRUMENT_VA(this);
94 
95  return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
96 }
97 
98 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
99  size_t dst_len) {
100  LLDB_INSTRUMENT_VA(src_path, dst_path, dst_len);
101 
102  llvm::SmallString<64> result(src_path);
103  FileSystem::Instance().Resolve(result);
104  ::snprintf(dst_path, dst_len, "%s", result.c_str());
105  return std::min(dst_len - 1, result.size());
106 }
107 
108 const char *SBFileSpec::GetFilename() const {
109  LLDB_INSTRUMENT_VA(this);
110 
111  return m_opaque_up->GetFilename().AsCString();
112 }
113 
114 const char *SBFileSpec::GetDirectory() const {
115  LLDB_INSTRUMENT_VA(this);
116 
117  FileSpec directory{*m_opaque_up};
118  directory.GetFilename().Clear();
119  return directory.GetCString();
120 }
121 
122 void SBFileSpec::SetFilename(const char *filename) {
123  LLDB_INSTRUMENT_VA(this, filename);
124 
125  if (filename && filename[0])
126  m_opaque_up->GetFilename().SetCString(filename);
127  else
128  m_opaque_up->GetFilename().Clear();
129 }
130 
131 void SBFileSpec::SetDirectory(const char *directory) {
132  LLDB_INSTRUMENT_VA(this, directory);
133 
134  if (directory && directory[0])
135  m_opaque_up->GetDirectory().SetCString(directory);
136  else
137  m_opaque_up->GetDirectory().Clear();
138 }
139 
140 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
141  LLDB_INSTRUMENT_VA(this, dst_path, dst_len);
142 
143  uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
144 
145  if (result == 0 && dst_path && dst_len > 0)
146  *dst_path = '\0';
147  return result;
148 }
149 
151  return m_opaque_up.get();
152 }
153 
155  return m_opaque_up.get();
156 }
157 
159  return *m_opaque_up;
160 }
161 
163 
165  *m_opaque_up = fs;
166 }
167 
168 bool SBFileSpec::GetDescription(SBStream &description) const {
169  LLDB_INSTRUMENT_VA(this, description);
170 
171  Stream &strm = description.ref();
172  char path[PATH_MAX];
173  if (m_opaque_up->GetPath(path, sizeof(path)))
174  strm.PutCString(path);
175  return true;
176 }
177 
178 void SBFileSpec::AppendPathComponent(const char *fn) {
179  LLDB_INSTRUMENT_VA(this, fn);
180 
181  m_opaque_up->AppendPathComponent(fn);
182 }
lldb::SBFileSpec::get
const lldb_private::FileSpec * get() const
Definition: SBFileSpec.cpp:154
Instrumentation.h
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:131
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
FileSystem.h
lldb::SBFileSpec::operator*
const lldb_private::FileSpec & operator*() const
Definition: SBFileSpec.cpp:158
lldb_private::Stream
Definition: Stream.h:28
lldb::SBFileSpec::operator==
bool operator==(const SBFileSpec &rhs) const
Definition: SBFileSpec.cpp:64
lldb::SBFileSpec::~SBFileSpec
~SBFileSpec()
lldb::SBFileSpec::operator=
const SBFileSpec & operator=(const lldb::SBFileSpec &rhs)
Definition: SBFileSpec.cpp:56
lldb::SBFileSpec::SetFileSpec
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:164
lldb::SBFileSpec::GetDescription
bool GetDescription(lldb::SBStream &description) const
Definition: SBFileSpec.cpp:168
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb::SBFileSpec::operator!=
bool operator!=(const SBFileSpec &rhs) const
Definition: SBFileSpec.cpp:70
SBStream.h
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:162
lldb::SBFileSpec::IsValid
bool IsValid() const
Definition: SBFileSpec.cpp:76
lldb::SBFileSpec::SBFileSpec
SBFileSpec()
Definition: SBFileSpec.cpp:26
lldb::SBFileSpec::operator->
const lldb_private::FileSpec * operator->() const
Definition: SBFileSpec.cpp:150
lldb::SBFileSpec::AppendPathComponent
void AppendPathComponent(const char *file_or_directory)
Definition: SBFileSpec.cpp:178
uint32_t
lldb::SBFileSpec::ResolveExecutableLocation
bool ResolveExecutableLocation()
Definition: SBFileSpec.cpp:92
lldb::SBFileSpec::GetPath
uint32_t GetPath(char *dst_path, size_t dst_len) const
Definition: SBFileSpec.cpp:140
lldb::SBFileSpec::GetFilename
const char * GetFilename() const
Definition: SBFileSpec.cpp:108
lldb::SBFileSpec::GetDirectory
const char * GetDirectory() const
Definition: SBFileSpec.cpp:114
lldb::SBFileSpec
Definition: SBFileSpec.h:16
lldb::SBFileSpec::Exists
bool Exists() const
Definition: SBFileSpec.cpp:86
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
PosixApi.h
FileSpec.h
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
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:122
lldb::SBStream
Definition: SBStream.h:18
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb
Definition: SBAddress.h:15
lldb::SBFileSpec::ResolvePath
static int ResolvePath(const char *src_path, char *dst_path, size_t dst_len)
Definition: SBFileSpec.cpp:98