blob: ef7d4c1d39f055f7af5efef8f1890d5bfefce818 [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"
10#include "lldb/API/SBLineEntry.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000011#include "lldb/API/SBStream.h"
Greg Claytonf02500c2013-06-18 22:51:05 +000012#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Symbol/CompileUnit.h"
14#include "lldb/Symbol/LineEntry.h"
15#include "lldb/Symbol/LineTable.h"
Greg Claytonf02500c2013-06-18 22:51:05 +000016#include "lldb/Symbol/SymbolVendor.h"
17#include "lldb/Symbol/Type.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023SBCompileUnit::SBCompileUnit() : m_opaque_ptr(NULL) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Kate Stoneb9c1b512016-09-06 20:57:50 +000025SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
26 : m_opaque_ptr(lldb_object_ptr) {}
27
28SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
29 : m_opaque_ptr(rhs.m_opaque_ptr) {}
30
31const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
32 m_opaque_ptr = rhs.m_opaque_ptr;
33 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = NULL; }
37
38SBFileSpec SBCompileUnit::GetFileSpec() const {
39 SBFileSpec file_spec;
40 if (m_opaque_ptr)
41 file_spec.SetFileSpec(*m_opaque_ptr);
42 return file_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045uint32_t SBCompileUnit::GetNumLineEntries() const {
46 if (m_opaque_ptr) {
47 LineTable *line_table = m_opaque_ptr->GetLineTable();
48 if (line_table)
49 return line_table->GetSize();
50 }
51 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
55 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonefabb122010-11-05 23:17:00 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 SBLineEntry sb_line_entry;
58 if (m_opaque_ptr) {
59 LineTable *line_table = m_opaque_ptr->GetLineTable();
60 if (line_table) {
61 LineEntry line_entry;
62 if (line_table->GetLineEntryAtIndex(idx, line_entry))
63 sb_line_entry.SetLineEntry(line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 }
66
67 if (log) {
68 SBStream sstr;
69 sb_line_entry.GetDescription(sstr);
70 log->Printf("SBCompileUnit(%p)::GetLineEntryAtIndex (idx=%u) => "
71 "SBLineEntry(%p): '%s'",
72 static_cast<void *>(m_opaque_ptr), idx,
73 static_cast<void *>(sb_line_entry.get()), sstr.GetData());
74 }
75
76 return sb_line_entry;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
80 SBFileSpec *inline_file_spec) const {
81 const bool exact = true;
82 return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083}
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
86 SBFileSpec *inline_file_spec,
87 bool exact) const {
88 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 uint32_t index = UINT32_MAX;
91 if (m_opaque_ptr) {
92 FileSpec file_spec;
93 if (inline_file_spec && inline_file_spec->IsValid())
94 file_spec = inline_file_spec->ref();
Caroline Ticedde9cff2010-09-20 05:20:02 +000095 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 file_spec = *m_opaque_ptr;
97
98 index = m_opaque_ptr->FindLineEntry(
99 start_idx, line, inline_file_spec ? inline_file_spec->get() : NULL,
100 exact, NULL);
101 }
102
103 if (log) {
104 SBStream sstr;
105 if (index == UINT32_MAX) {
106 log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
107 "line=%u, SBFileSpec(%p)) => NOT FOUND",
108 static_cast<void *>(m_opaque_ptr), start_idx, line,
109 inline_file_spec
110 ? static_cast<const void *>(inline_file_spec->get())
111 : NULL);
112 } else {
113 log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
114 "line=%u, SBFileSpec(%p)) => %u",
115 static_cast<void *>(m_opaque_ptr), start_idx, line,
116 inline_file_spec
117 ? static_cast<const void *>(inline_file_spec->get())
118 : NULL,
119 index);
120 }
121 }
122
123 return index;
124}
125
126uint32_t SBCompileUnit::GetNumSupportFiles() const {
127 if (m_opaque_ptr) {
128 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
129 return support_files.GetSize();
130 }
131 return 0;
132}
133
134lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
135 SBTypeList sb_type_list;
136
Zachary Turner117b1fa2018-10-25 20:45:40 +0000137 if (!m_opaque_ptr)
138 return sb_type_list;
139
140 ModuleSP module_sp(m_opaque_ptr->GetModule());
141 if (!module_sp)
142 return sb_type_list;
143
144 SymbolVendor *vendor = module_sp->GetSymbolVendor();
145 if (!vendor)
146 return sb_type_list;
147
148 TypeClass type_class = static_cast<TypeClass>(type_mask);
149 TypeList type_list;
150 vendor->GetTypes(m_opaque_ptr, type_class, type_list);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000151 sb_type_list.m_opaque_up->Append(type_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 return sb_type_list;
153}
154
155SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
156 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
157
158 SBFileSpec sb_file_spec;
159 if (m_opaque_ptr) {
160 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
161 FileSpec file_spec = support_files.GetFileSpecAtIndex(idx);
162 sb_file_spec.SetFileSpec(file_spec);
163 }
164
165 if (log) {
166 SBStream sstr;
167 sb_file_spec.GetDescription(sstr);
168 log->Printf("SBCompileUnit(%p)::GetGetFileSpecAtIndex (idx=%u) => "
169 "SBFileSpec(%p): '%s'",
170 static_cast<void *>(m_opaque_ptr), idx,
171 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
172 }
173
174 return sb_file_spec;
175}
176
177uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
178 const SBFileSpec &sb_file,
179 bool full) {
180 if (m_opaque_ptr) {
181 FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
182 return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
183 }
184 return 0;
185}
186
187lldb::LanguageType SBCompileUnit::GetLanguage() {
188 if (m_opaque_ptr)
189 return m_opaque_ptr->GetLanguage();
190 return lldb::eLanguageTypeUnknown;
191}
192
193bool SBCompileUnit::IsValid() const { return m_opaque_ptr != NULL; }
194
195bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
196 return m_opaque_ptr == rhs.m_opaque_ptr;
197}
198
199bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
200 return m_opaque_ptr != rhs.m_opaque_ptr;
201}
202
203const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
204 return m_opaque_ptr;
205}
206
207const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
208 return *m_opaque_ptr;
209}
210
211lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
212
213void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
214 m_opaque_ptr = lldb_object_ptr;
215}
216
217bool SBCompileUnit::GetDescription(SBStream &description) {
218 Stream &strm = description.ref();
219
220 if (m_opaque_ptr) {
221 m_opaque_ptr->Dump(&strm, false);
222 } else
223 strm.PutCString("No value");
224
225 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000226}