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   if (m_opaque_ptr) {
61     LineTable *line_table = m_opaque_ptr->GetLineTable();
62     if (line_table)
63       return line_table->GetSize();
64   }
65   return 0;
66 }
67 
68 SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
69   LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
70                            GetLineEntryAtIndex, (uint32_t), idx);
71 
72   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
73 
74   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);
81     }
82   }
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 
93   return LLDB_RECORD_RESULT(sb_line_entry);
94 }
95 
96 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
97                                            SBFileSpec *inline_file_spec) const {
98   LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
99                            (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx,
100                            line, inline_file_spec);
101 
102   const bool exact = true;
103   return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
104 }
105 
106 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
107                                            SBFileSpec *inline_file_spec,
108                                            bool exact) const {
109   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 
113   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
114 
115   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();
120     else
121       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 
151 uint32_t SBCompileUnit::GetNumSupportFiles() const {
152   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles);
153 
154   if (m_opaque_ptr) {
155     FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
156     return support_files.GetSize();
157   }
158   return 0;
159 }
160 
161 lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
162   LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t),
163                      type_mask);
164 
165   SBTypeList sb_type_list;
166 
167   if (!m_opaque_ptr)
168     return LLDB_RECORD_RESULT(sb_type_list);
169 
170   ModuleSP module_sp(m_opaque_ptr->GetModule());
171   if (!module_sp)
172     return LLDB_RECORD_RESULT(sb_type_list);
173 
174   SymbolVendor *vendor = module_sp->GetSymbolVendor();
175   if (!vendor)
176     return LLDB_RECORD_RESULT(sb_type_list);
177 
178   TypeClass type_class = static_cast<TypeClass>(type_mask);
179   TypeList type_list;
180   vendor->GetTypes(m_opaque_ptr, type_class, type_list);
181   sb_type_list.m_opaque_up->Append(type_list);
182   return LLDB_RECORD_RESULT(sb_type_list);
183 }
184 
185 SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
186   LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
187                            GetSupportFileAtIndex, (uint32_t), idx);
188 
189   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 
207   return LLDB_RECORD_RESULT(sb_file_spec);
208 }
209 
210 uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
211                                              const SBFileSpec &sb_file,
212                                              bool full) {
213   LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
214                      (uint32_t, const lldb::SBFileSpec &, bool), start_idx,
215                      sb_file, full);
216 
217   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 
224 lldb::LanguageType SBCompileUnit::GetLanguage() {
225   LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage);
226 
227   if (m_opaque_ptr)
228     return m_opaque_ptr->GetLanguage();
229   return lldb::eLanguageTypeUnknown;
230 }
231 
232 bool SBCompileUnit::IsValid() const {
233   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid);
234 
235   return m_opaque_ptr != NULL;
236 }
237 
238 bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
239   LLDB_RECORD_METHOD_CONST(
240       bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs);
241 
242   return m_opaque_ptr == rhs.m_opaque_ptr;
243 }
244 
245 bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
246   LLDB_RECORD_METHOD_CONST(
247       bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs);
248 
249   return m_opaque_ptr != rhs.m_opaque_ptr;
250 }
251 
252 const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
253   return m_opaque_ptr;
254 }
255 
256 const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
257   return *m_opaque_ptr;
258 }
259 
260 lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
261 
262 void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
263   m_opaque_ptr = lldb_object_ptr;
264 }
265 
266 bool SBCompileUnit::GetDescription(SBStream &description) {
267   LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &),
268                      description);
269 
270   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;
278 }
279