183 lines · cpp
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-exception6//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"15#include "lldb/Utility/Instrumentation.h"16#include "lldb/Utility/Stream.h"17 18#include "llvm/ADT/SmallString.h"19 20#include <cinttypes>21#include <climits>22 23using namespace lldb;24using namespace lldb_private;25 26SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {27 LLDB_INSTRUMENT_VA(this);28}29 30SBFileSpec::SBFileSpec(const SBFileSpec &rhs) {31 LLDB_INSTRUMENT_VA(this, rhs);32 33 m_opaque_up = clone(rhs.m_opaque_up);34}35 36SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)37 : m_opaque_up(new lldb_private::FileSpec(fspec)) {}38 39// Deprecated!!!40SBFileSpec::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 46SBFileSpec::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 54SBFileSpec::~SBFileSpec() = default;55 56const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {57 LLDB_INSTRUMENT_VA(this, rhs);58 59 if (this != &rhs)60 m_opaque_up = clone(rhs.m_opaque_up);61 return *this;62}63 64bool SBFileSpec::operator==(const SBFileSpec &rhs) const {65 LLDB_INSTRUMENT_VA(this, rhs);66 67 return ref() == rhs.ref();68}69 70bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {71 LLDB_INSTRUMENT_VA(this, rhs);72 73 return !(*this == rhs);74}75 76bool SBFileSpec::IsValid() const {77 LLDB_INSTRUMENT_VA(this);78 return this->operator bool();79}80SBFileSpec::operator bool() const {81 LLDB_INSTRUMENT_VA(this);82 83 return m_opaque_up->operator bool();84}85 86bool SBFileSpec::Exists() const {87 LLDB_INSTRUMENT_VA(this);88 89 return FileSystem::Instance().Exists(*m_opaque_up);90}91 92bool SBFileSpec::ResolveExecutableLocation() {93 LLDB_INSTRUMENT_VA(this);94 95 return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);96}97 98int 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 108const char *SBFileSpec::GetFilename() const {109 LLDB_INSTRUMENT_VA(this);110 111 return m_opaque_up->GetFilename().AsCString();112}113 114const char *SBFileSpec::GetDirectory() const {115 LLDB_INSTRUMENT_VA(this);116 117 FileSpec directory{*m_opaque_up};118 directory.ClearFilename();119 return directory.GetPathAsConstString().GetCString();120}121 122void SBFileSpec::SetFilename(const char *filename) {123 LLDB_INSTRUMENT_VA(this, filename);124 125 if (filename && filename[0])126 m_opaque_up->SetFilename(filename);127 else128 m_opaque_up->ClearFilename();129}130 131void SBFileSpec::SetDirectory(const char *directory) {132 LLDB_INSTRUMENT_VA(this, directory);133 134 if (directory && directory[0])135 m_opaque_up->SetDirectory(directory);136 else137 m_opaque_up->ClearDirectory();138}139 140uint32_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 150const lldb_private::FileSpec *SBFileSpec::operator->() const {151 return m_opaque_up.get();152}153 154const lldb_private::FileSpec *SBFileSpec::get() const {155 return m_opaque_up.get();156}157 158const lldb_private::FileSpec &SBFileSpec::operator*() const {159 return *m_opaque_up;160}161 162const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }163 164void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {165 *m_opaque_up = fs;166}167 168bool 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 178void SBFileSpec::AppendPathComponent(const char *fn) {179 LLDB_INSTRUMENT_VA(this, fn);180 181 m_opaque_up->AppendPathComponent(fn);182}183