Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / API / SBEvent.cpp
blobf12df2939420d63b9d64cdb02db232ed8439e827
1 //===-- SBEvent.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/SBEvent.h"
10 #include "lldb/API/SBBroadcaster.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
14 #include "lldb/Breakpoint/Breakpoint.h"
15 #include "lldb/Interpreter/CommandInterpreter.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/Event.h"
19 #include "lldb/Utility/Stream.h"
21 using namespace lldb;
22 using namespace lldb_private;
24 SBEvent::SBEvent() { LLDB_INSTRUMENT_VA(this); }
26 SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)
27 : m_event_sp(new Event(event_type, new EventDataBytes(cstr, cstr_len))),
28 m_opaque_ptr(m_event_sp.get()) {
29 LLDB_INSTRUMENT_VA(this, event_type, cstr, cstr_len);
32 SBEvent::SBEvent(EventSP &event_sp)
33 : m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {
34 LLDB_INSTRUMENT_VA(this, event_sp);
37 SBEvent::SBEvent(Event *event_ptr) : m_opaque_ptr(event_ptr) {
38 LLDB_INSTRUMENT_VA(this, event_ptr);
41 SBEvent::SBEvent(const SBEvent &rhs)
42 : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {
43 LLDB_INSTRUMENT_VA(this, rhs);
46 const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
47 LLDB_INSTRUMENT_VA(this, rhs);
49 if (this != &rhs) {
50 m_event_sp = rhs.m_event_sp;
51 m_opaque_ptr = rhs.m_opaque_ptr;
53 return *this;
56 SBEvent::~SBEvent() = default;
58 const char *SBEvent::GetDataFlavor() {
59 LLDB_INSTRUMENT_VA(this);
61 Event *lldb_event = get();
62 if (lldb_event) {
63 EventData *event_data = lldb_event->GetData();
64 if (event_data)
65 return ConstString(lldb_event->GetData()->GetFlavor()).GetCString();
67 return nullptr;
70 uint32_t SBEvent::GetType() const {
71 LLDB_INSTRUMENT_VA(this);
73 const Event *lldb_event = get();
74 uint32_t event_type = 0;
75 if (lldb_event)
76 event_type = lldb_event->GetType();
79 return event_type;
82 SBBroadcaster SBEvent::GetBroadcaster() const {
83 LLDB_INSTRUMENT_VA(this);
85 SBBroadcaster broadcaster;
86 const Event *lldb_event = get();
87 if (lldb_event)
88 broadcaster.reset(lldb_event->GetBroadcaster(), false);
89 return broadcaster;
92 const char *SBEvent::GetBroadcasterClass() const {
93 LLDB_INSTRUMENT_VA(this);
95 const Event *lldb_event = get();
96 if (lldb_event)
97 return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString();
98 else
99 return "unknown class";
102 bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) {
103 LLDB_INSTRUMENT_VA(this, broadcaster);
105 if (broadcaster)
106 return BroadcasterMatchesRef(*broadcaster);
107 return false;
110 bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) {
111 LLDB_INSTRUMENT_VA(this, broadcaster);
113 Event *lldb_event = get();
114 bool success = false;
115 if (lldb_event)
116 success = lldb_event->BroadcasterIs(broadcaster.get());
119 return success;
122 void SBEvent::Clear() {
123 LLDB_INSTRUMENT_VA(this);
125 Event *lldb_event = get();
126 if (lldb_event)
127 lldb_event->Clear();
130 EventSP &SBEvent::GetSP() const { return m_event_sp; }
132 Event *SBEvent::get() const {
133 // There is a dangerous accessor call GetSharedPtr which can be used, so if
134 // we have anything valid in m_event_sp, we must use that since if it gets
135 // used by a function that puts something in there, then it won't update
136 // m_opaque_ptr...
137 if (m_event_sp)
138 m_opaque_ptr = m_event_sp.get();
140 return m_opaque_ptr;
143 void SBEvent::reset(EventSP &event_sp) {
144 m_event_sp = event_sp;
145 m_opaque_ptr = m_event_sp.get();
148 void SBEvent::reset(Event *event_ptr) {
149 m_opaque_ptr = event_ptr;
150 m_event_sp.reset();
153 bool SBEvent::IsValid() const {
154 LLDB_INSTRUMENT_VA(this);
155 return this->operator bool();
157 SBEvent::operator bool() const {
158 LLDB_INSTRUMENT_VA(this);
160 // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
161 // See comments in SBEvent::get()....
162 return SBEvent::get() != nullptr;
165 const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
166 LLDB_INSTRUMENT_VA(event);
168 return ConstString(static_cast<const char *>(
169 EventDataBytes::GetBytesFromEvent(event.get())))
170 .GetCString();
173 bool SBEvent::GetDescription(SBStream &description) {
174 LLDB_INSTRUMENT_VA(this, description);
176 Stream &strm = description.ref();
178 if (get()) {
179 m_opaque_ptr->Dump(&strm);
180 } else
181 strm.PutCString("No value");
183 return true;
186 bool SBEvent::GetDescription(SBStream &description) const {
187 LLDB_INSTRUMENT_VA(this, description);
189 Stream &strm = description.ref();
191 if (get()) {
192 m_opaque_ptr->Dump(&strm);
193 } else
194 strm.PutCString("No value");
196 return true;