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