1 //===-- SBCompileUnit.cpp ---------------------------------------*- C++ -*-===//
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/SBCompileUnit.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBLineEntry.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/LineEntry.h"
16 #include "lldb/Symbol/LineTable.h"
17 #include "lldb/Symbol/SymbolVendor.h"
18 #include "lldb/Symbol/Type.h"
19 #include "lldb/Utility/Log.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 SBCompileUnit::SBCompileUnit() : m_opaque_ptr(NULL) {
25   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCompileUnit);
26 }
27 
28 SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
29     : m_opaque_ptr(lldb_object_ptr) {}
30 
31 SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
32     : m_opaque_ptr(rhs.m_opaque_ptr) {
33   LLDB_RECORD_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &), rhs);
34 }
35 
36 const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
37   LLDB_RECORD_METHOD(const lldb::SBCompileUnit &,
38                      SBCompileUnit, operator=,(const lldb::SBCompileUnit &),
39                      rhs);
40 
41   m_opaque_ptr = rhs.m_opaque_ptr;
42   return *this;
43 }
44 
45 SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = NULL; }
46 
47 SBFileSpec SBCompileUnit::GetFileSpec() const {
48   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBCompileUnit,
49                                    GetFileSpec);
50 
51   SBFileSpec file_spec;
52   if (m_opaque_ptr)
53     file_spec.SetFileSpec(*m_opaque_ptr);
54   return LLDB_RECORD_RESULT(file_spec);
55 }
56 
57 uint32_t SBCompileUnit::GetNumLineEntries() const {
58   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumLineEntries);
59 
60   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
61   if (m_opaque_ptr) {
62     LineTable *line_table = m_opaque_ptr->GetLineTable();
63     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());
68       return line_table->GetSize();
69     }
70   }
71   return 0;
72 }
73 
74 SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
75   LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
76                            GetLineEntryAtIndex, (uint32_t), idx);
77 
78   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
79 
80   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);
87     }
88   }
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 
99   return LLDB_RECORD_RESULT(sb_line_entry);
100 }
101 
102 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
103                                            SBFileSpec *inline_file_spec) const {
104   LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
105                            (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx,
106                            line, inline_file_spec);
107 
108   const bool exact = true;
109   return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
110 }
111 
112 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
113                                            SBFileSpec *inline_file_spec,
114                                            bool exact) const {
115   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 
119   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
120 
121   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();
126     else
127       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 
157 uint32_t SBCompileUnit::GetNumSupportFiles() const {
158   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles);
159 
160   if (m_opaque_ptr) {
161     FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
162     return support_files.GetSize();
163   }
164   return 0;
165 }
166 
167 lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
168   LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t),
169                      type_mask);
170 
171   SBTypeList sb_type_list;
172 
173   if (!m_opaque_ptr)
174     return LLDB_RECORD_RESULT(sb_type_list);
175 
176   ModuleSP module_sp(m_opaque_ptr->GetModule());
177   if (!module_sp)
178     return LLDB_RECORD_RESULT(sb_type_list);
179 
180   SymbolVendor *vendor = module_sp->GetSymbolVendor();
181   if (!vendor)
182     return LLDB_RECORD_RESULT(sb_type_list);
183 
184   TypeClass type_class = static_cast<TypeClass>(type_mask);
185   TypeList type_list;
186   vendor->GetTypes(m_opaque_ptr, type_class, type_list);
187   sb_type_list.m_opaque_up->Append(type_list);
188   return LLDB_RECORD_RESULT(sb_type_list);
189 }
190 
191 SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
192   LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
193                            GetSupportFileAtIndex, (uint32_t), idx);
194 
195   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 
213   return LLDB_RECORD_RESULT(sb_file_spec);
214 }
215 
216 uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
217                                              const SBFileSpec &sb_file,
218                                              bool full) {
219   LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
220                      (uint32_t, const lldb::SBFileSpec &, bool), start_idx,
221                      sb_file, full);
222 
223   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 
230 lldb::LanguageType SBCompileUnit::GetLanguage() {
231   LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage);
232 
233   if (m_opaque_ptr)
234     return m_opaque_ptr->GetLanguage();
235   return lldb::eLanguageTypeUnknown;
236 }
237 
238 bool SBCompileUnit::IsValid() const {
239   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid);
240 
241   return m_opaque_ptr != NULL;
242 }
243 
244 bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
245   LLDB_RECORD_METHOD_CONST(
246       bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs);
247 
248   return m_opaque_ptr == rhs.m_opaque_ptr;
249 }
250 
251 bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
252   LLDB_RECORD_METHOD_CONST(
253       bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs);
254 
255   return m_opaque_ptr != rhs.m_opaque_ptr;
256 }
257 
258 const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
259   return m_opaque_ptr;
260 }
261 
262 const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
263   return *m_opaque_ptr;
264 }
265 
266 lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
267 
268 void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
269   m_opaque_ptr = lldb_object_ptr;
270 }
271 
272 bool SBCompileUnit::GetDescription(SBStream &description) {
273   LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &),
274                      description);
275 
276   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;
284 }
285