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