15ffd83dbSDimitry Andric //===-- CompileUnit.cpp ---------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
10*0b57cec5SDimitry Andric #include "lldb/Core/Module.h"
11*0b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h"
129dba64beSDimitry Andric #include "lldb/Symbol/SymbolFile.h"
13*0b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/Language.h"
15480093f4SDimitry Andric #include "lldb/Utility/Timer.h"
16bdd1243dSDimitry Andric #include <optional>
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric using namespace lldb;
19*0b57cec5SDimitry Andric using namespace lldb_private;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
22*0b57cec5SDimitry Andric                          const char *pathname, const lldb::user_id_t cu_sym_id,
23*0b57cec5SDimitry Andric                          lldb::LanguageType language,
24*0b57cec5SDimitry Andric                          lldb_private::LazyBool is_optimized)
25480093f4SDimitry Andric     : CompileUnit(module_sp, user_data, FileSpec(pathname), cu_sym_id, language,
26480093f4SDimitry Andric                   is_optimized) {}
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
29*0b57cec5SDimitry Andric                          const FileSpec &fspec, const lldb::user_id_t cu_sym_id,
30*0b57cec5SDimitry Andric                          lldb::LanguageType language,
31*0b57cec5SDimitry Andric                          lldb_private::LazyBool is_optimized)
32480093f4SDimitry Andric     : ModuleChild(module_sp), UserID(cu_sym_id), m_user_data(user_data),
33480093f4SDimitry Andric       m_language(language), m_flags(0), m_file_spec(fspec),
34*0b57cec5SDimitry Andric       m_is_optimized(is_optimized) {
35*0b57cec5SDimitry Andric   if (language != eLanguageTypeUnknown)
36*0b57cec5SDimitry Andric     m_flags.Set(flagsParsedLanguage);
37*0b57cec5SDimitry Andric   assert(module_sp);
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric void CompileUnit::CalculateSymbolContext(SymbolContext *sc) {
41*0b57cec5SDimitry Andric   sc->comp_unit = this;
42*0b57cec5SDimitry Andric   GetModule()->CalculateSymbolContext(sc);
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric ModuleSP CompileUnit::CalculateSymbolContextModule() { return GetModule(); }
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric CompileUnit *CompileUnit::CalculateSymbolContextCompileUnit() { return this; }
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric void CompileUnit::DumpSymbolContext(Stream *s) {
50*0b57cec5SDimitry Andric   GetModule()->DumpSymbolContext(s);
51*0b57cec5SDimitry Andric   s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());
52*0b57cec5SDimitry Andric }
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric void CompileUnit::GetDescription(Stream *s,
55*0b57cec5SDimitry Andric                                  lldb::DescriptionLevel level) const {
56349cc55cSDimitry Andric   const char *language = GetCachedLanguage();
57*0b57cec5SDimitry Andric   *s << "id = " << (const UserID &)*this << ", file = \""
58480093f4SDimitry Andric      << this->GetPrimaryFile() << "\", language = \"" << language << '"';
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric void CompileUnit::ForeachFunction(
62*0b57cec5SDimitry Andric     llvm::function_ref<bool(const FunctionSP &)> lambda) const {
63*0b57cec5SDimitry Andric   std::vector<lldb::FunctionSP> sorted_functions;
64*0b57cec5SDimitry Andric   sorted_functions.reserve(m_functions_by_uid.size());
65*0b57cec5SDimitry Andric   for (auto &p : m_functions_by_uid)
66*0b57cec5SDimitry Andric     sorted_functions.push_back(p.second);
67fcaf7f86SDimitry Andric   llvm::sort(sorted_functions,
68*0b57cec5SDimitry Andric              [](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {
69*0b57cec5SDimitry Andric                return a->GetID() < b->GetID();
70*0b57cec5SDimitry Andric              });
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric   for (auto &f : sorted_functions)
73*0b57cec5SDimitry Andric     if (lambda(f))
74*0b57cec5SDimitry Andric       return;
75*0b57cec5SDimitry Andric }
76*0b57cec5SDimitry Andric 
77480093f4SDimitry Andric lldb::FunctionSP CompileUnit::FindFunction(
78480093f4SDimitry Andric     llvm::function_ref<bool(const FunctionSP &)> matching_lambda) {
79e8d8bef9SDimitry Andric   LLDB_SCOPED_TIMER();
80480093f4SDimitry Andric 
81480093f4SDimitry Andric   lldb::ModuleSP module = CalculateSymbolContextModule();
82480093f4SDimitry Andric 
83480093f4SDimitry Andric   if (!module)
84480093f4SDimitry Andric     return {};
85480093f4SDimitry Andric 
86480093f4SDimitry Andric   SymbolFile *symbol_file = module->GetSymbolFile();
87480093f4SDimitry Andric 
88480093f4SDimitry Andric   if (!symbol_file)
89480093f4SDimitry Andric     return {};
90480093f4SDimitry Andric 
91480093f4SDimitry Andric   // m_functions_by_uid is filled in lazily but we need all the entries.
92480093f4SDimitry Andric   symbol_file->ParseFunctions(*this);
93480093f4SDimitry Andric 
94480093f4SDimitry Andric   for (auto &p : m_functions_by_uid) {
95480093f4SDimitry Andric     if (matching_lambda(p.second))
96480093f4SDimitry Andric       return p.second;
97480093f4SDimitry Andric   }
98480093f4SDimitry Andric   return {};
99480093f4SDimitry Andric }
100480093f4SDimitry Andric 
101349cc55cSDimitry Andric const char *CompileUnit::GetCachedLanguage() const {
102349cc55cSDimitry Andric   if (m_flags.IsClear(flagsParsedLanguage))
103349cc55cSDimitry Andric     return "<not loaded>";
104349cc55cSDimitry Andric   return Language::GetNameForLanguageType(m_language);
105349cc55cSDimitry Andric }
106349cc55cSDimitry Andric 
107*0b57cec5SDimitry Andric // Dump the current contents of this object. No functions that cause on demand
108*0b57cec5SDimitry Andric // parsing of functions, globals, statics are called, so this is a good
109*0b57cec5SDimitry Andric // function to call to get an idea of the current contents of the CompileUnit
110*0b57cec5SDimitry Andric // object.
111*0b57cec5SDimitry Andric void CompileUnit::Dump(Stream *s, bool show_context) const {
112349cc55cSDimitry Andric   const char *language = GetCachedLanguage();
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric   s->Printf("%p: ", static_cast<const void *>(this));
115*0b57cec5SDimitry Andric   s->Indent();
116*0b57cec5SDimitry Andric   *s << "CompileUnit" << static_cast<const UserID &>(*this) << ", language = \""
117480093f4SDimitry Andric      << language << "\", file = '" << GetPrimaryFile() << "'\n";
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric   //  m_types.Dump(s);
120*0b57cec5SDimitry Andric 
121*0b57cec5SDimitry Andric   if (m_variables.get()) {
122*0b57cec5SDimitry Andric     s->IndentMore();
123*0b57cec5SDimitry Andric     m_variables->Dump(s, show_context);
124*0b57cec5SDimitry Andric     s->IndentLess();
125*0b57cec5SDimitry Andric   }
126*0b57cec5SDimitry Andric 
127*0b57cec5SDimitry Andric   if (!m_functions_by_uid.empty()) {
128*0b57cec5SDimitry Andric     s->IndentMore();
129*0b57cec5SDimitry Andric     ForeachFunction([&s, show_context](const FunctionSP &f) {
130*0b57cec5SDimitry Andric       f->Dump(s, show_context);
131*0b57cec5SDimitry Andric       return false;
132*0b57cec5SDimitry Andric     });
133*0b57cec5SDimitry Andric 
134*0b57cec5SDimitry Andric     s->IndentLess();
135*0b57cec5SDimitry Andric     s->EOL();
136*0b57cec5SDimitry Andric   }
137*0b57cec5SDimitry Andric }
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric // Add a function to this compile unit
140*0b57cec5SDimitry Andric void CompileUnit::AddFunction(FunctionSP &funcSP) {
141*0b57cec5SDimitry Andric   m_functions_by_uid[funcSP->GetID()] = funcSP;
142*0b57cec5SDimitry Andric }
143*0b57cec5SDimitry Andric 
144*0b57cec5SDimitry Andric FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {
145*0b57cec5SDimitry Andric   auto it = m_functions_by_uid.find(func_uid);
146*0b57cec5SDimitry Andric   if (it == m_functions_by_uid.end())
147*0b57cec5SDimitry Andric     return FunctionSP();
148*0b57cec5SDimitry Andric   return it->second;
149*0b57cec5SDimitry Andric }
150*0b57cec5SDimitry Andric 
151*0b57cec5SDimitry Andric lldb::LanguageType CompileUnit::GetLanguage() {
152*0b57cec5SDimitry Andric   if (m_language == eLanguageTypeUnknown) {
153*0b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedLanguage)) {
154*0b57cec5SDimitry Andric       m_flags.Set(flagsParsedLanguage);
1559dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
1569dba64beSDimitry Andric         m_language = symfile->ParseLanguage(*this);
157*0b57cec5SDimitry Andric     }
158*0b57cec5SDimitry Andric   }
159*0b57cec5SDimitry Andric   return m_language;
160*0b57cec5SDimitry Andric }
161*0b57cec5SDimitry Andric 
162*0b57cec5SDimitry Andric LineTable *CompileUnit::GetLineTable() {
163*0b57cec5SDimitry Andric   if (m_line_table_up == nullptr) {
164*0b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedLineTable)) {
165*0b57cec5SDimitry Andric       m_flags.Set(flagsParsedLineTable);
1669dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
1679dba64beSDimitry Andric         symfile->ParseLineTable(*this);
168*0b57cec5SDimitry Andric     }
169*0b57cec5SDimitry Andric   }
170*0b57cec5SDimitry Andric   return m_line_table_up.get();
171*0b57cec5SDimitry Andric }
172*0b57cec5SDimitry Andric 
173*0b57cec5SDimitry Andric void CompileUnit::SetLineTable(LineTable *line_table) {
174*0b57cec5SDimitry Andric   if (line_table == nullptr)
175*0b57cec5SDimitry Andric     m_flags.Clear(flagsParsedLineTable);
176*0b57cec5SDimitry Andric   else
177*0b57cec5SDimitry Andric     m_flags.Set(flagsParsedLineTable);
178*0b57cec5SDimitry Andric   m_line_table_up.reset(line_table);
179*0b57cec5SDimitry Andric }
180*0b57cec5SDimitry Andric 
1819dba64beSDimitry Andric void CompileUnit::SetSupportFiles(const FileSpecList &support_files) {
1829dba64beSDimitry Andric   m_support_files = support_files;
1839dba64beSDimitry Andric }
1849dba64beSDimitry Andric 
185349cc55cSDimitry Andric void CompileUnit::SetSupportFiles(FileSpecList &&support_files) {
186349cc55cSDimitry Andric   m_support_files = std::move(support_files);
187349cc55cSDimitry Andric }
188349cc55cSDimitry Andric 
189*0b57cec5SDimitry Andric DebugMacros *CompileUnit::GetDebugMacros() {
190*0b57cec5SDimitry Andric   if (m_debug_macros_sp.get() == nullptr) {
191*0b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedDebugMacros)) {
192*0b57cec5SDimitry Andric       m_flags.Set(flagsParsedDebugMacros);
1939dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
1949dba64beSDimitry Andric         symfile->ParseDebugMacros(*this);
195*0b57cec5SDimitry Andric     }
196*0b57cec5SDimitry Andric   }
197*0b57cec5SDimitry Andric 
198*0b57cec5SDimitry Andric   return m_debug_macros_sp.get();
199*0b57cec5SDimitry Andric }
200*0b57cec5SDimitry Andric 
201*0b57cec5SDimitry Andric void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) {
202*0b57cec5SDimitry Andric   if (debug_macros_sp.get() == nullptr)
203*0b57cec5SDimitry Andric     m_flags.Clear(flagsParsedDebugMacros);
204*0b57cec5SDimitry Andric   else
205*0b57cec5SDimitry Andric     m_flags.Set(flagsParsedDebugMacros);
206*0b57cec5SDimitry Andric   m_debug_macros_sp = debug_macros_sp;
207*0b57cec5SDimitry Andric }
208*0b57cec5SDimitry Andric 
209*0b57cec5SDimitry Andric VariableListSP CompileUnit::GetVariableList(bool can_create) {
210*0b57cec5SDimitry Andric   if (m_variables.get() == nullptr && can_create) {
211*0b57cec5SDimitry Andric     SymbolContext sc;
212*0b57cec5SDimitry Andric     CalculateSymbolContext(&sc);
213*0b57cec5SDimitry Andric     assert(sc.module_sp);
2149dba64beSDimitry Andric     sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc);
215*0b57cec5SDimitry Andric   }
216*0b57cec5SDimitry Andric 
217*0b57cec5SDimitry Andric   return m_variables;
218*0b57cec5SDimitry Andric }
219*0b57cec5SDimitry Andric 
220bdd1243dSDimitry Andric std::vector<uint32_t> FindFileIndexes(const FileSpecList &files,
221bdd1243dSDimitry Andric                                       const FileSpec &file) {
222480093f4SDimitry Andric   std::vector<uint32_t> result;
223480093f4SDimitry Andric   uint32_t idx = -1;
224bdd1243dSDimitry Andric   while ((idx = files.FindCompatibleIndex(idx + 1, file)) !=
225480093f4SDimitry Andric          UINT32_MAX)
226480093f4SDimitry Andric     result.push_back(idx);
227480093f4SDimitry Andric   return result;
228480093f4SDimitry Andric }
229480093f4SDimitry Andric 
230*0b57cec5SDimitry Andric uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line,
231*0b57cec5SDimitry Andric                                     const FileSpec *file_spec_ptr, bool exact,
232*0b57cec5SDimitry Andric                                     LineEntry *line_entry_ptr) {
233480093f4SDimitry Andric   if (!file_spec_ptr)
234480093f4SDimitry Andric     file_spec_ptr = &GetPrimaryFile();
235bdd1243dSDimitry Andric   std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),
236bdd1243dSDimitry Andric                                                        *file_spec_ptr);
237480093f4SDimitry Andric   if (file_indexes.empty())
238*0b57cec5SDimitry Andric     return UINT32_MAX;
239480093f4SDimitry Andric 
240fe6060f1SDimitry Andric   // TODO: Handle SourceLocationSpec column information
241bdd1243dSDimitry Andric   SourceLocationSpec location_spec(*file_spec_ptr, line,
242bdd1243dSDimitry Andric                                    /*column=*/std::nullopt,
243fe6060f1SDimitry Andric                                    /*check_inlines=*/false, exact);
244fe6060f1SDimitry Andric 
245*0b57cec5SDimitry Andric   LineTable *line_table = GetLineTable();
246*0b57cec5SDimitry Andric   if (line_table)
247480093f4SDimitry Andric     return line_table->FindLineEntryIndexByFileIndex(
248fe6060f1SDimitry Andric         start_idx, file_indexes, location_spec, line_entry_ptr);
249*0b57cec5SDimitry Andric   return UINT32_MAX;
250*0b57cec5SDimitry Andric }
251*0b57cec5SDimitry Andric 
252fe6060f1SDimitry Andric void CompileUnit::ResolveSymbolContext(
253fe6060f1SDimitry Andric     const SourceLocationSpec &src_location_spec,
254fe6060f1SDimitry Andric     SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
255fe6060f1SDimitry Andric   const FileSpec file_spec = src_location_spec.GetFileSpec();
25681ad6265SDimitry Andric   const uint32_t line = src_location_spec.GetLine().value_or(0);
257fe6060f1SDimitry Andric   const bool check_inlines = src_location_spec.GetCheckInlines();
258fe6060f1SDimitry Andric 
259*0b57cec5SDimitry Andric   // First find all of the file indexes that match our "file_spec". If
260*0b57cec5SDimitry Andric   // "file_spec" has an empty directory, then only compare the basenames when
261*0b57cec5SDimitry Andric   // finding file indexes
262*0b57cec5SDimitry Andric   bool file_spec_matches_cu_file_spec =
263480093f4SDimitry Andric       FileSpec::Match(file_spec, this->GetPrimaryFile());
264*0b57cec5SDimitry Andric 
265*0b57cec5SDimitry Andric   // If we are not looking for inlined functions and our file spec doesn't
266*0b57cec5SDimitry Andric   // match then we are done...
267*0b57cec5SDimitry Andric   if (!file_spec_matches_cu_file_spec && !check_inlines)
268480093f4SDimitry Andric     return;
269*0b57cec5SDimitry Andric 
270*0b57cec5SDimitry Andric   SymbolContext sc(GetModule());
271*0b57cec5SDimitry Andric   sc.comp_unit = this;
272*0b57cec5SDimitry Andric 
273480093f4SDimitry Andric   if (line == 0) {
274480093f4SDimitry Andric     if (file_spec_matches_cu_file_spec && !check_inlines) {
275480093f4SDimitry Andric       // only append the context if we aren't looking for inline call sites by
276480093f4SDimitry Andric       // file and line and if the file spec matches that of the compile unit
277480093f4SDimitry Andric       sc_list.Append(sc);
278480093f4SDimitry Andric     }
279480093f4SDimitry Andric     return;
280480093f4SDimitry Andric   }
281480093f4SDimitry Andric 
282bdd1243dSDimitry Andric   std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),
283bdd1243dSDimitry Andric                                                        file_spec);
284fe6060f1SDimitry Andric   const size_t num_file_indexes = file_indexes.size();
285fe6060f1SDimitry Andric   if (num_file_indexes == 0)
286fe6060f1SDimitry Andric     return;
287fe6060f1SDimitry Andric 
28881ad6265SDimitry Andric   // Found a matching source file in this compile unit load its debug info.
28981ad6265SDimitry Andric   GetModule()->GetSymbolFile()->SetLoadDebugInfoEnabled();
29081ad6265SDimitry Andric 
291*0b57cec5SDimitry Andric   LineTable *line_table = sc.comp_unit->GetLineTable();
292*0b57cec5SDimitry Andric 
293fe6060f1SDimitry Andric   if (line_table == nullptr) {
294fe6060f1SDimitry Andric     if (file_spec_matches_cu_file_spec && !check_inlines) {
295fe6060f1SDimitry Andric       sc_list.Append(sc);
296fe6060f1SDimitry Andric     }
297480093f4SDimitry Andric     return;
298fe6060f1SDimitry Andric   }
299480093f4SDimitry Andric 
300*0b57cec5SDimitry Andric   uint32_t line_idx;
301480093f4SDimitry Andric   LineEntry line_entry;
302*0b57cec5SDimitry Andric 
303*0b57cec5SDimitry Andric   if (num_file_indexes == 1) {
304*0b57cec5SDimitry Andric     // We only have a single support file that matches, so use the line
305*0b57cec5SDimitry Andric     // table function that searches for a line entries that match a single
306*0b57cec5SDimitry Andric     // support file index
307*0b57cec5SDimitry Andric     line_idx = line_table->FindLineEntryIndexByFileIndex(
308fe6060f1SDimitry Andric         0, file_indexes.front(), src_location_spec, &line_entry);
309480093f4SDimitry Andric   } else {
310480093f4SDimitry Andric     // We found multiple support files that match "file_spec" so use the
311480093f4SDimitry Andric     // line table function that searches for a line entries that match a
312480093f4SDimitry Andric     // multiple support file indexes.
313fe6060f1SDimitry Andric     line_idx = line_table->FindLineEntryIndexByFileIndex(
314fe6060f1SDimitry Andric         0, file_indexes, src_location_spec, &line_entry);
315480093f4SDimitry Andric   }
316*0b57cec5SDimitry Andric 
317*0b57cec5SDimitry Andric   // If "exact == true", then "found_line" will be the same as "line". If
318*0b57cec5SDimitry Andric   // "exact == false", the "found_line" will be the closest line entry
319*0b57cec5SDimitry Andric   // with a line number greater than "line" and we will use this for our
320*0b57cec5SDimitry Andric   // subsequent line exact matches below.
321fe6060f1SDimitry Andric   const bool inlines = false;
322fe6060f1SDimitry Andric   const bool exact = true;
323bdd1243dSDimitry Andric   const std::optional<uint16_t> column =
324bdd1243dSDimitry Andric       src_location_spec.GetColumn() ? std::optional<uint16_t>(line_entry.column)
325bdd1243dSDimitry Andric                                     : std::nullopt;
326349cc55cSDimitry Andric 
327349cc55cSDimitry Andric   SourceLocationSpec found_entry(line_entry.file, line_entry.line, column,
328349cc55cSDimitry Andric                                  inlines, exact);
329*0b57cec5SDimitry Andric 
330*0b57cec5SDimitry Andric   while (line_idx != UINT32_MAX) {
331*0b57cec5SDimitry Andric     // If they only asked for the line entry, then we're done, we can
332*0b57cec5SDimitry Andric     // just copy that over. But if they wanted more than just the line
333*0b57cec5SDimitry Andric     // number, fill it in.
334bdd1243dSDimitry Andric     SymbolContext resolved_sc;
335*0b57cec5SDimitry Andric     sc.line_entry = line_entry;
336bdd1243dSDimitry Andric     if (resolve_scope == eSymbolContextLineEntry) {
337bdd1243dSDimitry Andric       sc_list.Append(sc);
338*0b57cec5SDimitry Andric     } else {
339bdd1243dSDimitry Andric       line_entry.range.GetBaseAddress().CalculateSymbolContext(&resolved_sc,
340480093f4SDimitry Andric                                                                resolve_scope);
341bdd1243dSDimitry Andric       // Sometimes debug info is bad and isn't able to resolve the line entry's
342bdd1243dSDimitry Andric       // address back to the same compile unit and/or line entry. If the compile
343bdd1243dSDimitry Andric       // unit changed, then revert back to just the compile unit and line entry.
344bdd1243dSDimitry Andric       // Prior to this fix, the above code might end up not being able to lookup
345bdd1243dSDimitry Andric       // the address, and then it would clear compile unit and the line entry in
346bdd1243dSDimitry Andric       // the symbol context and the breakpoint would fail to get set even though
347bdd1243dSDimitry Andric       // we have a valid line table entry in this compile unit. The address
348bdd1243dSDimitry Andric       // lookup can also end up finding another function in another compiler
349bdd1243dSDimitry Andric       // unit if the DWARF has overlappging address ranges. So if we end up with
350bdd1243dSDimitry Andric       // no compile unit or a different one after the above function call,
351bdd1243dSDimitry Andric       // revert back to the same results as if resolve_scope was set exactly to
352bdd1243dSDimitry Andric       // eSymbolContextLineEntry.
353bdd1243dSDimitry Andric       if (resolved_sc.comp_unit == this) {
354bdd1243dSDimitry Andric         sc_list.Append(resolved_sc);
355bdd1243dSDimitry Andric       } else {
356bdd1243dSDimitry Andric         if (resolved_sc.comp_unit == nullptr && resolved_sc.module_sp) {
357bdd1243dSDimitry Andric           // Only report an error if we don't map back to any compile unit. With
358bdd1243dSDimitry Andric           // link time optimizations, the debug info might have many compile
359bdd1243dSDimitry Andric           // units that have the same address range due to function outlining
360bdd1243dSDimitry Andric           // or other link time optimizations. If the compile unit is NULL, then
361bdd1243dSDimitry Andric           // address resolving is completely failing and more deserving of an
362bdd1243dSDimitry Andric           // error message the user can see.
363bdd1243dSDimitry Andric           resolved_sc.module_sp->ReportError(
364bdd1243dSDimitry Andric               "unable to resolve a line table file address {0:x16} back "
365bdd1243dSDimitry Andric               "to a compile unit, please file a bug and attach the address "
366bdd1243dSDimitry Andric               "and file.",
367bdd1243dSDimitry Andric               line_entry.range.GetBaseAddress().GetFileAddress());
368bdd1243dSDimitry Andric         }
369bdd1243dSDimitry Andric         sc_list.Append(sc);
370bdd1243dSDimitry Andric       }
371*0b57cec5SDimitry Andric     }
372*0b57cec5SDimitry Andric 
373480093f4SDimitry Andric     if (num_file_indexes == 1)
374*0b57cec5SDimitry Andric       line_idx = line_table->FindLineEntryIndexByFileIndex(
375fe6060f1SDimitry Andric           line_idx + 1, file_indexes.front(), found_entry, &line_entry);
376480093f4SDimitry Andric     else
377*0b57cec5SDimitry Andric       line_idx = line_table->FindLineEntryIndexByFileIndex(
378fe6060f1SDimitry Andric           line_idx + 1, file_indexes, found_entry, &line_entry);
379*0b57cec5SDimitry Andric   }
380*0b57cec5SDimitry Andric }
381*0b57cec5SDimitry Andric 
382*0b57cec5SDimitry Andric bool CompileUnit::GetIsOptimized() {
383*0b57cec5SDimitry Andric   if (m_is_optimized == eLazyBoolCalculate) {
384*0b57cec5SDimitry Andric     m_is_optimized = eLazyBoolNo;
3859dba64beSDimitry Andric     if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {
3869dba64beSDimitry Andric       if (symfile->ParseIsOptimized(*this))
387*0b57cec5SDimitry Andric         m_is_optimized = eLazyBoolYes;
388*0b57cec5SDimitry Andric     }
389*0b57cec5SDimitry Andric   }
390*0b57cec5SDimitry Andric   return m_is_optimized;
391*0b57cec5SDimitry Andric }
392*0b57cec5SDimitry Andric 
393*0b57cec5SDimitry Andric void CompileUnit::SetVariableList(VariableListSP &variables) {
394*0b57cec5SDimitry Andric   m_variables = variables;
395*0b57cec5SDimitry Andric }
396*0b57cec5SDimitry Andric 
397*0b57cec5SDimitry Andric const std::vector<SourceModule> &CompileUnit::GetImportedModules() {
398*0b57cec5SDimitry Andric   if (m_imported_modules.empty() &&
399*0b57cec5SDimitry Andric       m_flags.IsClear(flagsParsedImportedModules)) {
400*0b57cec5SDimitry Andric     m_flags.Set(flagsParsedImportedModules);
4019dba64beSDimitry Andric     if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {
402*0b57cec5SDimitry Andric       SymbolContext sc;
403*0b57cec5SDimitry Andric       CalculateSymbolContext(&sc);
4049dba64beSDimitry Andric       symfile->ParseImportedModules(sc, m_imported_modules);
405*0b57cec5SDimitry Andric     }
406*0b57cec5SDimitry Andric   }
407*0b57cec5SDimitry Andric   return m_imported_modules;
408*0b57cec5SDimitry Andric }
409*0b57cec5SDimitry Andric 
410480093f4SDimitry Andric bool CompileUnit::ForEachExternalModule(
411480093f4SDimitry Andric     llvm::DenseSet<SymbolFile *> &visited_symbol_files,
412480093f4SDimitry Andric     llvm::function_ref<bool(Module &)> lambda) {
4139dba64beSDimitry Andric   if (SymbolFile *symfile = GetModule()->GetSymbolFile())
414480093f4SDimitry Andric     return symfile->ForEachExternalModule(*this, visited_symbol_files, lambda);
415480093f4SDimitry Andric   return false;
4169dba64beSDimitry Andric }
4179dba64beSDimitry Andric 
418*0b57cec5SDimitry Andric const FileSpecList &CompileUnit::GetSupportFiles() {
419*0b57cec5SDimitry Andric   if (m_support_files.GetSize() == 0) {
420*0b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedSupportFiles)) {
421*0b57cec5SDimitry Andric       m_flags.Set(flagsParsedSupportFiles);
4229dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
4239dba64beSDimitry Andric         symfile->ParseSupportFiles(*this, m_support_files);
424*0b57cec5SDimitry Andric     }
425*0b57cec5SDimitry Andric   }
426*0b57cec5SDimitry Andric   return m_support_files;
427*0b57cec5SDimitry Andric }
428*0b57cec5SDimitry Andric 
429*0b57cec5SDimitry Andric void *CompileUnit::GetUserData() const { return m_user_data; }
430