blob: d3e828c2ecc6ee2694366442479b4aa2c76a5a90 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBCompileUnit.cpp ---------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBCompileUnit.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/API/SBLineEntry.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000012#include "lldb/API/SBStream.h"
Greg Claytonf02500c2013-06-18 22:51:05 +000013#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Symbol/CompileUnit.h"
15#include "lldb/Symbol/LineEntry.h"
16#include "lldb/Symbol/LineTable.h"
Greg Claytonf02500c2013-06-18 22:51:05 +000017#include "lldb/Symbol/SymbolVendor.h"
18#include "lldb/Symbol/Type.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000019#include "lldb/Utility/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
Jonas Devliegherebaf56642019-03-06 00:06:00 +000024SBCompileUnit::SBCompileUnit() : m_opaque_ptr(NULL) {
25 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCompileUnit);
26}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
29 : m_opaque_ptr(lldb_object_ptr) {}
30
31SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
Jonas Devliegherebaf56642019-03-06 00:06:00 +000032 : m_opaque_ptr(rhs.m_opaque_ptr) {
33 LLDB_RECORD_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &), rhs);
34}
Kate Stoneb9c1b512016-09-06 20:57:50 +000035
36const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000037 LLDB_RECORD_METHOD(const lldb::SBCompileUnit &,
38 SBCompileUnit, operator=,(const lldb::SBCompileUnit &),
39 rhs);
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 m_opaque_ptr = rhs.m_opaque_ptr;
42 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = NULL; }
46
47SBFileSpec SBCompileUnit::GetFileSpec() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000048 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBCompileUnit,
49 GetFileSpec);
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 SBFileSpec file_spec;
52 if (m_opaque_ptr)
53 file_spec.SetFileSpec(*m_opaque_ptr);
Jonas Devliegherebaf56642019-03-06 00:06:00 +000054 return LLDB_RECORD_RESULT(file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057uint32_t SBCompileUnit::GetNumLineEntries() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000058 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumLineEntries);
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 if (m_opaque_ptr) {
61 LineTable *line_table = m_opaque_ptr->GetLineTable();
Jason Molendaf228b2c2019-03-05 22:17:47 +000062 if (line_table)
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 return line_table->GetSize();
64 }
65 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000069 LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
70 GetLineEntryAtIndex, (uint32_t), idx);
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonefabb122010-11-05 23:17:00 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 SBLineEntry sb_line_entry;
75 if (m_opaque_ptr) {
76 LineTable *line_table = m_opaque_ptr->GetLineTable();
77 if (line_table) {
78 LineEntry line_entry;
79 if (line_table->GetLineEntryAtIndex(idx, line_entry))
80 sb_line_entry.SetLineEntry(line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
83
84 if (log) {
85 SBStream sstr;
86 sb_line_entry.GetDescription(sstr);
87 log->Printf("SBCompileUnit(%p)::GetLineEntryAtIndex (idx=%u) => "
88 "SBLineEntry(%p): '%s'",
89 static_cast<void *>(m_opaque_ptr), idx,
90 static_cast<void *>(sb_line_entry.get()), sstr.GetData());
91 }
92
Jonas Devliegherebaf56642019-03-06 00:06:00 +000093 return LLDB_RECORD_RESULT(sb_line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
97 SBFileSpec *inline_file_spec) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000098 LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
99 (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx,
100 line, inline_file_spec);
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 const bool exact = true;
103 return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104}
105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
107 SBFileSpec *inline_file_spec,
108 bool exact) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000109 LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
110 (uint32_t, uint32_t, lldb::SBFileSpec *, bool),
111 start_idx, line, inline_file_spec, exact);
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 uint32_t index = UINT32_MAX;
116 if (m_opaque_ptr) {
117 FileSpec file_spec;
118 if (inline_file_spec && inline_file_spec->IsValid())
119 file_spec = inline_file_spec->ref();
Caroline Ticedde9cff2010-09-20 05:20:02 +0000120 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 file_spec = *m_opaque_ptr;
122
123 index = m_opaque_ptr->FindLineEntry(
124 start_idx, line, inline_file_spec ? inline_file_spec->get() : NULL,
125 exact, NULL);
126 }
127
128 if (log) {
129 SBStream sstr;
130 if (index == UINT32_MAX) {
131 log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
132 "line=%u, SBFileSpec(%p)) => NOT FOUND",
133 static_cast<void *>(m_opaque_ptr), start_idx, line,
134 inline_file_spec
135 ? static_cast<const void *>(inline_file_spec->get())
136 : NULL);
137 } else {
138 log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
139 "line=%u, SBFileSpec(%p)) => %u",
140 static_cast<void *>(m_opaque_ptr), start_idx, line,
141 inline_file_spec
142 ? static_cast<const void *>(inline_file_spec->get())
143 : NULL,
144 index);
145 }
146 }
147
148 return index;
149}
150
151uint32_t SBCompileUnit::GetNumSupportFiles() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000152 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles);
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 if (m_opaque_ptr) {
155 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
156 return support_files.GetSize();
157 }
158 return 0;
159}
160
161lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000162 LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t),
163 type_mask);
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 SBTypeList sb_type_list;
166
Zachary Turner117b1fa2018-10-25 20:45:40 +0000167 if (!m_opaque_ptr)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000168 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000169
170 ModuleSP module_sp(m_opaque_ptr->GetModule());
171 if (!module_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000172 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000173
174 SymbolVendor *vendor = module_sp->GetSymbolVendor();
175 if (!vendor)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000176 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000177
178 TypeClass type_class = static_cast<TypeClass>(type_mask);
179 TypeList type_list;
180 vendor->GetTypes(m_opaque_ptr, type_class, type_list);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000181 sb_type_list.m_opaque_up->Append(type_list);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000182 return LLDB_RECORD_RESULT(sb_type_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183}
184
185SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000186 LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
187 GetSupportFileAtIndex, (uint32_t), idx);
188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
190
191 SBFileSpec sb_file_spec;
192 if (m_opaque_ptr) {
193 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
194 FileSpec file_spec = support_files.GetFileSpecAtIndex(idx);
195 sb_file_spec.SetFileSpec(file_spec);
196 }
197
198 if (log) {
199 SBStream sstr;
200 sb_file_spec.GetDescription(sstr);
201 log->Printf("SBCompileUnit(%p)::GetGetFileSpecAtIndex (idx=%u) => "
202 "SBFileSpec(%p): '%s'",
203 static_cast<void *>(m_opaque_ptr), idx,
204 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
205 }
206
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000207 return LLDB_RECORD_RESULT(sb_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208}
209
210uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
211 const SBFileSpec &sb_file,
212 bool full) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000213 LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
214 (uint32_t, const lldb::SBFileSpec &, bool), start_idx,
215 sb_file, full);
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 if (m_opaque_ptr) {
218 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
219 return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
220 }
221 return 0;
222}
223
224lldb::LanguageType SBCompileUnit::GetLanguage() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000225 LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage);
226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 if (m_opaque_ptr)
228 return m_opaque_ptr->GetLanguage();
229 return lldb::eLanguageTypeUnknown;
230}
231
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000232bool SBCompileUnit::IsValid() const {
233 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid);
234
235 return m_opaque_ptr != NULL;
236}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237
238bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000239 LLDB_RECORD_METHOD_CONST(
240 bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs);
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 return m_opaque_ptr == rhs.m_opaque_ptr;
243}
244
245bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000246 LLDB_RECORD_METHOD_CONST(
247 bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs);
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 return m_opaque_ptr != rhs.m_opaque_ptr;
250}
251
252const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
253 return m_opaque_ptr;
254}
255
256const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
257 return *m_opaque_ptr;
258}
259
260lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
261
262void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
263 m_opaque_ptr = lldb_object_ptr;
264}
265
266bool SBCompileUnit::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000267 LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &),
268 description);
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 Stream &strm = description.ref();
271
272 if (m_opaque_ptr) {
273 m_opaque_ptr->Dump(&strm, false);
274 } else
275 strm.PutCString("No value");
276
277 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000278}