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 "lldb/API/SBLineEntry.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Symbol/CompileUnit.h"
14 #include "lldb/Symbol/LineEntry.h"
15 #include "lldb/Symbol/LineTable.h"
16 #include "lldb/Symbol/SymbolVendor.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Utility/Log.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 SBCompileUnit::SBCompileUnit() : m_opaque_ptr(NULL) {}
24 
25 SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
26     : m_opaque_ptr(lldb_object_ptr) {}
27 
28 SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
29     : m_opaque_ptr(rhs.m_opaque_ptr) {}
30 
31 const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
32   m_opaque_ptr = rhs.m_opaque_ptr;
33   return *this;
34 }
35 
36 SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = NULL; }
37 
38 SBFileSpec SBCompileUnit::GetFileSpec() const {
39   SBFileSpec file_spec;
40   if (m_opaque_ptr)
41     file_spec.SetFileSpec(*m_opaque_ptr);
42   return file_spec;
43 }
44 
45 uint32_t SBCompileUnit::GetNumLineEntries() const {
46   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
47   if (m_opaque_ptr) {
48     LineTable *line_table = m_opaque_ptr->GetLineTable();
49     if (line_table) {
50       log->Printf("SBCompileUnit(%p)::GetNumLineEntries() => %d",
51                 static_cast<void *>(m_opaque_ptr),
52                 (int)line_table->GetSize());
53       return line_table->GetSize();
54     }
55   }
56   return 0;
57 }
58 
59 SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
60   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
61 
62   SBLineEntry sb_line_entry;
63   if (m_opaque_ptr) {
64     LineTable *line_table = m_opaque_ptr->GetLineTable();
65     if (line_table) {
66       LineEntry line_entry;
67       if (line_table->GetLineEntryAtIndex(idx, line_entry))
68         sb_line_entry.SetLineEntry(line_entry);
69     }
70   }
71 
72   if (log) {
73     SBStream sstr;
74     sb_line_entry.GetDescription(sstr);
75     log->Printf("SBCompileUnit(%p)::GetLineEntryAtIndex (idx=%u) => "
76                 "SBLineEntry(%p): '%s'",
77                 static_cast<void *>(m_opaque_ptr), idx,
78                 static_cast<void *>(sb_line_entry.get()), sstr.GetData());
79   }
80 
81   return sb_line_entry;
82 }
83 
84 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
85                                            SBFileSpec *inline_file_spec) const {
86   const bool exact = true;
87   return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
88 }
89 
90 uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
91                                            SBFileSpec *inline_file_spec,
92                                            bool exact) const {
93   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
94 
95   uint32_t index = UINT32_MAX;
96   if (m_opaque_ptr) {
97     FileSpec file_spec;
98     if (inline_file_spec && inline_file_spec->IsValid())
99       file_spec = inline_file_spec->ref();
100     else
101       file_spec = *m_opaque_ptr;
102 
103     index = m_opaque_ptr->FindLineEntry(
104         start_idx, line, inline_file_spec ? inline_file_spec->get() : NULL,
105         exact, NULL);
106   }
107 
108   if (log) {
109     SBStream sstr;
110     if (index == UINT32_MAX) {
111       log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
112                   "line=%u, SBFileSpec(%p)) => NOT FOUND",
113                   static_cast<void *>(m_opaque_ptr), start_idx, line,
114                   inline_file_spec
115                       ? static_cast<const void *>(inline_file_spec->get())
116                       : NULL);
117     } else {
118       log->Printf("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, "
119                   "line=%u, SBFileSpec(%p)) => %u",
120                   static_cast<void *>(m_opaque_ptr), start_idx, line,
121                   inline_file_spec
122                       ? static_cast<const void *>(inline_file_spec->get())
123                       : NULL,
124                   index);
125     }
126   }
127 
128   return index;
129 }
130 
131 uint32_t SBCompileUnit::GetNumSupportFiles() const {
132   if (m_opaque_ptr) {
133     FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
134     return support_files.GetSize();
135   }
136   return 0;
137 }
138 
139 lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
140   SBTypeList sb_type_list;
141 
142   if (!m_opaque_ptr)
143     return sb_type_list;
144 
145   ModuleSP module_sp(m_opaque_ptr->GetModule());
146   if (!module_sp)
147     return sb_type_list;
148 
149   SymbolVendor *vendor = module_sp->GetSymbolVendor();
150   if (!vendor)
151     return sb_type_list;
152 
153   TypeClass type_class = static_cast<TypeClass>(type_mask);
154   TypeList type_list;
155   vendor->GetTypes(m_opaque_ptr, type_class, type_list);
156   sb_type_list.m_opaque_up->Append(type_list);
157   return sb_type_list;
158 }
159 
160 SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
161   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
162 
163   SBFileSpec sb_file_spec;
164   if (m_opaque_ptr) {
165     FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
166     FileSpec file_spec = support_files.GetFileSpecAtIndex(idx);
167     sb_file_spec.SetFileSpec(file_spec);
168   }
169 
170   if (log) {
171     SBStream sstr;
172     sb_file_spec.GetDescription(sstr);
173     log->Printf("SBCompileUnit(%p)::GetGetFileSpecAtIndex (idx=%u) => "
174                 "SBFileSpec(%p): '%s'",
175                 static_cast<void *>(m_opaque_ptr), idx,
176                 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
177   }
178 
179   return sb_file_spec;
180 }
181 
182 uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
183                                              const SBFileSpec &sb_file,
184                                              bool full) {
185   if (m_opaque_ptr) {
186     FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
187     return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
188   }
189   return 0;
190 }
191 
192 lldb::LanguageType SBCompileUnit::GetLanguage() {
193   if (m_opaque_ptr)
194     return m_opaque_ptr->GetLanguage();
195   return lldb::eLanguageTypeUnknown;
196 }
197 
198 bool SBCompileUnit::IsValid() const { return m_opaque_ptr != NULL; }
199 
200 bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
201   return m_opaque_ptr == rhs.m_opaque_ptr;
202 }
203 
204 bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
205   return m_opaque_ptr != rhs.m_opaque_ptr;
206 }
207 
208 const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
209   return m_opaque_ptr;
210 }
211 
212 const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
213   return *m_opaque_ptr;
214 }
215 
216 lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
217 
218 void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
219   m_opaque_ptr = lldb_object_ptr;
220 }
221 
222 bool SBCompileUnit::GetDescription(SBStream &description) {
223   Stream &strm = description.ref();
224 
225   if (m_opaque_ptr) {
226     m_opaque_ptr->Dump(&strm, false);
227   } else
228     strm.PutCString("No value");
229 
230   return true;
231 }
232