15ffd83dbSDimitry Andric //===-- SBFileSpec.cpp ----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/API/SBFileSpec.h"
100b57cec5SDimitry Andric #include "SBReproducerPrivate.h"
110b57cec5SDimitry Andric #include "Utils.h"
120b57cec5SDimitry Andric #include "lldb/API/SBStream.h"
130b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
140b57cec5SDimitry Andric #include "lldb/Host/PosixApi.h"
150b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
190b57cec5SDimitry Andric
20*5f7ddb14SDimitry Andric #include <cinttypes>
21*5f7ddb14SDimitry Andric #include <climits>
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric using namespace lldb;
240b57cec5SDimitry Andric using namespace lldb_private;
250b57cec5SDimitry Andric
SBFileSpec()260b57cec5SDimitry Andric SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
270b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFileSpec);
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric
SBFileSpec(const SBFileSpec & rhs)300b57cec5SDimitry Andric SBFileSpec::SBFileSpec(const SBFileSpec &rhs) : m_opaque_up() {
310b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBFileSpec, (const lldb::SBFileSpec &), rhs);
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric m_opaque_up = clone(rhs.m_opaque_up);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric
SBFileSpec(const lldb_private::FileSpec & fspec)360b57cec5SDimitry Andric SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
370b57cec5SDimitry Andric : m_opaque_up(new lldb_private::FileSpec(fspec)) {}
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric // Deprecated!!!
SBFileSpec(const char * path)400b57cec5SDimitry Andric SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
410b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBFileSpec, (const char *), path);
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric FileSystem::Instance().Resolve(*m_opaque_up);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric
SBFileSpec(const char * path,bool resolve)460b57cec5SDimitry Andric SBFileSpec::SBFileSpec(const char *path, bool resolve)
470b57cec5SDimitry Andric : m_opaque_up(new FileSpec(path)) {
480b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBFileSpec, (const char *, bool), path, resolve);
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric if (resolve)
510b57cec5SDimitry Andric FileSystem::Instance().Resolve(*m_opaque_up);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric
545ffd83dbSDimitry Andric SBFileSpec::~SBFileSpec() = default;
550b57cec5SDimitry Andric
operator =(const SBFileSpec & rhs)560b57cec5SDimitry Andric const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
570b57cec5SDimitry Andric LLDB_RECORD_METHOD(const lldb::SBFileSpec &,
580b57cec5SDimitry Andric SBFileSpec, operator=,(const lldb::SBFileSpec &), rhs);
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric if (this != &rhs)
610b57cec5SDimitry Andric m_opaque_up = clone(rhs.m_opaque_up);
620b57cec5SDimitry Andric return LLDB_RECORD_RESULT(*this);
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
operator ==(const SBFileSpec & rhs) const650b57cec5SDimitry Andric bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
660b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, operator==,(const SBFileSpec &rhs),
670b57cec5SDimitry Andric rhs);
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric return ref() == rhs.ref();
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric
operator !=(const SBFileSpec & rhs) const720b57cec5SDimitry Andric bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
730b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, operator!=,(const SBFileSpec &rhs),
740b57cec5SDimitry Andric rhs);
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric return !(*this == rhs);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
IsValid() const790b57cec5SDimitry Andric bool SBFileSpec::IsValid() const {
800b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, IsValid);
810b57cec5SDimitry Andric return this->operator bool();
820b57cec5SDimitry Andric }
operator bool() const830b57cec5SDimitry Andric SBFileSpec::operator bool() const {
840b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, operator bool);
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric return m_opaque_up->operator bool();
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
Exists() const890b57cec5SDimitry Andric bool SBFileSpec::Exists() const {
900b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFileSpec, Exists);
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric return FileSystem::Instance().Exists(*m_opaque_up);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
ResolveExecutableLocation()950b57cec5SDimitry Andric bool SBFileSpec::ResolveExecutableLocation() {
960b57cec5SDimitry Andric LLDB_RECORD_METHOD_NO_ARGS(bool, SBFileSpec, ResolveExecutableLocation);
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric
ResolvePath(const char * src_path,char * dst_path,size_t dst_len)1010b57cec5SDimitry Andric int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
1020b57cec5SDimitry Andric size_t dst_len) {
1030b57cec5SDimitry Andric LLDB_RECORD_STATIC_METHOD(int, SBFileSpec, ResolvePath,
1040b57cec5SDimitry Andric (const char *, char *, size_t), src_path, dst_path,
1050b57cec5SDimitry Andric dst_len);
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric llvm::SmallString<64> result(src_path);
1080b57cec5SDimitry Andric FileSystem::Instance().Resolve(result);
1090b57cec5SDimitry Andric ::snprintf(dst_path, dst_len, "%s", result.c_str());
1100b57cec5SDimitry Andric return std::min(dst_len - 1, result.size());
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric
GetFilename() const1130b57cec5SDimitry Andric const char *SBFileSpec::GetFilename() const {
1140b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec, GetFilename);
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric return m_opaque_up->GetFilename().AsCString();
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric
GetDirectory() const1190b57cec5SDimitry Andric const char *SBFileSpec::GetDirectory() const {
1200b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBFileSpec, GetDirectory);
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric FileSpec directory{*m_opaque_up};
1230b57cec5SDimitry Andric directory.GetFilename().Clear();
1240b57cec5SDimitry Andric return directory.GetCString();
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
SetFilename(const char * filename)1270b57cec5SDimitry Andric void SBFileSpec::SetFilename(const char *filename) {
1280b57cec5SDimitry Andric LLDB_RECORD_METHOD(void, SBFileSpec, SetFilename, (const char *), filename);
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric if (filename && filename[0])
1310b57cec5SDimitry Andric m_opaque_up->GetFilename().SetCString(filename);
1320b57cec5SDimitry Andric else
1330b57cec5SDimitry Andric m_opaque_up->GetFilename().Clear();
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric
SetDirectory(const char * directory)1360b57cec5SDimitry Andric void SBFileSpec::SetDirectory(const char *directory) {
1370b57cec5SDimitry Andric LLDB_RECORD_METHOD(void, SBFileSpec, SetDirectory, (const char *), directory);
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric if (directory && directory[0])
1400b57cec5SDimitry Andric m_opaque_up->GetDirectory().SetCString(directory);
1410b57cec5SDimitry Andric else
1420b57cec5SDimitry Andric m_opaque_up->GetDirectory().Clear();
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric
GetPath(char * dst_path,size_t dst_len) const1450b57cec5SDimitry Andric uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
1465ffd83dbSDimitry Andric LLDB_RECORD_CHAR_PTR_METHOD_CONST(uint32_t, SBFileSpec, GetPath,
1475ffd83dbSDimitry Andric (char *, size_t), dst_path, "", dst_len);
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric if (result == 0 && dst_path && dst_len > 0)
1520b57cec5SDimitry Andric *dst_path = '\0';
1530b57cec5SDimitry Andric return result;
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric
operator ->() const1560b57cec5SDimitry Andric const lldb_private::FileSpec *SBFileSpec::operator->() const {
1570b57cec5SDimitry Andric return m_opaque_up.get();
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric
get() const1600b57cec5SDimitry Andric const lldb_private::FileSpec *SBFileSpec::get() const {
1610b57cec5SDimitry Andric return m_opaque_up.get();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric
operator *() const1640b57cec5SDimitry Andric const lldb_private::FileSpec &SBFileSpec::operator*() const {
1650b57cec5SDimitry Andric return *m_opaque_up;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
ref() const1680b57cec5SDimitry Andric const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
1690b57cec5SDimitry Andric
SetFileSpec(const lldb_private::FileSpec & fs)1700b57cec5SDimitry Andric void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
1710b57cec5SDimitry Andric *m_opaque_up = fs;
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric
GetDescription(SBStream & description) const1740b57cec5SDimitry Andric bool SBFileSpec::GetDescription(SBStream &description) const {
1750b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(bool, SBFileSpec, GetDescription, (lldb::SBStream &),
1760b57cec5SDimitry Andric description);
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric Stream &strm = description.ref();
1790b57cec5SDimitry Andric char path[PATH_MAX];
1800b57cec5SDimitry Andric if (m_opaque_up->GetPath(path, sizeof(path)))
1810b57cec5SDimitry Andric strm.PutCString(path);
1820b57cec5SDimitry Andric return true;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric
AppendPathComponent(const char * fn)1850b57cec5SDimitry Andric void SBFileSpec::AppendPathComponent(const char *fn) {
1860b57cec5SDimitry Andric LLDB_RECORD_METHOD(void, SBFileSpec, AppendPathComponent, (const char *), fn);
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric m_opaque_up->AppendPathComponent(fn);
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric namespace lldb_private {
1920b57cec5SDimitry Andric namespace repro {
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric template <>
RegisterMethods(Registry & R)1950b57cec5SDimitry Andric void RegisterMethods<SBFileSpec>(Registry &R) {
1960b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, ());
1970b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const lldb::SBFileSpec &));
1980b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *));
1990b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBFileSpec, (const char *, bool));
2000b57cec5SDimitry Andric LLDB_REGISTER_METHOD(const lldb::SBFileSpec &,
2010b57cec5SDimitry Andric SBFileSpec, operator=,(const lldb::SBFileSpec &));
2020b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool,
2030b57cec5SDimitry Andric SBFileSpec, operator==,(const lldb::SBFileSpec &));
2040b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool,
2050b57cec5SDimitry Andric SBFileSpec, operator!=,(const lldb::SBFileSpec &));
2060b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, IsValid, ());
2070b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, operator bool, ());
2080b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, Exists, ());
2090b57cec5SDimitry Andric LLDB_REGISTER_METHOD(bool, SBFileSpec, ResolveExecutableLocation, ());
2100b57cec5SDimitry Andric LLDB_REGISTER_STATIC_METHOD(int, SBFileSpec, ResolvePath,
2110b57cec5SDimitry Andric (const char *, char *, size_t));
2120b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(const char *, SBFileSpec, GetFilename, ());
2130b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(const char *, SBFileSpec, GetDirectory, ());
2140b57cec5SDimitry Andric LLDB_REGISTER_METHOD(void, SBFileSpec, SetFilename, (const char *));
2150b57cec5SDimitry Andric LLDB_REGISTER_METHOD(void, SBFileSpec, SetDirectory, (const char *));
2160b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBFileSpec, GetDescription,
2170b57cec5SDimitry Andric (lldb::SBStream &));
2180b57cec5SDimitry Andric LLDB_REGISTER_METHOD(void, SBFileSpec, AppendPathComponent, (const char *));
2195ffd83dbSDimitry Andric LLDB_REGISTER_CHAR_PTR_METHOD_CONST(uint32_t, SBFileSpec, GetPath);
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric }
224