15ffd83dbSDimitry Andric //===-- CompileUnit.cpp ---------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
100b57cec5SDimitry Andric #include "lldb/Core/Module.h"
110b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h"
129dba64beSDimitry Andric #include "lldb/Symbol/SymbolFile.h"
130b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
140b57cec5SDimitry Andric #include "lldb/Target/Language.h"
15480093f4SDimitry Andric #include "lldb/Utility/Timer.h"
16bdd1243dSDimitry Andric #include <optional>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace lldb;
190b57cec5SDimitry Andric using namespace lldb_private;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
220b57cec5SDimitry Andric                          const char *pathname, const lldb::user_id_t cu_sym_id,
230b57cec5SDimitry Andric                          lldb::LanguageType language,
240b57cec5SDimitry Andric                          lldb_private::LazyBool is_optimized)
25480093f4SDimitry Andric     : CompileUnit(module_sp, user_data, FileSpec(pathname), cu_sym_id, language,
26480093f4SDimitry Andric                   is_optimized) {}
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
290b57cec5SDimitry Andric                          const FileSpec &fspec, const lldb::user_id_t cu_sym_id,
300b57cec5SDimitry Andric                          lldb::LanguageType language,
31*cdc20ff6SDimitry Andric                          lldb_private::LazyBool is_optimized,
32*cdc20ff6SDimitry Andric                          SupportFileList &&support_files)
33480093f4SDimitry Andric     : ModuleChild(module_sp), UserID(cu_sym_id), m_user_data(user_data),
34480093f4SDimitry Andric       m_language(language), m_flags(0), m_file_spec(fspec),
35*cdc20ff6SDimitry Andric       m_support_files(std::move(support_files)), m_is_optimized(is_optimized) {
360b57cec5SDimitry Andric   if (language != eLanguageTypeUnknown)
370b57cec5SDimitry Andric     m_flags.Set(flagsParsedLanguage);
380b57cec5SDimitry Andric   assert(module_sp);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric void CompileUnit::CalculateSymbolContext(SymbolContext *sc) {
420b57cec5SDimitry Andric   sc->comp_unit = this;
430b57cec5SDimitry Andric   GetModule()->CalculateSymbolContext(sc);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric ModuleSP CompileUnit::CalculateSymbolContextModule() { return GetModule(); }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric CompileUnit *CompileUnit::CalculateSymbolContextCompileUnit() { return this; }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric void CompileUnit::DumpSymbolContext(Stream *s) {
510b57cec5SDimitry Andric   GetModule()->DumpSymbolContext(s);
520b57cec5SDimitry Andric   s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric void CompileUnit::GetDescription(Stream *s,
560b57cec5SDimitry Andric                                  lldb::DescriptionLevel level) const {
57349cc55cSDimitry Andric   const char *language = GetCachedLanguage();
580b57cec5SDimitry Andric   *s << "id = " << (const UserID &)*this << ", file = \""
59480093f4SDimitry Andric      << this->GetPrimaryFile() << "\", language = \"" << language << '"';
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric void CompileUnit::ForeachFunction(
630b57cec5SDimitry Andric     llvm::function_ref<bool(const FunctionSP &)> lambda) const {
640b57cec5SDimitry Andric   std::vector<lldb::FunctionSP> sorted_functions;
650b57cec5SDimitry Andric   sorted_functions.reserve(m_functions_by_uid.size());
660b57cec5SDimitry Andric   for (auto &p : m_functions_by_uid)
670b57cec5SDimitry Andric     sorted_functions.push_back(p.second);
68fcaf7f86SDimitry Andric   llvm::sort(sorted_functions,
690b57cec5SDimitry Andric              [](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {
700b57cec5SDimitry Andric                return a->GetID() < b->GetID();
710b57cec5SDimitry Andric              });
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   for (auto &f : sorted_functions)
740b57cec5SDimitry Andric     if (lambda(f))
750b57cec5SDimitry Andric       return;
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
78480093f4SDimitry Andric lldb::FunctionSP CompileUnit::FindFunction(
79480093f4SDimitry Andric     llvm::function_ref<bool(const FunctionSP &)> matching_lambda) {
80e8d8bef9SDimitry Andric   LLDB_SCOPED_TIMER();
81480093f4SDimitry Andric 
82480093f4SDimitry Andric   lldb::ModuleSP module = CalculateSymbolContextModule();
83480093f4SDimitry Andric 
84480093f4SDimitry Andric   if (!module)
85480093f4SDimitry Andric     return {};
86480093f4SDimitry Andric 
87480093f4SDimitry Andric   SymbolFile *symbol_file = module->GetSymbolFile();
88480093f4SDimitry Andric 
89480093f4SDimitry Andric   if (!symbol_file)
90480093f4SDimitry Andric     return {};
91480093f4SDimitry Andric 
92480093f4SDimitry Andric   // m_functions_by_uid is filled in lazily but we need all the entries.
93480093f4SDimitry Andric   symbol_file->ParseFunctions(*this);
94480093f4SDimitry Andric 
95480093f4SDimitry Andric   for (auto &p : m_functions_by_uid) {
96480093f4SDimitry Andric     if (matching_lambda(p.second))
97480093f4SDimitry Andric       return p.second;
98480093f4SDimitry Andric   }
99480093f4SDimitry Andric   return {};
100480093f4SDimitry Andric }
101480093f4SDimitry Andric 
102349cc55cSDimitry Andric const char *CompileUnit::GetCachedLanguage() const {
103349cc55cSDimitry Andric   if (m_flags.IsClear(flagsParsedLanguage))
104349cc55cSDimitry Andric     return "<not loaded>";
105349cc55cSDimitry Andric   return Language::GetNameForLanguageType(m_language);
106349cc55cSDimitry Andric }
107349cc55cSDimitry Andric 
1080b57cec5SDimitry Andric // Dump the current contents of this object. No functions that cause on demand
1090b57cec5SDimitry Andric // parsing of functions, globals, statics are called, so this is a good
1100b57cec5SDimitry Andric // function to call to get an idea of the current contents of the CompileUnit
1110b57cec5SDimitry Andric // object.
1120b57cec5SDimitry Andric void CompileUnit::Dump(Stream *s, bool show_context) const {
113349cc55cSDimitry Andric   const char *language = GetCachedLanguage();
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   s->Printf("%p: ", static_cast<const void *>(this));
1160b57cec5SDimitry Andric   s->Indent();
1170b57cec5SDimitry Andric   *s << "CompileUnit" << static_cast<const UserID &>(*this) << ", language = \""
118480093f4SDimitry Andric      << language << "\", file = '" << GetPrimaryFile() << "'\n";
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   //  m_types.Dump(s);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   if (m_variables.get()) {
1230b57cec5SDimitry Andric     s->IndentMore();
1240b57cec5SDimitry Andric     m_variables->Dump(s, show_context);
1250b57cec5SDimitry Andric     s->IndentLess();
1260b57cec5SDimitry Andric   }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   if (!m_functions_by_uid.empty()) {
1290b57cec5SDimitry Andric     s->IndentMore();
1300b57cec5SDimitry Andric     ForeachFunction([&s, show_context](const FunctionSP &f) {
1310b57cec5SDimitry Andric       f->Dump(s, show_context);
1320b57cec5SDimitry Andric       return false;
1330b57cec5SDimitry Andric     });
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric     s->IndentLess();
1360b57cec5SDimitry Andric     s->EOL();
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric // Add a function to this compile unit
1410b57cec5SDimitry Andric void CompileUnit::AddFunction(FunctionSP &funcSP) {
1420b57cec5SDimitry Andric   m_functions_by_uid[funcSP->GetID()] = funcSP;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {
1460b57cec5SDimitry Andric   auto it = m_functions_by_uid.find(func_uid);
1470b57cec5SDimitry Andric   if (it == m_functions_by_uid.end())
1480b57cec5SDimitry Andric     return FunctionSP();
1490b57cec5SDimitry Andric   return it->second;
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric lldb::LanguageType CompileUnit::GetLanguage() {
1530b57cec5SDimitry Andric   if (m_language == eLanguageTypeUnknown) {
1540b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedLanguage)) {
1550b57cec5SDimitry Andric       m_flags.Set(flagsParsedLanguage);
1569dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
1579dba64beSDimitry Andric         m_language = symfile->ParseLanguage(*this);
1580b57cec5SDimitry Andric     }
1590b57cec5SDimitry Andric   }
1600b57cec5SDimitry Andric   return m_language;
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric LineTable *CompileUnit::GetLineTable() {
1640b57cec5SDimitry Andric   if (m_line_table_up == nullptr) {
1650b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedLineTable)) {
1660b57cec5SDimitry Andric       m_flags.Set(flagsParsedLineTable);
1679dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
1689dba64beSDimitry Andric         symfile->ParseLineTable(*this);
1690b57cec5SDimitry Andric     }
1700b57cec5SDimitry Andric   }
1710b57cec5SDimitry Andric   return m_line_table_up.get();
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric void CompileUnit::SetLineTable(LineTable *line_table) {
1750b57cec5SDimitry Andric   if (line_table == nullptr)
1760b57cec5SDimitry Andric     m_flags.Clear(flagsParsedLineTable);
1770b57cec5SDimitry Andric   else
1780b57cec5SDimitry Andric     m_flags.Set(flagsParsedLineTable);
1790b57cec5SDimitry Andric   m_line_table_up.reset(line_table);
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric DebugMacros *CompileUnit::GetDebugMacros() {
1830b57cec5SDimitry Andric   if (m_debug_macros_sp.get() == nullptr) {
1840b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedDebugMacros)) {
1850b57cec5SDimitry Andric       m_flags.Set(flagsParsedDebugMacros);
1869dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
1879dba64beSDimitry Andric         symfile->ParseDebugMacros(*this);
1880b57cec5SDimitry Andric     }
1890b57cec5SDimitry Andric   }
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   return m_debug_macros_sp.get();
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) {
1950b57cec5SDimitry Andric   if (debug_macros_sp.get() == nullptr)
1960b57cec5SDimitry Andric     m_flags.Clear(flagsParsedDebugMacros);
1970b57cec5SDimitry Andric   else
1980b57cec5SDimitry Andric     m_flags.Set(flagsParsedDebugMacros);
1990b57cec5SDimitry Andric   m_debug_macros_sp = debug_macros_sp;
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric VariableListSP CompileUnit::GetVariableList(bool can_create) {
2030b57cec5SDimitry Andric   if (m_variables.get() == nullptr && can_create) {
2040b57cec5SDimitry Andric     SymbolContext sc;
2050b57cec5SDimitry Andric     CalculateSymbolContext(&sc);
2060b57cec5SDimitry Andric     assert(sc.module_sp);
2079dba64beSDimitry Andric     sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc);
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   return m_variables;
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
213*cdc20ff6SDimitry Andric std::vector<uint32_t> FindFileIndexes(const SupportFileList &files,
214bdd1243dSDimitry Andric                                       const FileSpec &file) {
215480093f4SDimitry Andric   std::vector<uint32_t> result;
216480093f4SDimitry Andric   uint32_t idx = -1;
217bdd1243dSDimitry Andric   while ((idx = files.FindCompatibleIndex(idx + 1, file)) !=
218480093f4SDimitry Andric          UINT32_MAX)
219480093f4SDimitry Andric     result.push_back(idx);
220480093f4SDimitry Andric   return result;
221480093f4SDimitry Andric }
222480093f4SDimitry Andric 
2230b57cec5SDimitry Andric uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line,
2240b57cec5SDimitry Andric                                     const FileSpec *file_spec_ptr, bool exact,
2250b57cec5SDimitry Andric                                     LineEntry *line_entry_ptr) {
226480093f4SDimitry Andric   if (!file_spec_ptr)
227480093f4SDimitry Andric     file_spec_ptr = &GetPrimaryFile();
228bdd1243dSDimitry Andric   std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),
229bdd1243dSDimitry Andric                                                        *file_spec_ptr);
230480093f4SDimitry Andric   if (file_indexes.empty())
2310b57cec5SDimitry Andric     return UINT32_MAX;
232480093f4SDimitry Andric 
233fe6060f1SDimitry Andric   // TODO: Handle SourceLocationSpec column information
234bdd1243dSDimitry Andric   SourceLocationSpec location_spec(*file_spec_ptr, line,
235bdd1243dSDimitry Andric                                    /*column=*/std::nullopt,
236fe6060f1SDimitry Andric                                    /*check_inlines=*/false, exact);
237fe6060f1SDimitry Andric 
2380b57cec5SDimitry Andric   LineTable *line_table = GetLineTable();
2390b57cec5SDimitry Andric   if (line_table)
240480093f4SDimitry Andric     return line_table->FindLineEntryIndexByFileIndex(
241fe6060f1SDimitry Andric         start_idx, file_indexes, location_spec, line_entry_ptr);
2420b57cec5SDimitry Andric   return UINT32_MAX;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
245fe6060f1SDimitry Andric void CompileUnit::ResolveSymbolContext(
246fe6060f1SDimitry Andric     const SourceLocationSpec &src_location_spec,
247fe6060f1SDimitry Andric     SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
248fe6060f1SDimitry Andric   const FileSpec file_spec = src_location_spec.GetFileSpec();
24981ad6265SDimitry Andric   const uint32_t line = src_location_spec.GetLine().value_or(0);
250fe6060f1SDimitry Andric   const bool check_inlines = src_location_spec.GetCheckInlines();
251fe6060f1SDimitry Andric 
2520b57cec5SDimitry Andric   // First find all of the file indexes that match our "file_spec". If
2530b57cec5SDimitry Andric   // "file_spec" has an empty directory, then only compare the basenames when
2540b57cec5SDimitry Andric   // finding file indexes
2550b57cec5SDimitry Andric   bool file_spec_matches_cu_file_spec =
256480093f4SDimitry Andric       FileSpec::Match(file_spec, this->GetPrimaryFile());
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   // If we are not looking for inlined functions and our file spec doesn't
2590b57cec5SDimitry Andric   // match then we are done...
2600b57cec5SDimitry Andric   if (!file_spec_matches_cu_file_spec && !check_inlines)
261480093f4SDimitry Andric     return;
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   SymbolContext sc(GetModule());
2640b57cec5SDimitry Andric   sc.comp_unit = this;
2650b57cec5SDimitry Andric 
266480093f4SDimitry Andric   if (line == 0) {
267480093f4SDimitry Andric     if (file_spec_matches_cu_file_spec && !check_inlines) {
268480093f4SDimitry Andric       // only append the context if we aren't looking for inline call sites by
269480093f4SDimitry Andric       // file and line and if the file spec matches that of the compile unit
270480093f4SDimitry Andric       sc_list.Append(sc);
271480093f4SDimitry Andric     }
272480093f4SDimitry Andric     return;
273480093f4SDimitry Andric   }
274480093f4SDimitry Andric 
275bdd1243dSDimitry Andric   std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),
276bdd1243dSDimitry Andric                                                        file_spec);
277fe6060f1SDimitry Andric   const size_t num_file_indexes = file_indexes.size();
278fe6060f1SDimitry Andric   if (num_file_indexes == 0)
279fe6060f1SDimitry Andric     return;
280fe6060f1SDimitry Andric 
28181ad6265SDimitry Andric   // Found a matching source file in this compile unit load its debug info.
28281ad6265SDimitry Andric   GetModule()->GetSymbolFile()->SetLoadDebugInfoEnabled();
28381ad6265SDimitry Andric 
2840b57cec5SDimitry Andric   LineTable *line_table = sc.comp_unit->GetLineTable();
2850b57cec5SDimitry Andric 
286fe6060f1SDimitry Andric   if (line_table == nullptr) {
287fe6060f1SDimitry Andric     if (file_spec_matches_cu_file_spec && !check_inlines) {
288fe6060f1SDimitry Andric       sc_list.Append(sc);
289fe6060f1SDimitry Andric     }
290480093f4SDimitry Andric     return;
291fe6060f1SDimitry Andric   }
292480093f4SDimitry Andric 
2930b57cec5SDimitry Andric   uint32_t line_idx;
294480093f4SDimitry Andric   LineEntry line_entry;
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   if (num_file_indexes == 1) {
2970b57cec5SDimitry Andric     // We only have a single support file that matches, so use the line
2980b57cec5SDimitry Andric     // table function that searches for a line entries that match a single
2990b57cec5SDimitry Andric     // support file index
3000b57cec5SDimitry Andric     line_idx = line_table->FindLineEntryIndexByFileIndex(
301fe6060f1SDimitry Andric         0, file_indexes.front(), src_location_spec, &line_entry);
302480093f4SDimitry Andric   } else {
303480093f4SDimitry Andric     // We found multiple support files that match "file_spec" so use the
304480093f4SDimitry Andric     // line table function that searches for a line entries that match a
305480093f4SDimitry Andric     // multiple support file indexes.
306fe6060f1SDimitry Andric     line_idx = line_table->FindLineEntryIndexByFileIndex(
307fe6060f1SDimitry Andric         0, file_indexes, src_location_spec, &line_entry);
308480093f4SDimitry Andric   }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   // If "exact == true", then "found_line" will be the same as "line". If
3110b57cec5SDimitry Andric   // "exact == false", the "found_line" will be the closest line entry
3120b57cec5SDimitry Andric   // with a line number greater than "line" and we will use this for our
3130b57cec5SDimitry Andric   // subsequent line exact matches below.
314fe6060f1SDimitry Andric   const bool inlines = false;
315fe6060f1SDimitry Andric   const bool exact = true;
316bdd1243dSDimitry Andric   const std::optional<uint16_t> column =
317bdd1243dSDimitry Andric       src_location_spec.GetColumn() ? std::optional<uint16_t>(line_entry.column)
318bdd1243dSDimitry Andric                                     : std::nullopt;
319349cc55cSDimitry Andric 
320349cc55cSDimitry Andric   SourceLocationSpec found_entry(line_entry.file, line_entry.line, column,
321349cc55cSDimitry Andric                                  inlines, exact);
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   while (line_idx != UINT32_MAX) {
3240b57cec5SDimitry Andric     // If they only asked for the line entry, then we're done, we can
3250b57cec5SDimitry Andric     // just copy that over. But if they wanted more than just the line
3260b57cec5SDimitry Andric     // number, fill it in.
327bdd1243dSDimitry Andric     SymbolContext resolved_sc;
3280b57cec5SDimitry Andric     sc.line_entry = line_entry;
329bdd1243dSDimitry Andric     if (resolve_scope == eSymbolContextLineEntry) {
330bdd1243dSDimitry Andric       sc_list.Append(sc);
3310b57cec5SDimitry Andric     } else {
332bdd1243dSDimitry Andric       line_entry.range.GetBaseAddress().CalculateSymbolContext(&resolved_sc,
333480093f4SDimitry Andric                                                                resolve_scope);
334bdd1243dSDimitry Andric       // Sometimes debug info is bad and isn't able to resolve the line entry's
335bdd1243dSDimitry Andric       // address back to the same compile unit and/or line entry. If the compile
336bdd1243dSDimitry Andric       // unit changed, then revert back to just the compile unit and line entry.
337bdd1243dSDimitry Andric       // Prior to this fix, the above code might end up not being able to lookup
338bdd1243dSDimitry Andric       // the address, and then it would clear compile unit and the line entry in
339bdd1243dSDimitry Andric       // the symbol context and the breakpoint would fail to get set even though
340bdd1243dSDimitry Andric       // we have a valid line table entry in this compile unit. The address
341bdd1243dSDimitry Andric       // lookup can also end up finding another function in another compiler
342bdd1243dSDimitry Andric       // unit if the DWARF has overlappging address ranges. So if we end up with
343bdd1243dSDimitry Andric       // no compile unit or a different one after the above function call,
344bdd1243dSDimitry Andric       // revert back to the same results as if resolve_scope was set exactly to
345bdd1243dSDimitry Andric       // eSymbolContextLineEntry.
346bdd1243dSDimitry Andric       if (resolved_sc.comp_unit == this) {
347bdd1243dSDimitry Andric         sc_list.Append(resolved_sc);
348bdd1243dSDimitry Andric       } else {
349bdd1243dSDimitry Andric         if (resolved_sc.comp_unit == nullptr && resolved_sc.module_sp) {
350bdd1243dSDimitry Andric           // Only report an error if we don't map back to any compile unit. With
351bdd1243dSDimitry Andric           // link time optimizations, the debug info might have many compile
352bdd1243dSDimitry Andric           // units that have the same address range due to function outlining
353bdd1243dSDimitry Andric           // or other link time optimizations. If the compile unit is NULL, then
354bdd1243dSDimitry Andric           // address resolving is completely failing and more deserving of an
355bdd1243dSDimitry Andric           // error message the user can see.
356bdd1243dSDimitry Andric           resolved_sc.module_sp->ReportError(
357bdd1243dSDimitry Andric               "unable to resolve a line table file address {0:x16} back "
358bdd1243dSDimitry Andric               "to a compile unit, please file a bug and attach the address "
359bdd1243dSDimitry Andric               "and file.",
360bdd1243dSDimitry Andric               line_entry.range.GetBaseAddress().GetFileAddress());
361bdd1243dSDimitry Andric         }
362bdd1243dSDimitry Andric         sc_list.Append(sc);
363bdd1243dSDimitry Andric       }
3640b57cec5SDimitry Andric     }
3650b57cec5SDimitry Andric 
366480093f4SDimitry Andric     if (num_file_indexes == 1)
3670b57cec5SDimitry Andric       line_idx = line_table->FindLineEntryIndexByFileIndex(
368fe6060f1SDimitry Andric           line_idx + 1, file_indexes.front(), found_entry, &line_entry);
369480093f4SDimitry Andric     else
3700b57cec5SDimitry Andric       line_idx = line_table->FindLineEntryIndexByFileIndex(
371fe6060f1SDimitry Andric           line_idx + 1, file_indexes, found_entry, &line_entry);
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric bool CompileUnit::GetIsOptimized() {
3760b57cec5SDimitry Andric   if (m_is_optimized == eLazyBoolCalculate) {
3770b57cec5SDimitry Andric     m_is_optimized = eLazyBoolNo;
3789dba64beSDimitry Andric     if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {
3799dba64beSDimitry Andric       if (symfile->ParseIsOptimized(*this))
3800b57cec5SDimitry Andric         m_is_optimized = eLazyBoolYes;
3810b57cec5SDimitry Andric     }
3820b57cec5SDimitry Andric   }
3830b57cec5SDimitry Andric   return m_is_optimized;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric void CompileUnit::SetVariableList(VariableListSP &variables) {
3870b57cec5SDimitry Andric   m_variables = variables;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric const std::vector<SourceModule> &CompileUnit::GetImportedModules() {
3910b57cec5SDimitry Andric   if (m_imported_modules.empty() &&
3920b57cec5SDimitry Andric       m_flags.IsClear(flagsParsedImportedModules)) {
3930b57cec5SDimitry Andric     m_flags.Set(flagsParsedImportedModules);
3949dba64beSDimitry Andric     if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {
3950b57cec5SDimitry Andric       SymbolContext sc;
3960b57cec5SDimitry Andric       CalculateSymbolContext(&sc);
3979dba64beSDimitry Andric       symfile->ParseImportedModules(sc, m_imported_modules);
3980b57cec5SDimitry Andric     }
3990b57cec5SDimitry Andric   }
4000b57cec5SDimitry Andric   return m_imported_modules;
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
403480093f4SDimitry Andric bool CompileUnit::ForEachExternalModule(
404480093f4SDimitry Andric     llvm::DenseSet<SymbolFile *> &visited_symbol_files,
405480093f4SDimitry Andric     llvm::function_ref<bool(Module &)> lambda) {
4069dba64beSDimitry Andric   if (SymbolFile *symfile = GetModule()->GetSymbolFile())
407480093f4SDimitry Andric     return symfile->ForEachExternalModule(*this, visited_symbol_files, lambda);
408480093f4SDimitry Andric   return false;
4099dba64beSDimitry Andric }
4109dba64beSDimitry Andric 
411*cdc20ff6SDimitry Andric const SupportFileList &CompileUnit::GetSupportFiles() {
4120b57cec5SDimitry Andric   if (m_support_files.GetSize() == 0) {
4130b57cec5SDimitry Andric     if (m_flags.IsClear(flagsParsedSupportFiles)) {
4140b57cec5SDimitry Andric       m_flags.Set(flagsParsedSupportFiles);
4159dba64beSDimitry Andric       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
4169dba64beSDimitry Andric         symfile->ParseSupportFiles(*this, m_support_files);
4170b57cec5SDimitry Andric     }
4180b57cec5SDimitry Andric   }
4190b57cec5SDimitry Andric   return m_support_files;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric void *CompileUnit::GetUserData() const { return m_user_data; }
423