Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / API / SBTypeFormat.cpp
blob86e11e8b8fde6c95adcb07235fcc8d7dfdf8462c
1 //===-- SBTypeFormat.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/SBTypeFormat.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBStream.h"
14 #include "lldb/DataFormatters/DataVisualization.h"
16 using namespace lldb;
17 using namespace lldb_private;
19 SBTypeFormat::SBTypeFormat() { LLDB_INSTRUMENT_VA(this); }
21 SBTypeFormat::SBTypeFormat(lldb::Format format, uint32_t options)
22 : m_opaque_sp(
23 TypeFormatImplSP(new TypeFormatImpl_Format(format, options))) {
24 LLDB_INSTRUMENT_VA(this, format, options);
27 SBTypeFormat::SBTypeFormat(const char *type, uint32_t options)
28 : m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_EnumType(
29 ConstString(type ? type : ""), options))) {
30 LLDB_INSTRUMENT_VA(this, type, options);
33 SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat &rhs)
34 : m_opaque_sp(rhs.m_opaque_sp) {
35 LLDB_INSTRUMENT_VA(this, rhs);
38 SBTypeFormat::~SBTypeFormat() = default;
40 bool SBTypeFormat::IsValid() const {
41 LLDB_INSTRUMENT_VA(this);
42 return this->operator bool();
44 SBTypeFormat::operator bool() const {
45 LLDB_INSTRUMENT_VA(this);
47 return m_opaque_sp.get() != nullptr;
50 lldb::Format SBTypeFormat::GetFormat() {
51 LLDB_INSTRUMENT_VA(this);
53 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
54 return ((TypeFormatImpl_Format *)m_opaque_sp.get())->GetFormat();
55 return lldb::eFormatInvalid;
58 const char *SBTypeFormat::GetTypeName() {
59 LLDB_INSTRUMENT_VA(this);
61 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
62 return ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
63 ->GetTypeName()
64 .AsCString("");
65 return "";
68 uint32_t SBTypeFormat::GetOptions() {
69 LLDB_INSTRUMENT_VA(this);
71 if (IsValid())
72 return m_opaque_sp->GetOptions();
73 return 0;
76 void SBTypeFormat::SetFormat(lldb::Format fmt) {
77 LLDB_INSTRUMENT_VA(this, fmt);
79 if (CopyOnWrite_Impl(Type::eTypeFormat))
80 ((TypeFormatImpl_Format *)m_opaque_sp.get())->SetFormat(fmt);
83 void SBTypeFormat::SetTypeName(const char *type) {
84 LLDB_INSTRUMENT_VA(this, type);
86 if (CopyOnWrite_Impl(Type::eTypeEnum))
87 ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
88 ->SetTypeName(ConstString(type ? type : ""));
91 void SBTypeFormat::SetOptions(uint32_t value) {
92 LLDB_INSTRUMENT_VA(this, value);
94 if (CopyOnWrite_Impl(Type::eTypeKeepSame))
95 m_opaque_sp->SetOptions(value);
98 bool SBTypeFormat::GetDescription(lldb::SBStream &description,
99 lldb::DescriptionLevel description_level) {
100 LLDB_INSTRUMENT_VA(this, description, description_level);
102 if (!IsValid())
103 return false;
104 else {
105 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
106 return true;
110 lldb::SBTypeFormat &SBTypeFormat::operator=(const lldb::SBTypeFormat &rhs) {
111 LLDB_INSTRUMENT_VA(this, rhs);
113 if (this != &rhs) {
114 m_opaque_sp = rhs.m_opaque_sp;
116 return *this;
119 bool SBTypeFormat::operator==(lldb::SBTypeFormat &rhs) {
120 LLDB_INSTRUMENT_VA(this, rhs);
122 if (!IsValid())
123 return !rhs.IsValid();
124 return m_opaque_sp == rhs.m_opaque_sp;
127 bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat &rhs) {
128 LLDB_INSTRUMENT_VA(this, rhs);
130 if (!IsValid())
131 return !rhs.IsValid();
133 if (GetFormat() == rhs.GetFormat())
134 return GetOptions() == rhs.GetOptions();
135 else
136 return false;
139 bool SBTypeFormat::operator!=(lldb::SBTypeFormat &rhs) {
140 LLDB_INSTRUMENT_VA(this, rhs);
142 if (!IsValid())
143 return !rhs.IsValid();
144 return m_opaque_sp != rhs.m_opaque_sp;
147 lldb::TypeFormatImplSP SBTypeFormat::GetSP() { return m_opaque_sp; }
149 void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP &typeformat_impl_sp) {
150 m_opaque_sp = typeformat_impl_sp;
153 SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP &typeformat_impl_sp)
154 : m_opaque_sp(typeformat_impl_sp) {}
156 bool SBTypeFormat::CopyOnWrite_Impl(Type type) {
157 if (!IsValid())
158 return false;
160 if (m_opaque_sp.unique() &&
161 ((type == Type::eTypeKeepSame) ||
162 (type == Type::eTypeFormat &&
163 m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat) ||
164 (type == Type::eTypeEnum &&
165 m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)))
166 return true;
168 if (type == Type::eTypeKeepSame) {
169 if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
170 type = Type::eTypeFormat;
171 else
172 type = Type::eTypeEnum;
175 if (type == Type::eTypeFormat)
176 SetSP(
177 TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
178 else
179 SetSP(TypeFormatImplSP(
180 new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));
182 return true;