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 23 using namespace lldb; 24 using namespace lldb_private; 25 26 SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) { 27 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpec); 28 } 29 30 SBFileSpec::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 36 SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec) 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() {} 55 56 const 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 *this; 63 } 64 65 bool SBFileSpec::IsValid() const { 66 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, IsValid); 67 return this->operator bool(); 68 } 69 SBFileSpec::operator bool() const { 70 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, operator bool); 71 72 return m_opaque_up->operator bool(); 73 } 74 75 bool SBFileSpec::Exists() const { 76 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, Exists); 77 78 return FileSystem::Instance().Exists(*m_opaque_up); 79 } 80 81 bool SBFileSpec::ResolveExecutableLocation() { 82 LLDB_RECORD_METHOD_NO_ARGS(bool, SBFileSpec, ResolveExecutableLocation); 83 84 return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up); 85 } 86 87 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path, 88 size_t dst_len) { 89 LLDB_RECORD_STATIC_METHOD(int, SBFileSpec, ResolvePath, 90 (const char *, char *, size_t), src_path, dst_path, 91 dst_len); 92 93 llvm::SmallString<64> result(src_path); 94 FileSystem::Instance().Resolve(result); 95 ::snprintf(dst_path, dst_len, "%s", result.c_str()); 96 return std::min(dst_len - 1, result.size()); 97 } 98 99 const char *SBFileSpec::GetFilename() const { 100 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec, GetFilename); 101 102 return m_opaque_up->GetFilename().AsCString(); 103 } 104 105 const char *SBFileSpec::GetDirectory() const { 106 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec, GetDirectory); 107 108 FileSpec directory{*m_opaque_up}; 109 directory.GetFilename().Clear(); 110 return directory.GetCString(); 111 } 112 113 void SBFileSpec::SetFilename(const char *filename) { 114 LLDB_RECORD_METHOD(void, SBFileSpec, SetFilename, (const char *), filename); 115 116 if (filename && filename[0]) 117 m_opaque_up->GetFilename().SetCString(filename); 118 else 119 m_opaque_up->GetFilename().Clear(); 120 } 121 122 void SBFileSpec::SetDirectory(const char *directory) { 123 LLDB_RECORD_METHOD(void, SBFileSpec, SetDirectory, (const char *), directory); 124 125 if (directory && directory[0]) 126 m_opaque_up->GetDirectory().SetCString(directory); 127 else 128 m_opaque_up->GetDirectory().Clear(); 129 } 130 131 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const { 132 LLDB_RECORD_METHOD_CONST(uint32_t, SBFileSpec, GetPath, (char *, size_t), 133 dst_path, dst_len); 134 135 uint32_t result = m_opaque_up->GetPath(dst_path, dst_len); 136 137 if (result == 0 && dst_path && dst_len > 0) 138 *dst_path = '\0'; 139 return result; 140 } 141 142 const lldb_private::FileSpec *SBFileSpec::operator->() const { 143 return m_opaque_up.get(); 144 } 145 146 const lldb_private::FileSpec *SBFileSpec::get() const { 147 return m_opaque_up.get(); 148 } 149 150 const lldb_private::FileSpec &SBFileSpec::operator*() const { 151 return *m_opaque_up; 152 } 153 154 const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; } 155 156 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) { 157 *m_opaque_up = fs; 158 } 159 160 bool SBFileSpec::GetDescription(SBStream &description) const { 161 LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, GetDescription, (lldb::SBStream &), 162 description); 163 164 Stream &strm = description.ref(); 165 char path[PATH_MAX]; 166 if (m_opaque_up->GetPath(path, sizeof(path))) 167 strm.PutCString(path); 168 return true; 169 } 170 171 void SBFileSpec::AppendPathComponent(const char *fn) { 172 LLDB_RECORD_METHOD(void, SBFileSpec, AppendPathComponent, (const char *), fn); 173 174 m_opaque_up->AppendPathComponent(fn); 175 } 176