Path: blob/main/contrib/llvm-project/lldb/source/API/SBFileSpec.cpp
39587 views
//===-- SBFileSpec.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBFileSpec.h"9#include "Utils.h"10#include "lldb/API/SBStream.h"11#include "lldb/Host/FileSystem.h"12#include "lldb/Host/PosixApi.h"13#include "lldb/Utility/FileSpec.h"14#include "lldb/Utility/Instrumentation.h"15#include "lldb/Utility/Stream.h"1617#include "llvm/ADT/SmallString.h"1819#include <cinttypes>20#include <climits>2122using namespace lldb;23using namespace lldb_private;2425SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {26LLDB_INSTRUMENT_VA(this);27}2829SBFileSpec::SBFileSpec(const SBFileSpec &rhs) {30LLDB_INSTRUMENT_VA(this, rhs);3132m_opaque_up = clone(rhs.m_opaque_up);33}3435SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)36: m_opaque_up(new lldb_private::FileSpec(fspec)) {}3738// Deprecated!!!39SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {40LLDB_INSTRUMENT_VA(this, path);4142FileSystem::Instance().Resolve(*m_opaque_up);43}4445SBFileSpec::SBFileSpec(const char *path, bool resolve)46: m_opaque_up(new FileSpec(path)) {47LLDB_INSTRUMENT_VA(this, path, resolve);4849if (resolve)50FileSystem::Instance().Resolve(*m_opaque_up);51}5253SBFileSpec::~SBFileSpec() = default;5455const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {56LLDB_INSTRUMENT_VA(this, rhs);5758if (this != &rhs)59m_opaque_up = clone(rhs.m_opaque_up);60return *this;61}6263bool SBFileSpec::operator==(const SBFileSpec &rhs) const {64LLDB_INSTRUMENT_VA(this, rhs);6566return ref() == rhs.ref();67}6869bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {70LLDB_INSTRUMENT_VA(this, rhs);7172return !(*this == rhs);73}7475bool SBFileSpec::IsValid() const {76LLDB_INSTRUMENT_VA(this);77return this->operator bool();78}79SBFileSpec::operator bool() const {80LLDB_INSTRUMENT_VA(this);8182return m_opaque_up->operator bool();83}8485bool SBFileSpec::Exists() const {86LLDB_INSTRUMENT_VA(this);8788return FileSystem::Instance().Exists(*m_opaque_up);89}9091bool SBFileSpec::ResolveExecutableLocation() {92LLDB_INSTRUMENT_VA(this);9394return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);95}9697int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,98size_t dst_len) {99LLDB_INSTRUMENT_VA(src_path, dst_path, dst_len);100101llvm::SmallString<64> result(src_path);102FileSystem::Instance().Resolve(result);103::snprintf(dst_path, dst_len, "%s", result.c_str());104return std::min(dst_len - 1, result.size());105}106107const char *SBFileSpec::GetFilename() const {108LLDB_INSTRUMENT_VA(this);109110return m_opaque_up->GetFilename().AsCString();111}112113const char *SBFileSpec::GetDirectory() const {114LLDB_INSTRUMENT_VA(this);115116FileSpec directory{*m_opaque_up};117directory.ClearFilename();118return directory.GetPathAsConstString().GetCString();119}120121void SBFileSpec::SetFilename(const char *filename) {122LLDB_INSTRUMENT_VA(this, filename);123124if (filename && filename[0])125m_opaque_up->SetFilename(filename);126else127m_opaque_up->ClearFilename();128}129130void SBFileSpec::SetDirectory(const char *directory) {131LLDB_INSTRUMENT_VA(this, directory);132133if (directory && directory[0])134m_opaque_up->SetDirectory(directory);135else136m_opaque_up->ClearDirectory();137}138139uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {140LLDB_INSTRUMENT_VA(this, dst_path, dst_len);141142uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);143144if (result == 0 && dst_path && dst_len > 0)145*dst_path = '\0';146return result;147}148149const lldb_private::FileSpec *SBFileSpec::operator->() const {150return m_opaque_up.get();151}152153const lldb_private::FileSpec *SBFileSpec::get() const {154return m_opaque_up.get();155}156157const lldb_private::FileSpec &SBFileSpec::operator*() const {158return *m_opaque_up;159}160161const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }162163void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {164*m_opaque_up = fs;165}166167bool SBFileSpec::GetDescription(SBStream &description) const {168LLDB_INSTRUMENT_VA(this, description);169170Stream &strm = description.ref();171char path[PATH_MAX];172if (m_opaque_up->GetPath(path, sizeof(path)))173strm.PutCString(path);174return true;175}176177void SBFileSpec::AppendPathComponent(const char *fn) {178LLDB_INSTRUMENT_VA(this, fn);179180m_opaque_up->AppendPathComponent(fn);181}182183184