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   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;
52 }
53 
54 SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
55   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
56 
57   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);
64     }
65   }
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;
77 }
78 
79 uint32_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);
83 }
84 
85 uint32_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));
89 
90   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();
95     else
96       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 
126 uint32_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 
134 lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
135   SBTypeList sb_type_list;
136 
137   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);
151   sb_type_list.m_opaque_ap->Append(type_list);
152   return sb_type_list;
153 }
154 
155 SBFileSpec 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 
177 uint32_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 
187 lldb::LanguageType SBCompileUnit::GetLanguage() {
188   if (m_opaque_ptr)
189     return m_opaque_ptr->GetLanguage();
190   return lldb::eLanguageTypeUnknown;
191 }
192 
193 bool SBCompileUnit::IsValid() const { return m_opaque_ptr != NULL; }
194 
195 bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
196   return m_opaque_ptr == rhs.m_opaque_ptr;
197 }
198 
199 bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
200   return m_opaque_ptr != rhs.m_opaque_ptr;
201 }
202 
203 const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
204   return m_opaque_ptr;
205 }
206 
207 const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
208   return *m_opaque_ptr;
209 }
210 
211 lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
212 
213 void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
214   m_opaque_ptr = lldb_object_ptr;
215 }
216 
217 bool 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;
226 }
227