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"
18
19#include "llvm/ADT/SmallString.h"
20
21#include <cinttypes>
22#include <climits>
23
24using namespace lldb;
25using namespace lldb_private;
26
30
36
39
40// Deprecated!!!
41SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
42 LLDB_INSTRUMENT_VA(this, path);
43
45}
46
47SBFileSpec::SBFileSpec(const char *path, bool resolve)
48 : m_opaque_up(new FileSpec(path)) {
49 LLDB_INSTRUMENT_VA(this, path, resolve);
50
51 if (resolve)
53}
54
55SBFileSpec::~SBFileSpec() = default;
56
58 LLDB_INSTRUMENT_VA(this, rhs);
59
60 if (this != &rhs)
62 return *this;
63}
64
65bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
66 LLDB_INSTRUMENT_VA(this, rhs);
67
68 return ref() == rhs.ref();
69}
70
71bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
72 LLDB_INSTRUMENT_VA(this, rhs);
73
74 return !(*this == rhs);
75}
76
77bool SBFileSpec::IsValid() const {
79 return this->operator bool();
80}
81SBFileSpec::operator bool() const {
83
84 return m_opaque_up->operator bool();
85}
86
87bool SBFileSpec::Exists() const {
89
91}
92
98
99int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
100 size_t dst_len) {
101 LLDB_INSTRUMENT_VA(src_path, dst_path, dst_len);
102
103 llvm::SmallString<64> result(src_path);
105 ::snprintf(dst_path, dst_len, "%s", result.c_str());
106 return std::min(dst_len - 1, result.size());
107}
108
109const char *SBFileSpec::GetFilename() const {
110 LLDB_INSTRUMENT_VA(this);
111
112 return ConstString(m_opaque_up->GetFilename()).AsCString(nullptr);
113}
114
115const char *SBFileSpec::GetDirectory() const {
116 LLDB_INSTRUMENT_VA(this);
117
118 FileSpec directory{*m_opaque_up};
119 directory.ClearFilename();
120 return ConstString(directory.GetPath()).GetCString();
121}
122
123void SBFileSpec::SetFilename(const char *filename) {
124 LLDB_INSTRUMENT_VA(this, filename);
125
126 if (filename && filename[0])
127 m_opaque_up->SetFilename(filename);
128 else
129 m_opaque_up->ClearFilename();
130}
131
132void SBFileSpec::SetDirectory(const char *directory) {
133 LLDB_INSTRUMENT_VA(this, directory);
134
135 if (directory && directory[0])
136 m_opaque_up->SetDirectory(directory);
137 else
138 m_opaque_up->ClearDirectory();
139}
140
141uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
142 LLDB_INSTRUMENT_VA(this, dst_path, dst_len);
143
144 uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
145
146 if (result == 0 && dst_path && dst_len > 0)
147 *dst_path = '\0';
148 return result;
149}
150
152 return m_opaque_up.get();
153}
154
156 return m_opaque_up.get();
157}
158
162
164
168
169bool SBFileSpec::GetDescription(SBStream &description) const {
170 LLDB_INSTRUMENT_VA(this, description);
171
172 Stream &strm = description.ref();
173 char path[PATH_MAX];
174 if (m_opaque_up->GetPath(path, sizeof(path)))
175 strm.PutCString(path);
176 return true;
177}
178
180 LLDB_INSTRUMENT_VA(this, fn);
181
182 m_opaque_up->AppendPathComponent(fn);
183}
#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 uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
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
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