1 //===-- SBTypeFilter.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 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBTypeFilter.h"
10 #include "lldb/Utility/Instrumentation.h"
11 
12 #include "lldb/API/SBStream.h"
13 
14 #include "lldb/DataFormatters/DataVisualization.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 SBTypeFilter::SBTypeFilter() { LLDB_INSTRUMENT_VA(this); }
20 
21 SBTypeFilter::SBTypeFilter(uint32_t options)
22     : m_opaque_sp(TypeFilterImplSP(new TypeFilterImpl(options))) {
23   LLDB_INSTRUMENT_VA(this, options);
24 }
25 
26 SBTypeFilter::SBTypeFilter(const lldb::SBTypeFilter &rhs)
27     : m_opaque_sp(rhs.m_opaque_sp) {
28   LLDB_INSTRUMENT_VA(this, rhs);
29 }
30 
31 SBTypeFilter::~SBTypeFilter() = default;
32 
33 bool SBTypeFilter::IsValid() const {
34   LLDB_INSTRUMENT_VA(this);
35   return this->operator bool();
36 }
37 SBTypeFilter::operator bool() const {
38   LLDB_INSTRUMENT_VA(this);
39 
40   return m_opaque_sp.get() != nullptr;
41 }
42 
43 uint32_t SBTypeFilter::GetOptions() {
44   LLDB_INSTRUMENT_VA(this);
45 
46   if (IsValid())
47     return m_opaque_sp->GetOptions();
48   return 0;
49 }
50 
51 void SBTypeFilter::SetOptions(uint32_t value) {
52   LLDB_INSTRUMENT_VA(this, value);
53 
54   if (CopyOnWrite_Impl())
55     m_opaque_sp->SetOptions(value);
56 }
57 
58 bool SBTypeFilter::GetDescription(lldb::SBStream &description,
59                                   lldb::DescriptionLevel description_level) {
60   LLDB_INSTRUMENT_VA(this, description, description_level);
61 
62   if (!IsValid())
63     return false;
64   else {
65     description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
66     return true;
67   }
68 }
69 
70 void SBTypeFilter::Clear() {
71   LLDB_INSTRUMENT_VA(this);
72 
73   if (CopyOnWrite_Impl())
74     m_opaque_sp->Clear();
75 }
76 
77 uint32_t SBTypeFilter::GetNumberOfExpressionPaths() {
78   LLDB_INSTRUMENT_VA(this);
79 
80   if (IsValid())
81     return m_opaque_sp->GetCount();
82   return 0;
83 }
84 
85 const char *SBTypeFilter::GetExpressionPathAtIndex(uint32_t i) {
86   LLDB_INSTRUMENT_VA(this, i);
87 
88   if (!IsValid())
89     return nullptr;
90 
91   const char *item = m_opaque_sp->GetExpressionPathAtIndex(i);
92   if (item && *item == '.')
93     item++;
94   return ConstString(item).GetCString();
95 }
96 
97 bool SBTypeFilter::ReplaceExpressionPathAtIndex(uint32_t i, const char *item) {
98   LLDB_INSTRUMENT_VA(this, i, item);
99 
100   if (CopyOnWrite_Impl())
101     return m_opaque_sp->SetExpressionPathAtIndex(i, item);
102   else
103     return false;
104 }
105 
106 void SBTypeFilter::AppendExpressionPath(const char *item) {
107   LLDB_INSTRUMENT_VA(this, item);
108 
109   if (CopyOnWrite_Impl())
110     m_opaque_sp->AddExpressionPath(item);
111 }
112 
113 lldb::SBTypeFilter &SBTypeFilter::operator=(const lldb::SBTypeFilter &rhs) {
114   LLDB_INSTRUMENT_VA(this, rhs);
115 
116   if (this != &rhs) {
117     m_opaque_sp = rhs.m_opaque_sp;
118   }
119   return *this;
120 }
121 
122 bool SBTypeFilter::operator==(lldb::SBTypeFilter &rhs) {
123   LLDB_INSTRUMENT_VA(this, rhs);
124 
125   if (!IsValid())
126     return !rhs.IsValid();
127 
128   return m_opaque_sp == rhs.m_opaque_sp;
129 }
130 
131 bool SBTypeFilter::IsEqualTo(lldb::SBTypeFilter &rhs) {
132   LLDB_INSTRUMENT_VA(this, rhs);
133 
134   if (!IsValid())
135     return !rhs.IsValid();
136 
137   if (GetNumberOfExpressionPaths() != rhs.GetNumberOfExpressionPaths())
138     return false;
139 
140   for (uint32_t j = 0; j < GetNumberOfExpressionPaths(); j++)
141     if (strcmp(GetExpressionPathAtIndex(j), rhs.GetExpressionPathAtIndex(j)) !=
142         0)
143       return false;
144 
145   return GetOptions() == rhs.GetOptions();
146 }
147 
148 bool SBTypeFilter::operator!=(lldb::SBTypeFilter &rhs) {
149   LLDB_INSTRUMENT_VA(this, rhs);
150 
151   if (!IsValid())
152     return !rhs.IsValid();
153 
154   return m_opaque_sp != rhs.m_opaque_sp;
155 }
156 
157 lldb::TypeFilterImplSP SBTypeFilter::GetSP() { return m_opaque_sp; }
158 
159 void SBTypeFilter::SetSP(const lldb::TypeFilterImplSP &typefilter_impl_sp) {
160   m_opaque_sp = typefilter_impl_sp;
161 }
162 
163 SBTypeFilter::SBTypeFilter(const lldb::TypeFilterImplSP &typefilter_impl_sp)
164     : m_opaque_sp(typefilter_impl_sp) {}
165 
166 bool SBTypeFilter::CopyOnWrite_Impl() {
167   if (!IsValid())
168     return false;
169   if (m_opaque_sp.unique())
170     return true;
171 
172   TypeFilterImplSP new_sp(new TypeFilterImpl(GetOptions()));
173 
174   for (uint32_t j = 0; j < GetNumberOfExpressionPaths(); j++)
175     new_sp->AddExpressionPath(GetExpressionPathAtIndex(j));
176 
177   SetSP(new_sp);
178 
179   return true;
180 }
181