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
10#include "Utils.h"
11#include "lldb/API/SBStream.h"
13#include "lldb/Host/PosixApi.h"
17#include "lldb/Utility/Stream.h"
19
20#include "llvm/ADT/SmallString.h"
21
22#include <cinttypes>
23#include <climits>
24
25using namespace lldb;
26using namespace lldb_private;
27
31
37
40
41// Deprecated!!!
42SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
43 LLDB_INSTRUMENT_VA(this, path);
44
46}
47
48SBFileSpec::SBFileSpec(const char *path, bool resolve)
49 : m_opaque_up(new FileSpec(path)) {
50 LLDB_INSTRUMENT_VA(this, path, resolve);
51
52 if (resolve)
54}
55
56SBFileSpec::~SBFileSpec() = default;
57
59 LLDB_INSTRUMENT_VA(this, rhs);
60
61 if (this != &rhs)
63 return *this;
64}
65
66bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
67 LLDB_INSTRUMENT_VA(this, rhs);
68
69 return ref() == rhs.ref();
70}
71
72bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
73 LLDB_INSTRUMENT_VA(this, rhs);
74
75 return !(*this == rhs);
76}
77
78bool SBFileSpec::IsValid() const {
80 return this->operator bool();
81}
82SBFileSpec::operator bool() const {
84
85 return m_opaque_up->operator bool();
86}
87
88bool SBFileSpec::Exists() const {
90
92}
93
99
100int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
101 size_t dst_len) {
102 LLDB_INSTRUMENT_VA(src_path, dst_path, dst_len);
103
104 llvm::SmallString<64> result(src_path);
106 ::snprintf(dst_path, dst_len, "%s", result.c_str());
107 return std::min(dst_len - 1, result.size());
108}
109
110const char *SBFileSpec::GetFilename() const {
111 LLDB_INSTRUMENT_VA(this);
112
114}
115
116const char *SBFileSpec::GetDirectory() const {
117 LLDB_INSTRUMENT_VA(this);
118
119 FileSpec directory{*m_opaque_up};
120 directory.ClearFilename();
121 return StringPool::GetSystemPool().Intern(directory.GetPath());
122}
123
124void SBFileSpec::SetFilename(const char *filename) {
125 LLDB_INSTRUMENT_VA(this, filename);
126
127 if (filename && filename[0])
128 m_opaque_up->SetFilename(filename);
129 else
130 m_opaque_up->ClearFilename();
131}
132
133void SBFileSpec::SetDirectory(const char *directory) {
134 LLDB_INSTRUMENT_VA(this, directory);
135
136 if (directory && directory[0])
137 m_opaque_up->SetDirectory(directory);
138 else
139 m_opaque_up->ClearDirectory();
140}
141
142uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
143 LLDB_INSTRUMENT_VA(this, dst_path, dst_len);
144
145 uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
146
147 if (result == 0 && dst_path && dst_len > 0)
148 *dst_path = '\0';
149 return result;
150}
151
153 return m_opaque_up.get();
154}
155
157 return m_opaque_up.get();
158}
159
163
165
169
170bool SBFileSpec::GetDescription(SBStream &description) const {
171 LLDB_INSTRUMENT_VA(this, description);
172
173 Stream &strm = description.ref();
174 char path[PATH_MAX];
175 if (m_opaque_up->GetPath(path, sizeof(path)))
176 strm.PutCString(path);
177 return true;
178}
179
181 LLDB_INSTRUMENT_VA(this, fn);
182
183 m_opaque_up->AppendPathComponent(fn);
184}
#define LLDB_INSTRUMENT_VA(...)
void SetFilename(const char *filename)
void SetFileSpec(const lldb_private::FileSpec &fspec)
void AppendPathComponent(const char *file_or_directory)
static int ResolvePath(const char *src_path, char *dst_path, size_t dst_len)
bool IsValid() const
const SBFileSpec & operator=(const lldb::SBFileSpec &rhs)
const lldb_private::FileSpec & operator*() const
const lldb_private::FileSpec & ref() const
uint32_t GetPath(char *dst_path, size_t dst_len) const
bool Exists() const
const lldb_private::FileSpec * get() const
const char * GetFilename() const
bool GetDescription(lldb::SBStream &description) const
bool ResolveExecutableLocation()
void SetDirectory(const char *directory)
const char * GetDirectory() const
bool operator!=(const SBFileSpec &rhs) const
bool operator==(const SBFileSpec &rhs) const
std::unique_ptr< lldb_private::FileSpec > m_opaque_up
Definition SBFileSpec.h:95
const lldb_private::FileSpec * operator->() const
lldb_private::Stream & ref()
Definition SBStream.cpp:179
A file utility class.
Definition FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:380
void ClearFilename()
Clear the filename in this object.
Definition FileSpec.cpp:368
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
const char * InternNonEmpty(llvm::StringRef str) const
Like Intern, but an empty string yields nullptr.
Definition StringPool.h:81
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
A class that represents a running process on the host machine.
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition Utils.h:17
#define PATH_MAX