[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBFile.cpp
blob2ae4b1481afbf2f52faccd765be63c1eecc9d24f
1 //===-- SBFile.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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBFile.h"
10 #include "lldb/API/SBError.h"
11 #include "lldb/Host/File.h"
12 #include "lldb/Utility/Instrumentation.h"
14 using namespace lldb;
15 using namespace lldb_private;
17 SBFile::~SBFile() = default;
19 SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {
20 // We have no way to capture the incoming FileSP as the class isn't
21 // instrumented, so pretend that it's always null.
22 LLDB_INSTRUMENT_VA(this, file_sp);
25 SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
26 LLDB_INSTRUMENT_VA(this, rhs);
29 SBFile &SBFile ::operator=(const SBFile &rhs) {
30 LLDB_INSTRUMENT_VA(this, rhs);
32 if (this != &rhs)
33 m_opaque_sp = rhs.m_opaque_sp;
34 return *this;
37 SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }
39 SBFile::SBFile(FILE *file, bool transfer_ownership) {
40 LLDB_INSTRUMENT_VA(this, file, transfer_ownership);
42 m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
45 SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
46 LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);
48 auto options = File::GetOptionsFromMode(mode);
49 if (!options) {
50 llvm::consumeError(options.takeError());
51 return;
53 m_opaque_sp =
54 std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
57 SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
58 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
60 SBError error;
61 if (!m_opaque_sp) {
62 error = Status::FromErrorString("invalid SBFile");
63 *bytes_read = 0;
64 } else {
65 error.SetError(m_opaque_sp->Read(buf, num_bytes));
66 *bytes_read = num_bytes;
68 return error;
71 SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
72 size_t *bytes_written) {
73 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
75 SBError error;
76 if (!m_opaque_sp) {
77 error = Status::FromErrorString("invalid SBFile");
78 *bytes_written = 0;
79 } else {
80 error.SetError(m_opaque_sp->Write(buf, num_bytes));
81 *bytes_written = num_bytes;
83 return error;
86 SBError SBFile::Flush() {
87 LLDB_INSTRUMENT_VA(this);
89 SBError error;
90 if (!m_opaque_sp) {
91 error = Status::FromErrorString("invalid SBFile");
92 } else {
93 error.SetError(m_opaque_sp->Flush());
95 return error;
98 bool SBFile::IsValid() const {
99 LLDB_INSTRUMENT_VA(this);
100 return m_opaque_sp && m_opaque_sp->IsValid();
103 SBError SBFile::Close() {
104 LLDB_INSTRUMENT_VA(this);
105 SBError error;
106 if (m_opaque_sp)
107 error.SetError(m_opaque_sp->Close());
108 return error;
111 SBFile::operator bool() const {
112 LLDB_INSTRUMENT_VA(this);
113 return IsValid();
116 bool SBFile::operator!() const {
117 LLDB_INSTRUMENT_VA(this);
118 return !IsValid();
121 FileSP SBFile::GetFile() const {
122 LLDB_INSTRUMENT_VA(this);
123 return m_opaque_sp;