blob: b8afeff390457df3c6d3738501d9598c95f3335f [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
Jason Molendab459f182019-03-06 02:32:45 +000060 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 if (m_opaque_ptr) {
62 LineTable *line_table = m_opaque_ptr->GetLineTable();
Jason Molendab459f182019-03-06 02:32:45 +000063 if (line_table) {
64 if (log)
65 log->Printf("SBCompileUnit(%p)::GetNumLineEntries() => %d",
66 static_cast<void *>(m_opaque_ptr),
67 (int)line_table->GetSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 return line_table->GetSize();
Jason Molendab459f182019-03-06 02:32:45 +000069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 }
71 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000075 LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
76 GetLineEntryAtIndex, (uint32_t), idx);
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonefabb122010-11-05 23:17:00 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 SBLineEntry sb_line_entry;
81 if (m_opaque_ptr) {
82 LineTable *line_table = m_opaque_ptr->GetLineTable();
83 if (line_table) {
84 LineEntry line_entry;
85 if (line_table->GetLineEntryAtIndex(idx, line_entry))
86 sb_line_entry.SetLineEntry(line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 }
89
90 if (log) {
91 SBStream sstr;
92 sb_line_entry.GetDescription(sstr);
93 log->Printf("SBCompileUnit(%p)::GetLineEntryAtIndex (idx=%u) => "
94 "SBLineEntry(%p): '%s'",
95 static_cast<void *>(m_opaque_ptr), idx,
96 static_cast<void *>(sb_line_entry.get()), sstr.GetData());
97 }
98
Jonas Devliegherebaf56642019-03-06 00:06:00 +000099 return LLDB_RECORD_RESULT(sb_line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
103 SBFileSpec *inline_file_spec) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000104 LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
105 (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx,
106 line, inline_file_spec);
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 const bool exact = true;
109 return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
113 SBFileSpec *inline_file_spec,
114 bool exact) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000115 LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
116 (uint32_t, uint32_t, lldb::SBFileSpec *, bool),
117 start_idx, line, inline_file_spec, exact);
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 uint32_t index = UINT32_MAX;
122 if (m_opaque_ptr) {
123 FileSpec file_spec;
124 if (inline_file_spec && inline_file_spec->IsValid())
125 file_spec = inline_file_spec->ref();
Caroline Ticedde9cff2010-09-20 05:20:02 +0000126 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 file_spec = *m_opaque_ptr;
128
129 index = m_opaque_ptr->FindLineEntry(
130 start_idx, line, inline_file_spec ? inline_file_spec->get() : NULL,
131 exact, NULL);
132 }
133
134 if (log) {
135 SBStream sstr;
136 if (index == UINT32_MAX) {
137 log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
138 "line=%u, SBFileSpec(%p)) => NOT FOUND",
139 static_cast<void *>(m_opaque_ptr), start_idx, line,
140 inline_file_spec
141 ? static_cast<const void *>(inline_file_spec->get())
142 : NULL);
143 } else {
144 log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
145 "line=%u, SBFileSpec(%p)) => %u",
146 static_cast<void *>(m_opaque_ptr), start_idx, line,
147 inline_file_spec
148 ? static_cast<const void *>(inline_file_spec->get())
149 : NULL,
150 index);
151 }
152 }
153
154 return index;
155}
156
157uint32_t SBCompileUnit::GetNumSupportFiles() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000158 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles);
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 if (m_opaque_ptr) {
161 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
162 return support_files.GetSize();
163 }
164 return 0;
165}
166
167lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000168 LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t),
169 type_mask);
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 SBTypeList sb_type_list;
172
Zachary Turner117b1fa2018-10-25 20:45:40 +0000173 if (!m_opaque_ptr)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000174 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000175
176 ModuleSP module_sp(m_opaque_ptr->GetModule());
177 if (!module_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000178 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000179
180 SymbolVendor *vendor = module_sp->GetSymbolVendor();
181 if (!vendor)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000182 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000183
184 TypeClass type_class = static_cast<TypeClass>(type_mask);
185 TypeList type_list;
186 vendor->GetTypes(m_opaque_ptr, type_class, type_list);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000187 sb_type_list.m_opaque_up->Append(type_list);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000188 return LLDB_RECORD_RESULT(sb_type_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189}
190
191SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000192 LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
193 GetSupportFileAtIndex, (uint32_t), idx);
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
196
197 SBFileSpec sb_file_spec;
198 if (m_opaque_ptr) {
199 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
200 FileSpec file_spec = support_files.GetFileSpecAtIndex(idx);
201 sb_file_spec.SetFileSpec(file_spec);
202 }
203
204 if (log) {
205 SBStream sstr;
206 sb_file_spec.GetDescription(sstr);
207 log->Printf("SBCompileUnit(%p)::GetGetFileSpecAtIndex (idx=%u) => "
208 "SBFileSpec(%p): '%s'",
209 static_cast<void *>(m_opaque_ptr), idx,
210 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
211 }
212
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000213 return LLDB_RECORD_RESULT(sb_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214}
215
216uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
217 const SBFileSpec &sb_file,
218 bool full) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000219 LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
220 (uint32_t, const lldb::SBFileSpec &, bool), start_idx,
221 sb_file, full);
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 if (m_opaque_ptr) {
224 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
225 return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
226 }
227 return 0;
228}
229
230lldb::LanguageType SBCompileUnit::GetLanguage() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000231 LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage);
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 if (m_opaque_ptr)
234 return m_opaque_ptr->GetLanguage();
235 return lldb::eLanguageTypeUnknown;
236}
237
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000238bool SBCompileUnit::IsValid() const {
239 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid);
240
241 return m_opaque_ptr != NULL;
242}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243
244bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000245 LLDB_RECORD_METHOD_CONST(
246 bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs);
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 return m_opaque_ptr == rhs.m_opaque_ptr;
249}
250
251bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000252 LLDB_RECORD_METHOD_CONST(
253 bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs);
254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 return m_opaque_ptr != rhs.m_opaque_ptr;
256}
257
258const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
259 return m_opaque_ptr;
260}
261
262const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
263 return *m_opaque_ptr;
264}
265
266lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
267
268void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
269 m_opaque_ptr = lldb_object_ptr;
270}
271
272bool SBCompileUnit::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000273 LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &),
274 description);
275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 Stream &strm = description.ref();
277
278 if (m_opaque_ptr) {
279 m_opaque_ptr->Dump(&strm, false);
280 } else
281 strm.PutCString("No value");
282
283 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000284}