180814287SRaphael Isemann //===-- SymbolFilePDB.cpp -------------------------------------------------===//
274e08ca0SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
674e08ca0SZachary Turner //
774e08ca0SZachary Turner //===----------------------------------------------------------------------===//
874e08ca0SZachary Turner 
974e08ca0SZachary Turner #include "SymbolFilePDB.h"
1074e08ca0SZachary Turner 
11c1e530eeSAleksandr Urakov #include "PDBASTParser.h"
12c1e530eeSAleksandr Urakov #include "PDBLocationToDWARFExpression.h"
13c1e530eeSAleksandr Urakov 
1442dff790SZachary Turner #include "clang/Lex/Lexer.h"
1542dff790SZachary Turner 
168be30215SAlex Langford #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
1774e08ca0SZachary Turner #include "lldb/Core/Module.h"
1874e08ca0SZachary Turner #include "lldb/Core/PluginManager.h"
1974e08ca0SZachary Turner #include "lldb/Symbol/CompileUnit.h"
2074e08ca0SZachary Turner #include "lldb/Symbol/LineTable.h"
2174e08ca0SZachary Turner #include "lldb/Symbol/ObjectFile.h"
2274e08ca0SZachary Turner #include "lldb/Symbol/SymbolContext.h"
2310a02577SAaron Smith #include "lldb/Symbol/SymbolVendor.h"
24ec40f818SAaron Smith #include "lldb/Symbol/TypeList.h"
25308e39caSAaron Smith #include "lldb/Symbol/TypeMap.h"
26cab0d23fSAaron Smith #include "lldb/Symbol/Variable.h"
27c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
280e252e38SAlex Langford #include "lldb/Utility/Log.h"
2986e9434dSAaron Smith #include "lldb/Utility/RegularExpression.h"
3074e08ca0SZachary Turner 
31eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
32b8d8c62bSPavel Labath #include "llvm/DebugInfo/PDB/GenericError.h"
331f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBDataStream.h"
3474e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
3574e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
36308e39caSAaron Smith #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
3774e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
381f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBTable.h"
3974e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbol.h"
407ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
4174e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
4274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
431f8552abSAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
4474e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
4574e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
4674e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
4774e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
487ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
49eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
5042dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
51eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
5242dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
5342dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
5442dff790SZachary Turner 
55672d2c12SJonas Devlieghere #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
56c1e530eeSAleksandr Urakov #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
57307f5ae8SZachary Turner #include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
5842dff790SZachary Turner 
59908c1154SNico Weber #if defined(_WIN32)
603031fa88SMehdi Chinoune #include "llvm/Config/llvm-config.h"
61908c1154SNico Weber #endif
62908c1154SNico Weber 
6310a02577SAaron Smith using namespace lldb;
6474e08ca0SZachary Turner using namespace lldb_private;
6554fd7ff6SZachary Turner using namespace llvm::pdb;
6674e08ca0SZachary Turner 
67bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(SymbolFilePDB)
68fbb4d1e4SJonas Devlieghere 
697d71dd92SAdrian Prantl char SymbolFilePDB::ID;
707d71dd92SAdrian Prantl 
71b9c1b51eSKate Stone namespace {
TranslateLanguage(PDB_Lang lang)72b9c1b51eSKate Stone lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
73b9c1b51eSKate Stone   switch (lang) {
7454fd7ff6SZachary Turner   case PDB_Lang::Cpp:
7574e08ca0SZachary Turner     return lldb::LanguageType::eLanguageTypeC_plus_plus;
7654fd7ff6SZachary Turner   case PDB_Lang::C:
7774e08ca0SZachary Turner     return lldb::LanguageType::eLanguageTypeC;
780561be6cSNathan Lanza   case PDB_Lang::Swift:
790561be6cSNathan Lanza     return lldb::LanguageType::eLanguageTypeSwift;
801ccfef14SArlo Siemsen   case PDB_Lang::Rust:
811ccfef14SArlo Siemsen     return lldb::LanguageType::eLanguageTypeRust;
8274e08ca0SZachary Turner   default:
8374e08ca0SZachary Turner     return lldb::LanguageType::eLanguageTypeUnknown;
8474e08ca0SZachary Turner   }
8574e08ca0SZachary Turner }
867e8c7beaSZachary Turner 
ShouldAddLine(uint32_t requested_line,uint32_t actual_line,uint32_t addr_length)87b9c1b51eSKate Stone bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
88b9c1b51eSKate Stone                    uint32_t addr_length) {
89b9c1b51eSKate Stone   return ((requested_line == 0 || actual_line == requested_line) &&
90b9c1b51eSKate Stone           addr_length > 0);
917e8c7beaSZachary Turner }
92c8316ed2SAaron Smith } // namespace
9374e08ca0SZachary Turner 
ShouldUseNativeReader()94307f5ae8SZachary Turner static bool ShouldUseNativeReader() {
9571970b72SGreg Clayton #if defined(_WIN32)
96908c1154SNico Weber #if LLVM_ENABLE_DIA_SDK
97307f5ae8SZachary Turner   llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
98908c1154SNico Weber   if (!use_native.equals_insensitive("on") &&
99908c1154SNico Weber       !use_native.equals_insensitive("yes") &&
100908c1154SNico Weber       !use_native.equals_insensitive("1") &&
101908c1154SNico Weber       !use_native.equals_insensitive("true"))
102908c1154SNico Weber     return false;
10371970b72SGreg Clayton #endif
104908c1154SNico Weber #endif
105908c1154SNico Weber   return true;
106307f5ae8SZachary Turner }
107307f5ae8SZachary Turner 
Initialize()108b9c1b51eSKate Stone void SymbolFilePDB::Initialize() {
109307f5ae8SZachary Turner   if (ShouldUseNativeReader()) {
110307f5ae8SZachary Turner     npdb::SymbolFileNativePDB::Initialize();
111307f5ae8SZachary Turner   } else {
112b9c1b51eSKate Stone     PluginManager::RegisterPlugin(GetPluginNameStatic(),
113b9c1b51eSKate Stone                                   GetPluginDescriptionStatic(), CreateInstance,
11474e08ca0SZachary Turner                                   DebuggerInitialize);
11574e08ca0SZachary Turner   }
116307f5ae8SZachary Turner }
11774e08ca0SZachary Turner 
Terminate()118b9c1b51eSKate Stone void SymbolFilePDB::Terminate() {
119307f5ae8SZachary Turner   if (ShouldUseNativeReader()) {
120307f5ae8SZachary Turner     npdb::SymbolFileNativePDB::Terminate();
121307f5ae8SZachary Turner   } else {
12274e08ca0SZachary Turner     PluginManager::UnregisterPlugin(CreateInstance);
12374e08ca0SZachary Turner   }
124307f5ae8SZachary Turner }
12574e08ca0SZachary Turner 
DebuggerInitialize(lldb_private::Debugger & debugger)126b9c1b51eSKate Stone void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
12774e08ca0SZachary Turner 
GetPluginDescriptionStatic()12849481b53SPavel Labath llvm::StringRef SymbolFilePDB::GetPluginDescriptionStatic() {
12974e08ca0SZachary Turner   return "Microsoft PDB debug symbol file reader.";
13074e08ca0SZachary Turner }
13174e08ca0SZachary Turner 
13274e08ca0SZachary Turner lldb_private::SymbolFile *
CreateInstance(ObjectFileSP objfile_sp)133d2deeb44SPavel Labath SymbolFilePDB::CreateInstance(ObjectFileSP objfile_sp) {
134d2deeb44SPavel Labath   return new SymbolFilePDB(std::move(objfile_sp));
13574e08ca0SZachary Turner }
13674e08ca0SZachary Turner 
SymbolFilePDB(lldb::ObjectFileSP objfile_sp)137d2deeb44SPavel Labath SymbolFilePDB::SymbolFilePDB(lldb::ObjectFileSP objfile_sp)
1385cbf516cSJeffrey Tan     : SymbolFileCommon(std::move(objfile_sp)), m_session_up(), m_global_scope_up() {}
13974e08ca0SZachary Turner 
140fd2433e1SJonas Devlieghere SymbolFilePDB::~SymbolFilePDB() = default;
14174e08ca0SZachary Turner 
CalculateAbilities()142b9c1b51eSKate Stone uint32_t SymbolFilePDB::CalculateAbilities() {
1431f8552abSAaron Smith   uint32_t abilities = 0;
144d2deeb44SPavel Labath   if (!m_objfile_sp)
1451f8552abSAaron Smith     return 0;
1461f8552abSAaron Smith 
147b9c1b51eSKate Stone   if (!m_session_up) {
14874e08ca0SZachary Turner     // Lazily load and match the PDB file, but only do this once.
149d2deeb44SPavel Labath     std::string exePath = m_objfile_sp->GetFileSpec().GetPath();
150b9c1b51eSKate Stone     auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
151b9c1b51eSKate Stone                                 m_session_up);
152b9c1b51eSKate Stone     if (error) {
1534fd6a960SZachary Turner       llvm::consumeError(std::move(error));
154d2deeb44SPavel Labath       auto module_sp = m_objfile_sp->GetModule();
1551f8552abSAaron Smith       if (!module_sp)
1561f8552abSAaron Smith         return 0;
1571f8552abSAaron Smith       // See if any symbol file is specified through `--symfile` option.
1581f8552abSAaron Smith       FileSpec symfile = module_sp->GetSymbolFileFileSpec();
1591f8552abSAaron Smith       if (!symfile)
1601f8552abSAaron Smith         return 0;
1611f8552abSAaron Smith       error = loadDataForPDB(PDB_ReaderType::DIA,
162c8316ed2SAaron Smith                              llvm::StringRef(symfile.GetPath()), m_session_up);
1631f8552abSAaron Smith       if (error) {
1641f8552abSAaron Smith         llvm::consumeError(std::move(error));
16574e08ca0SZachary Turner         return 0;
16674e08ca0SZachary Turner       }
167b8d8c62bSPavel Labath     }
1681f8552abSAaron Smith   }
169d5a925f4SAaron Smith   if (!m_session_up)
1701f8552abSAaron Smith     return 0;
1711f8552abSAaron Smith 
1721f8552abSAaron Smith   auto enum_tables_up = m_session_up->getEnumTables();
1731f8552abSAaron Smith   if (!enum_tables_up)
1741f8552abSAaron Smith     return 0;
1751f8552abSAaron Smith   while (auto table_up = enum_tables_up->getNext()) {
1761f8552abSAaron Smith     if (table_up->getItemCount() == 0)
1771f8552abSAaron Smith       continue;
1781f8552abSAaron Smith     auto type = table_up->getTableType();
1791f8552abSAaron Smith     switch (type) {
1801f8552abSAaron Smith     case PDB_TableType::Symbols:
1811f8552abSAaron Smith       // This table represents a store of symbols with types listed in
1821f8552abSAaron Smith       // PDBSym_Type
183c8316ed2SAaron Smith       abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
184c8316ed2SAaron Smith                     LocalVariables | VariableTypes);
1851f8552abSAaron Smith       break;
1861f8552abSAaron Smith     case PDB_TableType::LineNumbers:
1871f8552abSAaron Smith       abilities |= LineTables;
1881f8552abSAaron Smith       break;
189c8316ed2SAaron Smith     default:
190c8316ed2SAaron Smith       break;
1911f8552abSAaron Smith     }
1921f8552abSAaron Smith   }
1931f8552abSAaron Smith   return abilities;
19474e08ca0SZachary Turner }
19574e08ca0SZachary Turner 
InitializeObject()196b9c1b51eSKate Stone void SymbolFilePDB::InitializeObject() {
197d2deeb44SPavel Labath   lldb::addr_t obj_load_address =
198d2deeb44SPavel Labath       m_objfile_sp->GetBaseAddress().GetFileAddress();
199c8316ed2SAaron Smith   lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
20074e08ca0SZachary Turner   m_session_up->setLoadAddress(obj_load_address);
20110a02577SAaron Smith   if (!m_global_scope_up)
20210a02577SAaron Smith     m_global_scope_up = m_session_up->getGlobalScope();
20310a02577SAaron Smith   lldbassert(m_global_scope_up.get());
20474e08ca0SZachary Turner }
20574e08ca0SZachary Turner 
CalculateNumCompileUnits()206e0119909SPavel Labath uint32_t SymbolFilePDB::CalculateNumCompileUnits() {
20710a02577SAaron Smith   auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
20810a02577SAaron Smith   if (!compilands)
20910a02577SAaron Smith     return 0;
21010a02577SAaron Smith 
21110a02577SAaron Smith   // The linker could link *.dll (compiland language = LINK), or import
21205097246SAdrian Prantl   // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
21305097246SAdrian Prantl   // found as a child of the global scope (PDB executable). Usually, such
21405097246SAdrian Prantl   // compilands contain `thunk` symbols in which we are not interested for
21505097246SAdrian Prantl   // now. However we still count them in the compiland list. If we perform
21605097246SAdrian Prantl   // any compiland related activity, like finding symbols through
21705097246SAdrian Prantl   // llvm::pdb::IPDBSession methods, such compilands will all be searched
21805097246SAdrian Prantl   // automatically no matter whether we include them or not.
219e0119909SPavel Labath   uint32_t compile_unit_count = compilands->getChildCount();
22074e08ca0SZachary Turner 
221b9c1b51eSKate Stone   // The linker can inject an additional "dummy" compilation unit into the
2229d0eb996SAdrian McCarthy   // PDB. Ignore this special compile unit for our purposes, if it is there.
2239d0eb996SAdrian McCarthy   // It is always the last one.
224e0119909SPavel Labath   auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1);
22510a02577SAaron Smith   lldbassert(last_compiland_up.get());
22610a02577SAaron Smith   std::string name = last_compiland_up->getName();
22774e08ca0SZachary Turner   if (name == "* Linker *")
228e0119909SPavel Labath     --compile_unit_count;
229e0119909SPavel Labath   return compile_unit_count;
23074e08ca0SZachary Turner }
23174e08ca0SZachary Turner 
GetCompileUnitIndex(const llvm::pdb::PDBSymbolCompiland & pdb_compiland,uint32_t & index)23210a02577SAaron Smith void SymbolFilePDB::GetCompileUnitIndex(
233c8316ed2SAaron Smith     const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
23410a02577SAaron Smith   auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
23510a02577SAaron Smith   if (!results_up)
23610a02577SAaron Smith     return;
237e664b5dcSAaron Smith   auto uid = pdb_compiland.getSymIndexId();
238fbdf0b93SRaphael Isemann   for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
23910a02577SAaron Smith     auto compiland_up = results_up->getChildAtIndex(cu_idx);
24010a02577SAaron Smith     if (!compiland_up)
24110a02577SAaron Smith       continue;
24210a02577SAaron Smith     if (compiland_up->getSymIndexId() == uid) {
24310a02577SAaron Smith       index = cu_idx;
24410a02577SAaron Smith       return;
24510a02577SAaron Smith     }
24610a02577SAaron Smith   }
24710a02577SAaron Smith   index = UINT32_MAX;
24810a02577SAaron Smith }
24910a02577SAaron Smith 
25010a02577SAaron Smith std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
GetPDBCompilandByUID(uint32_t uid)25110a02577SAaron Smith SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
25210a02577SAaron Smith   return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
25310a02577SAaron Smith }
25410a02577SAaron Smith 
ParseCompileUnitAtIndex(uint32_t index)255b9c1b51eSKate Stone lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
25610a02577SAaron Smith   if (index >= GetNumCompileUnits())
25710a02577SAaron Smith     return CompUnitSP();
25874e08ca0SZachary Turner 
25910a02577SAaron Smith   // Assuming we always retrieve same compilands listed in same order through
26010a02577SAaron Smith   // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
26110a02577SAaron Smith   // compile unit makes no sense.
26210a02577SAaron Smith   auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
26310a02577SAaron Smith   if (!results)
26410a02577SAaron Smith     return CompUnitSP();
26510a02577SAaron Smith   auto compiland_up = results->getChildAtIndex(index);
26610a02577SAaron Smith   if (!compiland_up)
26710a02577SAaron Smith     return CompUnitSP();
26810a02577SAaron Smith   return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
26974e08ca0SZachary Turner }
27074e08ca0SZachary Turner 
ParseLanguage(CompileUnit & comp_unit)271863f8c18SZachary Turner lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) {
272656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
273863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
27410a02577SAaron Smith   if (!compiland_up)
27574e08ca0SZachary Turner     return lldb::eLanguageTypeUnknown;
27610a02577SAaron Smith   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
27774e08ca0SZachary Turner   if (!details)
27874e08ca0SZachary Turner     return lldb::eLanguageTypeUnknown;
27974e08ca0SZachary Turner   return TranslateLanguage(details->getLanguage());
28074e08ca0SZachary Turner }
28174e08ca0SZachary Turner 
282863f8c18SZachary Turner lldb_private::Function *
ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc & pdb_func,CompileUnit & comp_unit)283863f8c18SZachary Turner SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func,
284863f8c18SZachary Turner                                                   CompileUnit &comp_unit) {
285863f8c18SZachary Turner   if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId()))
286a5235af9SAleksandr Urakov     return result.get();
287a5235af9SAleksandr Urakov 
288e664b5dcSAaron Smith   auto file_vm_addr = pdb_func.getVirtualAddress();
289308e39caSAaron Smith   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
2907ac1c780SAaron Smith     return nullptr;
2917ac1c780SAaron Smith 
292e664b5dcSAaron Smith   auto func_length = pdb_func.getLength();
293c8316ed2SAaron Smith   AddressRange func_range =
294863f8c18SZachary Turner       AddressRange(file_vm_addr, func_length,
295863f8c18SZachary Turner                    GetObjectFile()->GetModule()->GetSectionList());
2967ac1c780SAaron Smith   if (!func_range.GetBaseAddress().IsValid())
2977ac1c780SAaron Smith     return nullptr;
2987ac1c780SAaron Smith 
299e664b5dcSAaron Smith   lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
3007ac1c780SAaron Smith   if (!func_type)
3017ac1c780SAaron Smith     return nullptr;
3027ac1c780SAaron Smith 
303e664b5dcSAaron Smith   user_id_t func_type_uid = pdb_func.getSignatureId();
304f76fe682SAaron Smith 
3057ac1c780SAaron Smith   Mangled mangled = GetMangledForPDBFunc(pdb_func);
3067ac1c780SAaron Smith 
307c8316ed2SAaron Smith   FunctionSP func_sp =
308863f8c18SZachary Turner       std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(),
309c8316ed2SAaron Smith                                  func_type_uid, mangled, func_type, func_range);
3107ac1c780SAaron Smith 
311863f8c18SZachary Turner   comp_unit.AddFunction(func_sp);
312c68925abSZachary Turner 
313d0050d1bSNathan Lanza   LanguageType lang = ParseLanguage(comp_unit);
3140e252e38SAlex Langford   auto type_system_or_err = GetTypeSystemForLanguage(lang);
3150e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
316a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
317a007a6d8SPavel Labath                    "Unable to parse PDBFunc");
318c68925abSZachary Turner     return nullptr;
3190e252e38SAlex Langford   }
3200e252e38SAlex Langford 
3216e3b0cc2SRaphael Isemann   TypeSystemClang *clang_type_system =
3226e3b0cc2SRaphael Isemann     llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
323c68925abSZachary Turner   if (!clang_type_system)
324c68925abSZachary Turner     return nullptr;
325c68925abSZachary Turner   clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func);
326c68925abSZachary Turner 
3277ac1c780SAaron Smith   return func_sp.get();
3287ac1c780SAaron Smith }
3297ac1c780SAaron Smith 
ParseFunctions(CompileUnit & comp_unit)330863f8c18SZachary Turner size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) {
331656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
3327ac1c780SAaron Smith   size_t func_added = 0;
333863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
3347ac1c780SAaron Smith   if (!compiland_up)
3357ac1c780SAaron Smith     return 0;
3367ac1c780SAaron Smith   auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
3377ac1c780SAaron Smith   if (!results_up)
3387ac1c780SAaron Smith     return 0;
3397ac1c780SAaron Smith   while (auto pdb_func_up = results_up->getNext()) {
340863f8c18SZachary Turner     auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId());
3417ac1c780SAaron Smith     if (!func_sp) {
342863f8c18SZachary Turner       if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit))
3437ac1c780SAaron Smith         ++func_added;
3447ac1c780SAaron Smith     }
3457ac1c780SAaron Smith   }
3467ac1c780SAaron Smith   return func_added;
34774e08ca0SZachary Turner }
34874e08ca0SZachary Turner 
ParseLineTable(CompileUnit & comp_unit)349863f8c18SZachary Turner bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) {
350656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
351863f8c18SZachary Turner   if (comp_unit.GetLineTable())
35210a02577SAaron Smith     return true;
353863f8c18SZachary Turner   return ParseCompileUnitLineTable(comp_unit, 0);
35474e08ca0SZachary Turner }
35574e08ca0SZachary Turner 
ParseDebugMacros(CompileUnit & comp_unit)356863f8c18SZachary Turner bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) {
35774e08ca0SZachary Turner   // PDB doesn't contain information about macros
35874e08ca0SZachary Turner   return false;
35974e08ca0SZachary Turner }
36074e08ca0SZachary Turner 
ParseSupportFiles(CompileUnit & comp_unit,lldb_private::FileSpecList & support_files)361863f8c18SZachary Turner bool SymbolFilePDB::ParseSupportFiles(
362863f8c18SZachary Turner     CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) {
36374e08ca0SZachary Turner 
364b9c1b51eSKate Stone   // In theory this is unnecessary work for us, because all of this information
3659d0eb996SAdrian McCarthy   // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
3669d0eb996SAdrian McCarthy   // second time seems like a waste.  Unfortunately, there's no good way around
3679d0eb996SAdrian McCarthy   // this short of a moderate refactor since SymbolVendor depends on being able
3689d0eb996SAdrian McCarthy   // to cache this list.
369656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
370863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
37110a02577SAaron Smith   if (!compiland_up)
37274e08ca0SZachary Turner     return false;
37310a02577SAaron Smith   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
37474e08ca0SZachary Turner   if (!files || files->getChildCount() == 0)
37574e08ca0SZachary Turner     return false;
37674e08ca0SZachary Turner 
377b9c1b51eSKate Stone   while (auto file = files->getNext()) {
3788f3be7a3SJonas Devlieghere     FileSpec spec(file->getFileName(), FileSpec::Style::windows);
37910a02577SAaron Smith     support_files.AppendIfUnique(spec);
38074e08ca0SZachary Turner   }
3819ea80d25SPavel Labath 
38274e08ca0SZachary Turner   return true;
38374e08ca0SZachary Turner }
38474e08ca0SZachary Turner 
ParseImportedModules(const lldb_private::SymbolContext & sc,std::vector<SourceModule> & imported_modules)385b9c1b51eSKate Stone bool SymbolFilePDB::ParseImportedModules(
386b9c1b51eSKate Stone     const lldb_private::SymbolContext &sc,
3870f30a3b6SAdrian Prantl     std::vector<SourceModule> &imported_modules) {
38874e08ca0SZachary Turner   // PDB does not yet support module debug info
38974e08ca0SZachary Turner   return false;
39074e08ca0SZachary Turner }
39174e08ca0SZachary Turner 
ParseFunctionBlocksForPDBSymbol(uint64_t func_file_vm_addr,const llvm::pdb::PDBSymbol * pdb_symbol,lldb_private::Block * parent_block,bool is_top_parent)392c8316ed2SAaron Smith static size_t ParseFunctionBlocksForPDBSymbol(
393ffc1b8fdSZachary Turner     uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol,
394ffc1b8fdSZachary Turner     lldb_private::Block *parent_block, bool is_top_parent) {
3957ac1c780SAaron Smith   assert(pdb_symbol && parent_block);
3967ac1c780SAaron Smith 
3977ac1c780SAaron Smith   size_t num_added = 0;
3987ac1c780SAaron Smith   switch (pdb_symbol->getSymTag()) {
3997ac1c780SAaron Smith   case PDB_SymType::Block:
4007ac1c780SAaron Smith   case PDB_SymType::Function: {
4017ac1c780SAaron Smith     Block *block = nullptr;
4027ac1c780SAaron Smith     auto &raw_sym = pdb_symbol->getRawSymbol();
4037ac1c780SAaron Smith     if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
4047ac1c780SAaron Smith       if (pdb_func->hasNoInlineAttribute())
4057ac1c780SAaron Smith         break;
4067ac1c780SAaron Smith       if (is_top_parent)
4077ac1c780SAaron Smith         block = parent_block;
4087ac1c780SAaron Smith       else
4097ac1c780SAaron Smith         break;
41062e48ed1SKazu Hirata     } else if (llvm::isa<PDBSymbolBlock>(pdb_symbol)) {
4117ac1c780SAaron Smith       auto uid = pdb_symbol->getSymIndexId();
4127ac1c780SAaron Smith       if (parent_block->FindBlockByID(uid))
4137ac1c780SAaron Smith         break;
4147ac1c780SAaron Smith       if (raw_sym.getVirtualAddress() < func_file_vm_addr)
4157ac1c780SAaron Smith         break;
4167ac1c780SAaron Smith 
4177ac1c780SAaron Smith       auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
4187ac1c780SAaron Smith       parent_block->AddChild(block_sp);
4197ac1c780SAaron Smith       block = block_sp.get();
4207ac1c780SAaron Smith     } else
4217ac1c780SAaron Smith       llvm_unreachable("Unexpected PDB symbol!");
4227ac1c780SAaron Smith 
423c8316ed2SAaron Smith     block->AddRange(Block::Range(
424c8316ed2SAaron Smith         raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
4257ac1c780SAaron Smith     block->FinalizeRanges();
4267ac1c780SAaron Smith     ++num_added;
4277ac1c780SAaron Smith 
4287ac1c780SAaron Smith     auto results_up = pdb_symbol->findAllChildren();
4297ac1c780SAaron Smith     if (!results_up)
4307ac1c780SAaron Smith       break;
4317ac1c780SAaron Smith     while (auto symbol_up = results_up->getNext()) {
432c8316ed2SAaron Smith       num_added += ParseFunctionBlocksForPDBSymbol(
433ffc1b8fdSZachary Turner           func_file_vm_addr, symbol_up.get(), block, false);
4347ac1c780SAaron Smith     }
4357ac1c780SAaron Smith   } break;
436c8316ed2SAaron Smith   default:
437c8316ed2SAaron Smith     break;
4387ac1c780SAaron Smith   }
4397ac1c780SAaron Smith   return num_added;
4407ac1c780SAaron Smith }
4417ac1c780SAaron Smith 
ParseBlocksRecursive(Function & func)442ffc1b8fdSZachary Turner size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) {
443656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
4447ac1c780SAaron Smith   size_t num_added = 0;
445ffc1b8fdSZachary Turner   auto uid = func.GetID();
4467ac1c780SAaron Smith   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
4477ac1c780SAaron Smith   if (!pdb_func_up)
4487ac1c780SAaron Smith     return 0;
449ffc1b8fdSZachary Turner   Block &parent_block = func.GetBlock(false);
450ffc1b8fdSZachary Turner   num_added = ParseFunctionBlocksForPDBSymbol(
451ffc1b8fdSZachary Turner       pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true);
4527ac1c780SAaron Smith   return num_added;
453b9c1b51eSKate Stone }
454b9c1b51eSKate Stone 
ParseTypes(CompileUnit & comp_unit)455863f8c18SZachary Turner size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) {
456656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
45766b84079SAaron Smith 
45866b84079SAaron Smith   size_t num_added = 0;
459ac0d41c7SZachary Turner   auto compiland = GetPDBCompilandByUID(comp_unit.GetID());
46066b84079SAaron Smith   if (!compiland)
46166b84079SAaron Smith     return 0;
46266b84079SAaron Smith 
46366b84079SAaron Smith   auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
46466b84079SAaron Smith     std::unique_ptr<IPDBEnumSymbols> results;
46566b84079SAaron Smith     PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
46666b84079SAaron Smith                                     PDB_SymType::UDT};
46766b84079SAaron Smith     for (auto tag : tags_to_search) {
46866b84079SAaron Smith       results = raw_sym.findAllChildren(tag);
46966b84079SAaron Smith       if (!results || results->getChildCount() == 0)
47066b84079SAaron Smith         continue;
47166b84079SAaron Smith       while (auto symbol = results->getNext()) {
47266b84079SAaron Smith         switch (symbol->getSymTag()) {
473ec40f818SAaron Smith         case PDB_SymType::Enum:
474ec40f818SAaron Smith         case PDB_SymType::UDT:
475ec40f818SAaron Smith         case PDB_SymType::Typedef:
476ec40f818SAaron Smith           break;
477ec40f818SAaron Smith         default:
478ec40f818SAaron Smith           continue;
479ec40f818SAaron Smith         }
480ec40f818SAaron Smith 
481ec40f818SAaron Smith         // This should cause the type to get cached and stored in the `m_types`
482ec40f818SAaron Smith         // lookup.
4837d2a74fcSAleksandr Urakov         if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
4847d2a74fcSAleksandr Urakov           // Resolve the type completely to avoid a completion
4857d2a74fcSAleksandr Urakov           // (and so a list change, which causes an iterators invalidation)
4867d2a74fcSAleksandr Urakov           // during a TypeList dumping
4877d2a74fcSAleksandr Urakov           type->GetFullCompilerType();
488ec40f818SAaron Smith           ++num_added;
489ec40f818SAaron Smith         }
49066b84079SAaron Smith       }
4917d2a74fcSAleksandr Urakov     }
49266b84079SAaron Smith   };
49366b84079SAaron Smith 
49466b84079SAaron Smith   ParseTypesByTagFn(*compiland);
49566b84079SAaron Smith 
49666b84079SAaron Smith   // Also parse global types particularly coming from this compiland.
49705097246SAdrian Prantl   // Unfortunately, PDB has no compiland information for each global type. We
49805097246SAdrian Prantl   // have to parse them all. But ensure we only do this once.
49966b84079SAaron Smith   static bool parse_all_global_types = false;
50066b84079SAaron Smith   if (!parse_all_global_types) {
50166b84079SAaron Smith     ParseTypesByTagFn(*m_global_scope_up);
50266b84079SAaron Smith     parse_all_global_types = true;
50366b84079SAaron Smith   }
504ec40f818SAaron Smith   return num_added;
50574e08ca0SZachary Turner }
50674e08ca0SZachary Turner 
50774e08ca0SZachary Turner size_t
ParseVariablesForContext(const lldb_private::SymbolContext & sc)508b9c1b51eSKate Stone SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
509656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
510cab0d23fSAaron Smith   if (!sc.comp_unit)
511cab0d23fSAaron Smith     return 0;
512cab0d23fSAaron Smith 
513cab0d23fSAaron Smith   size_t num_added = 0;
514cab0d23fSAaron Smith   if (sc.function) {
515cab0d23fSAaron Smith     auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
516cab0d23fSAaron Smith         sc.function->GetID());
517cab0d23fSAaron Smith     if (!pdb_func)
518cab0d23fSAaron Smith       return 0;
519cab0d23fSAaron Smith 
520cab0d23fSAaron Smith     num_added += ParseVariables(sc, *pdb_func);
521cab0d23fSAaron Smith     sc.function->GetBlock(false).SetDidParseVariables(true, true);
522cab0d23fSAaron Smith   } else if (sc.comp_unit) {
523cab0d23fSAaron Smith     auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
524cab0d23fSAaron Smith     if (!compiland)
525cab0d23fSAaron Smith       return 0;
526cab0d23fSAaron Smith 
527cab0d23fSAaron Smith     if (sc.comp_unit->GetVariableList(false))
528cab0d23fSAaron Smith       return 0;
529cab0d23fSAaron Smith 
530cab0d23fSAaron Smith     auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
531cab0d23fSAaron Smith     if (results && results->getChildCount()) {
532cab0d23fSAaron Smith       while (auto result = results->getNext()) {
533356aa4a9SAleksandr Urakov         auto cu_id = GetCompilandId(*result);
534cab0d23fSAaron Smith         // FIXME: We are not able to determine variable's compile unit.
535cab0d23fSAaron Smith         if (cu_id == 0)
536cab0d23fSAaron Smith           continue;
537cab0d23fSAaron Smith 
538cab0d23fSAaron Smith         if (cu_id == sc.comp_unit->GetID())
539cab0d23fSAaron Smith           num_added += ParseVariables(sc, *result);
540cab0d23fSAaron Smith       }
541cab0d23fSAaron Smith     }
542cab0d23fSAaron Smith 
543cab0d23fSAaron Smith     // FIXME: A `file static` or `global constant` variable appears both in
544cab0d23fSAaron Smith     // compiland's children and global scope's children with unexpectedly
545cab0d23fSAaron Smith     // different symbol's Id making it ambiguous.
546cab0d23fSAaron Smith 
547cab0d23fSAaron Smith     // FIXME: 'local constant', for example, const char var[] = "abc", declared
548cab0d23fSAaron Smith     // in a function scope, can't be found in PDB.
549cab0d23fSAaron Smith 
550cab0d23fSAaron Smith     // Parse variables in this compiland.
551cab0d23fSAaron Smith     num_added += ParseVariables(sc, *compiland);
552cab0d23fSAaron Smith   }
553cab0d23fSAaron Smith 
554cab0d23fSAaron Smith   return num_added;
55574e08ca0SZachary Turner }
55674e08ca0SZachary Turner 
ResolveTypeUID(lldb::user_id_t type_uid)557b9c1b51eSKate Stone lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
558656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
55942dff790SZachary Turner   auto find_result = m_types.find(type_uid);
56042dff790SZachary Turner   if (find_result != m_types.end())
56142dff790SZachary Turner     return find_result->second.get();
56242dff790SZachary Turner 
5630e252e38SAlex Langford   auto type_system_or_err =
564b9c1b51eSKate Stone       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
5650e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
566a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
567a007a6d8SPavel Labath                    "Unable to ResolveTypeUID");
5680e252e38SAlex Langford     return nullptr;
5690e252e38SAlex Langford   }
5700e252e38SAlex Langford 
5716e3b0cc2SRaphael Isemann   TypeSystemClang *clang_type_system =
5726e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
57342dff790SZachary Turner   if (!clang_type_system)
57474e08ca0SZachary Turner     return nullptr;
575709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_type_system->GetPDBParser();
57642dff790SZachary Turner   if (!pdb)
57742dff790SZachary Turner     return nullptr;
57842dff790SZachary Turner 
57942dff790SZachary Turner   auto pdb_type = m_session_up->getSymbolById(type_uid);
58042dff790SZachary Turner   if (pdb_type == nullptr)
58142dff790SZachary Turner     return nullptr;
58242dff790SZachary Turner 
58342dff790SZachary Turner   lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
584d5a925f4SAaron Smith   if (result) {
58542dff790SZachary Turner     m_types.insert(std::make_pair(type_uid, result));
586f46e8974SPavel Labath     GetTypeList().Insert(result);
587ec40f818SAaron Smith   }
58842dff790SZachary Turner   return result.get();
58974e08ca0SZachary Turner }
59074e08ca0SZachary Turner 
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)591eca07c59SAdrian Prantl llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
592eca07c59SAdrian Prantl     lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
593eca07c59SAdrian Prantl   return llvm::None;
594eca07c59SAdrian Prantl }
595eca07c59SAdrian Prantl 
CompleteType(lldb_private::CompilerType & compiler_type)596b9c1b51eSKate Stone bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
5977d2a74fcSAleksandr Urakov   std::lock_guard<std::recursive_mutex> guard(
5987d2a74fcSAleksandr Urakov       GetObjectFile()->GetModule()->GetMutex());
5997d2a74fcSAleksandr Urakov 
6000e252e38SAlex Langford   auto type_system_or_err =
6010e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6020e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
603a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
604a007a6d8SPavel Labath                    "Unable to get dynamic array info for UID");
6050e252e38SAlex Langford     return false;
6060e252e38SAlex Langford   }
6070e252e38SAlex Langford 
6086e3b0cc2SRaphael Isemann   TypeSystemClang *clang_ast_ctx =
6096e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
6100e252e38SAlex Langford 
6117d2a74fcSAleksandr Urakov   if (!clang_ast_ctx)
61274e08ca0SZachary Turner     return false;
6137d2a74fcSAleksandr Urakov 
614709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
6157d2a74fcSAleksandr Urakov   if (!pdb)
6167d2a74fcSAleksandr Urakov     return false;
6177d2a74fcSAleksandr Urakov 
6187d2a74fcSAleksandr Urakov   return pdb->CompleteTypeFromPDB(compiler_type);
61974e08ca0SZachary Turner }
62074e08ca0SZachary Turner 
GetDeclForUID(lldb::user_id_t uid)621b9c1b51eSKate Stone lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
6220e252e38SAlex Langford   auto type_system_or_err =
6230e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6240e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
625a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
626a007a6d8SPavel Labath                    "Unable to get decl for UID");
6270e252e38SAlex Langford     return CompilerDecl();
6280e252e38SAlex Langford   }
6290e252e38SAlex Langford 
6306e3b0cc2SRaphael Isemann   TypeSystemClang *clang_ast_ctx =
6316e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
632709426b3SAleksandr Urakov   if (!clang_ast_ctx)
633709426b3SAleksandr Urakov     return CompilerDecl();
634709426b3SAleksandr Urakov 
635709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
636709426b3SAleksandr Urakov   if (!pdb)
637709426b3SAleksandr Urakov     return CompilerDecl();
638709426b3SAleksandr Urakov 
639709426b3SAleksandr Urakov   auto symbol = m_session_up->getSymbolById(uid);
640709426b3SAleksandr Urakov   if (!symbol)
641709426b3SAleksandr Urakov     return CompilerDecl();
642709426b3SAleksandr Urakov 
643709426b3SAleksandr Urakov   auto decl = pdb->GetDeclForSymbol(*symbol);
644709426b3SAleksandr Urakov   if (!decl)
645709426b3SAleksandr Urakov     return CompilerDecl();
646709426b3SAleksandr Urakov 
64746ca55f2SRaphael Isemann   return clang_ast_ctx->GetCompilerDecl(decl);
64874e08ca0SZachary Turner }
64974e08ca0SZachary Turner 
65074e08ca0SZachary Turner lldb_private::CompilerDeclContext
GetDeclContextForUID(lldb::user_id_t uid)651b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
6520e252e38SAlex Langford   auto type_system_or_err =
6530e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6540e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
655a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
656a007a6d8SPavel Labath                    "Unable to get DeclContext for UID");
6570e252e38SAlex Langford     return CompilerDeclContext();
6580e252e38SAlex Langford   }
6590e252e38SAlex Langford 
6606e3b0cc2SRaphael Isemann   TypeSystemClang *clang_ast_ctx =
6616e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
662709426b3SAleksandr Urakov   if (!clang_ast_ctx)
663709426b3SAleksandr Urakov     return CompilerDeclContext();
664709426b3SAleksandr Urakov 
665709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
666709426b3SAleksandr Urakov   if (!pdb)
667709426b3SAleksandr Urakov     return CompilerDeclContext();
668709426b3SAleksandr Urakov 
669709426b3SAleksandr Urakov   auto symbol = m_session_up->getSymbolById(uid);
670709426b3SAleksandr Urakov   if (!symbol)
671709426b3SAleksandr Urakov     return CompilerDeclContext();
672709426b3SAleksandr Urakov 
673709426b3SAleksandr Urakov   auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
674709426b3SAleksandr Urakov   if (!decl_context)
675709426b3SAleksandr Urakov     return GetDeclContextContainingUID(uid);
676709426b3SAleksandr Urakov 
67742ec584aSRaphael Isemann   return clang_ast_ctx->CreateDeclContext(decl_context);
67874e08ca0SZachary Turner }
67974e08ca0SZachary Turner 
68074e08ca0SZachary Turner lldb_private::CompilerDeclContext
GetDeclContextContainingUID(lldb::user_id_t uid)681b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
6820e252e38SAlex Langford   auto type_system_or_err =
6830e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6840e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
685a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
686a007a6d8SPavel Labath                    "Unable to get DeclContext containing UID");
6870e252e38SAlex Langford     return CompilerDeclContext();
6880e252e38SAlex Langford   }
6890e252e38SAlex Langford 
6906e3b0cc2SRaphael Isemann   TypeSystemClang *clang_ast_ctx =
6916e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
692709426b3SAleksandr Urakov   if (!clang_ast_ctx)
693709426b3SAleksandr Urakov     return CompilerDeclContext();
694709426b3SAleksandr Urakov 
695709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
696709426b3SAleksandr Urakov   if (!pdb)
697709426b3SAleksandr Urakov     return CompilerDeclContext();
698709426b3SAleksandr Urakov 
699709426b3SAleksandr Urakov   auto symbol = m_session_up->getSymbolById(uid);
700709426b3SAleksandr Urakov   if (!symbol)
701709426b3SAleksandr Urakov     return CompilerDeclContext();
702709426b3SAleksandr Urakov 
703709426b3SAleksandr Urakov   auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
704709426b3SAleksandr Urakov   assert(decl_context);
705709426b3SAleksandr Urakov 
70642ec584aSRaphael Isemann   return clang_ast_ctx->CreateDeclContext(decl_context);
70774e08ca0SZachary Turner }
70874e08ca0SZachary Turner 
ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx)709b9c1b51eSKate Stone void SymbolFilePDB::ParseDeclsForContext(
710709426b3SAleksandr Urakov     lldb_private::CompilerDeclContext decl_ctx) {
7110e252e38SAlex Langford   auto type_system_or_err =
7120e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
7130e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
714a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
715a007a6d8SPavel Labath                    "Unable to parse decls for context");
7160e252e38SAlex Langford     return;
7170e252e38SAlex Langford   }
7180e252e38SAlex Langford 
7196e3b0cc2SRaphael Isemann   TypeSystemClang *clang_ast_ctx =
7206e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
721709426b3SAleksandr Urakov   if (!clang_ast_ctx)
722709426b3SAleksandr Urakov     return;
723709426b3SAleksandr Urakov 
724709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
725709426b3SAleksandr Urakov   if (!pdb)
726709426b3SAleksandr Urakov     return;
727709426b3SAleksandr Urakov 
728709426b3SAleksandr Urakov   pdb->ParseDeclsForDeclContext(
729709426b3SAleksandr Urakov       static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
730709426b3SAleksandr Urakov }
73174e08ca0SZachary Turner 
73274e08ca0SZachary Turner uint32_t
ResolveSymbolContext(const lldb_private::Address & so_addr,SymbolContextItem resolve_scope,lldb_private::SymbolContext & sc)733b9c1b51eSKate Stone SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
734991e4453SZachary Turner                                     SymbolContextItem resolve_scope,
735b9c1b51eSKate Stone                                     lldb_private::SymbolContext &sc) {
736656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7377ac1c780SAaron Smith   uint32_t resolved_flags = 0;
7384d4d63eeSPavel Labath   if (resolve_scope & eSymbolContextCompUnit ||
7394d4d63eeSPavel Labath       resolve_scope & eSymbolContextVariable ||
7404d4d63eeSPavel Labath       resolve_scope & eSymbolContextFunction ||
7414d4d63eeSPavel Labath       resolve_scope & eSymbolContextBlock ||
7427ac1c780SAaron Smith       resolve_scope & eSymbolContextLineEntry) {
7437ac1c780SAaron Smith     auto cu_sp = GetCompileUnitContainsAddress(so_addr);
7447ac1c780SAaron Smith     if (!cu_sp) {
74533cdbff2SRichard Trieu       if (resolved_flags & eSymbolContextVariable) {
7467ac1c780SAaron Smith         // TODO: Resolve variables
7477ac1c780SAaron Smith       }
7487ac1c780SAaron Smith       return 0;
7497ac1c780SAaron Smith     }
7507ac1c780SAaron Smith     sc.comp_unit = cu_sp.get();
7517ac1c780SAaron Smith     resolved_flags |= eSymbolContextCompUnit;
7527ac1c780SAaron Smith     lldbassert(sc.module_sp == cu_sp->GetModule());
7539ea80d25SPavel Labath   }
7547ac1c780SAaron Smith 
755398f81b3SAleksandr Urakov   if (resolve_scope & eSymbolContextFunction ||
756398f81b3SAleksandr Urakov       resolve_scope & eSymbolContextBlock) {
7579ea80d25SPavel Labath     addr_t file_vm_addr = so_addr.GetFileAddress();
7589ea80d25SPavel Labath     auto symbol_up =
7599ea80d25SPavel Labath         m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
7609ea80d25SPavel Labath     if (symbol_up) {
7617ac1c780SAaron Smith       auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
7627ac1c780SAaron Smith       assert(pdb_func);
7637ac1c780SAaron Smith       auto func_uid = pdb_func->getSymIndexId();
7647ac1c780SAaron Smith       sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
7657ac1c780SAaron Smith       if (sc.function == nullptr)
766863f8c18SZachary Turner         sc.function =
767863f8c18SZachary Turner             ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit);
7687ac1c780SAaron Smith       if (sc.function) {
7697ac1c780SAaron Smith         resolved_flags |= eSymbolContextFunction;
7707ac1c780SAaron Smith         if (resolve_scope & eSymbolContextBlock) {
771398f81b3SAleksandr Urakov           auto block_symbol = m_session_up->findSymbolByAddress(
772398f81b3SAleksandr Urakov               file_vm_addr, PDB_SymType::Block);
773398f81b3SAleksandr Urakov           auto block_id = block_symbol ? block_symbol->getSymIndexId()
774398f81b3SAleksandr Urakov                                        : sc.function->GetID();
775398f81b3SAleksandr Urakov           sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
7767ac1c780SAaron Smith           if (sc.block)
7777ac1c780SAaron Smith             resolved_flags |= eSymbolContextBlock;
7787ac1c780SAaron Smith         }
7797ac1c780SAaron Smith       }
7807ac1c780SAaron Smith     }
7817ac1c780SAaron Smith   }
7827ac1c780SAaron Smith 
7837ac1c780SAaron Smith   if (resolve_scope & eSymbolContextLineEntry) {
7847ac1c780SAaron Smith     if (auto *line_table = sc.comp_unit->GetLineTable()) {
7857ac1c780SAaron Smith       Address addr(so_addr);
7867ac1c780SAaron Smith       if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
7877ac1c780SAaron Smith         resolved_flags |= eSymbolContextLineEntry;
7887ac1c780SAaron Smith     }
7897ac1c780SAaron Smith   }
7909ea80d25SPavel Labath 
7917ac1c780SAaron Smith   return resolved_flags;
79274e08ca0SZachary Turner }
79374e08ca0SZachary Turner 
ResolveSymbolContext(const lldb_private::SourceLocationSpec & src_location_spec,SymbolContextItem resolve_scope,lldb_private::SymbolContextList & sc_list)794b9c1b51eSKate Stone uint32_t SymbolFilePDB::ResolveSymbolContext(
7953e2ed744SMed Ismail Bennani     const lldb_private::SourceLocationSpec &src_location_spec,
796991e4453SZachary Turner     SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
797656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
79810a02577SAaron Smith   const size_t old_size = sc_list.GetSize();
7993e2ed744SMed Ismail Bennani   const FileSpec &file_spec = src_location_spec.GetFileSpec();
800aa88161bSKazu Hirata   const uint32_t line = src_location_spec.GetLine().value_or(0);
801b9c1b51eSKate Stone   if (resolve_scope & lldb::eSymbolContextCompUnit) {
802b9c1b51eSKate Stone     // Locate all compilation units with line numbers referencing the specified
8039d0eb996SAdrian McCarthy     // file.  For example, if `file_spec` is <vector>, then this should return
8049d0eb996SAdrian McCarthy     // all source files and header files that reference <vector>, either
8059d0eb996SAdrian McCarthy     // directly or indirectly.
806b9c1b51eSKate Stone     auto compilands = m_session_up->findCompilandsForSourceFile(
807b9c1b51eSKate Stone         file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
80874e08ca0SZachary Turner 
80910a02577SAaron Smith     if (!compilands)
81010a02577SAaron Smith       return 0;
81110a02577SAaron Smith 
8129d0eb996SAdrian McCarthy     // For each one, either find its previously parsed data or parse it afresh
8139d0eb996SAdrian McCarthy     // and add it to the symbol context list.
814b9c1b51eSKate Stone     while (auto compiland = compilands->getNext()) {
81505097246SAdrian Prantl       // If we're not checking inlines, then don't add line information for
81605097246SAdrian Prantl       // this file unless the FileSpec matches. For inline functions, we don't
81705097246SAdrian Prantl       // have to match the FileSpec since they could be defined in headers
81805097246SAdrian Prantl       // other than file specified in FileSpec.
8193e2ed744SMed Ismail Bennani       if (!src_location_spec.GetCheckInlines()) {
820487b0c6bSAaron Smith         std::string source_file = compiland->getSourceFileFullPath();
82110a02577SAaron Smith         if (source_file.empty())
82210a02577SAaron Smith           continue;
8238f3be7a3SJonas Devlieghere         FileSpec this_spec(source_file, FileSpec::Style::windows);
82410a02577SAaron Smith         bool need_full_match = !file_spec.GetDirectory().IsEmpty();
82510a02577SAaron Smith         if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
82674e08ca0SZachary Turner           continue;
82774e08ca0SZachary Turner       }
82874e08ca0SZachary Turner 
82974e08ca0SZachary Turner       SymbolContext sc;
83010a02577SAaron Smith       auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
831d5a925f4SAaron Smith       if (!cu)
83210a02577SAaron Smith         continue;
83374e08ca0SZachary Turner       sc.comp_unit = cu.get();
83474e08ca0SZachary Turner       sc.module_sp = cu->GetModule();
83574e08ca0SZachary Turner 
836b9c1b51eSKate Stone       // If we were asked to resolve line entries, add all entries to the line
8379d0eb996SAdrian McCarthy       // table that match the requested line (or all lines if `line` == 0).
8387ac1c780SAaron Smith       if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
8397ac1c780SAaron Smith                            eSymbolContextLineEntry)) {
840863f8c18SZachary Turner         bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line);
8417ac1c780SAaron Smith 
8427ac1c780SAaron Smith         if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
8437ac1c780SAaron Smith           // The query asks for line entries, but we can't get them for the
84405097246SAdrian Prantl           // compile unit. This is not normal for `line` = 0. So just assert
84505097246SAdrian Prantl           // it.
846f76fe682SAaron Smith           assert(line && "Couldn't get all line entries!\n");
8477ac1c780SAaron Smith 
8487ac1c780SAaron Smith           // Current compiland does not have the requested line. Search next.
8497ac1c780SAaron Smith           continue;
8507ac1c780SAaron Smith         }
8517ac1c780SAaron Smith 
8527ac1c780SAaron Smith         if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
8537ac1c780SAaron Smith           if (!has_line_table)
8547ac1c780SAaron Smith             continue;
8557ac1c780SAaron Smith 
8567ac1c780SAaron Smith           auto *line_table = sc.comp_unit->GetLineTable();
8577ac1c780SAaron Smith           lldbassert(line_table);
8587ac1c780SAaron Smith 
8597ac1c780SAaron Smith           uint32_t num_line_entries = line_table->GetSize();
8607ac1c780SAaron Smith           // Skip the terminal line entry.
8617ac1c780SAaron Smith           --num_line_entries;
8627ac1c780SAaron Smith 
86305097246SAdrian Prantl           // If `line `!= 0, see if we can resolve function for each line entry
86405097246SAdrian Prantl           // in the line table.
8657ac1c780SAaron Smith           for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
8667ac1c780SAaron Smith                ++line_idx) {
8677ac1c780SAaron Smith             if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
8687ac1c780SAaron Smith               continue;
8697ac1c780SAaron Smith 
8707ac1c780SAaron Smith             auto file_vm_addr =
8717ac1c780SAaron Smith                 sc.line_entry.range.GetBaseAddress().GetFileAddress();
872308e39caSAaron Smith             if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
8737ac1c780SAaron Smith               continue;
8747ac1c780SAaron Smith 
875c8316ed2SAaron Smith             auto symbol_up = m_session_up->findSymbolByAddress(
876c8316ed2SAaron Smith                 file_vm_addr, PDB_SymType::Function);
8777ac1c780SAaron Smith             if (symbol_up) {
8787ac1c780SAaron Smith               auto func_uid = symbol_up->getSymIndexId();
8797ac1c780SAaron Smith               sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
8807ac1c780SAaron Smith               if (sc.function == nullptr) {
8817ac1c780SAaron Smith                 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
8827ac1c780SAaron Smith                 assert(pdb_func);
883863f8c18SZachary Turner                 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func,
884863f8c18SZachary Turner                                                                  *sc.comp_unit);
8857ac1c780SAaron Smith               }
8867ac1c780SAaron Smith               if (sc.function && (resolve_scope & eSymbolContextBlock)) {
8877ac1c780SAaron Smith                 Block &block = sc.function->GetBlock(true);
8887ac1c780SAaron Smith                 sc.block = block.FindBlockByID(sc.function->GetID());
8897ac1c780SAaron Smith               }
8907ac1c780SAaron Smith             }
8917ac1c780SAaron Smith             sc_list.Append(sc);
8927ac1c780SAaron Smith           }
8937ac1c780SAaron Smith         } else if (has_line_table) {
8947ac1c780SAaron Smith           // We can parse line table for the compile unit. But no query to
8957ac1c780SAaron Smith           // resolve function or block. We append `sc` to the list anyway.
8967ac1c780SAaron Smith           sc_list.Append(sc);
8977ac1c780SAaron Smith         }
8987ac1c780SAaron Smith       } else {
8997ac1c780SAaron Smith         // No query for line entry, function or block. But we have a valid
9007ac1c780SAaron Smith         // compile unit, append `sc` to the list.
9017ac1c780SAaron Smith         sc_list.Append(sc);
9027ac1c780SAaron Smith       }
90374e08ca0SZachary Turner     }
90474e08ca0SZachary Turner   }
90510a02577SAaron Smith   return sc_list.GetSize() - old_size;
90674e08ca0SZachary Turner }
90774e08ca0SZachary Turner 
GetMangledForPDBData(const PDBSymbolData & pdb_data)908cab0d23fSAaron Smith std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
909356aa4a9SAleksandr Urakov   // Cache public names at first
910356aa4a9SAleksandr Urakov   if (m_public_names.empty())
911356aa4a9SAleksandr Urakov     if (auto result_up =
912356aa4a9SAleksandr Urakov             m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
913356aa4a9SAleksandr Urakov       while (auto symbol_up = result_up->getNext())
914356aa4a9SAleksandr Urakov         if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
915356aa4a9SAleksandr Urakov           m_public_names[addr] = symbol_up->getRawSymbol().getName();
916cab0d23fSAaron Smith 
917356aa4a9SAleksandr Urakov   // Look up the name in the cache
918356aa4a9SAleksandr Urakov   return m_public_names.lookup(pdb_data.getVirtualAddress());
919cab0d23fSAaron Smith }
920cab0d23fSAaron Smith 
ParseVariableForPDBData(const lldb_private::SymbolContext & sc,const llvm::pdb::PDBSymbolData & pdb_data)921cab0d23fSAaron Smith VariableSP SymbolFilePDB::ParseVariableForPDBData(
922cab0d23fSAaron Smith     const lldb_private::SymbolContext &sc,
923cab0d23fSAaron Smith     const llvm::pdb::PDBSymbolData &pdb_data) {
924cab0d23fSAaron Smith   VariableSP var_sp;
925cab0d23fSAaron Smith   uint32_t var_uid = pdb_data.getSymIndexId();
926cab0d23fSAaron Smith   auto result = m_variables.find(var_uid);
927cab0d23fSAaron Smith   if (result != m_variables.end())
928cab0d23fSAaron Smith     return result->second;
929cab0d23fSAaron Smith 
930cab0d23fSAaron Smith   ValueType scope = eValueTypeInvalid;
931cab0d23fSAaron Smith   bool is_static_member = false;
932cab0d23fSAaron Smith   bool is_external = false;
933cab0d23fSAaron Smith   bool is_artificial = false;
934cab0d23fSAaron Smith 
935cab0d23fSAaron Smith   switch (pdb_data.getDataKind()) {
936cab0d23fSAaron Smith   case PDB_DataKind::Global:
937cab0d23fSAaron Smith     scope = eValueTypeVariableGlobal;
938cab0d23fSAaron Smith     is_external = true;
939cab0d23fSAaron Smith     break;
940cab0d23fSAaron Smith   case PDB_DataKind::Local:
941cab0d23fSAaron Smith     scope = eValueTypeVariableLocal;
942cab0d23fSAaron Smith     break;
943cab0d23fSAaron Smith   case PDB_DataKind::FileStatic:
944cab0d23fSAaron Smith     scope = eValueTypeVariableStatic;
945cab0d23fSAaron Smith     break;
946cab0d23fSAaron Smith   case PDB_DataKind::StaticMember:
947cab0d23fSAaron Smith     is_static_member = true;
948cab0d23fSAaron Smith     scope = eValueTypeVariableStatic;
949cab0d23fSAaron Smith     break;
950cab0d23fSAaron Smith   case PDB_DataKind::Member:
951cab0d23fSAaron Smith     scope = eValueTypeVariableStatic;
952cab0d23fSAaron Smith     break;
953cab0d23fSAaron Smith   case PDB_DataKind::Param:
954cab0d23fSAaron Smith     scope = eValueTypeVariableArgument;
955cab0d23fSAaron Smith     break;
956cab0d23fSAaron Smith   case PDB_DataKind::Constant:
957cab0d23fSAaron Smith     scope = eValueTypeConstResult;
958cab0d23fSAaron Smith     break;
959cab0d23fSAaron Smith   default:
960cab0d23fSAaron Smith     break;
961cab0d23fSAaron Smith   }
962cab0d23fSAaron Smith 
963cab0d23fSAaron Smith   switch (pdb_data.getLocationType()) {
964cab0d23fSAaron Smith   case PDB_LocType::TLS:
965cab0d23fSAaron Smith     scope = eValueTypeVariableThreadLocal;
966cab0d23fSAaron Smith     break;
967cab0d23fSAaron Smith   case PDB_LocType::RegRel: {
968cab0d23fSAaron Smith     // It is a `this` pointer.
969cab0d23fSAaron Smith     if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
970cab0d23fSAaron Smith       scope = eValueTypeVariableArgument;
971cab0d23fSAaron Smith       is_artificial = true;
972cab0d23fSAaron Smith     }
973cab0d23fSAaron Smith   } break;
974cab0d23fSAaron Smith   default:
975cab0d23fSAaron Smith     break;
976cab0d23fSAaron Smith   }
977cab0d23fSAaron Smith 
978cab0d23fSAaron Smith   Declaration decl;
979cab0d23fSAaron Smith   if (!is_artificial && !pdb_data.isCompilerGenerated()) {
980cab0d23fSAaron Smith     if (auto lines = pdb_data.getLineNumbers()) {
981cab0d23fSAaron Smith       if (auto first_line = lines->getNext()) {
982cab0d23fSAaron Smith         uint32_t src_file_id = first_line->getSourceFileId();
983cab0d23fSAaron Smith         auto src_file = m_session_up->getSourceFileById(src_file_id);
984cab0d23fSAaron Smith         if (src_file) {
9858f3be7a3SJonas Devlieghere           FileSpec spec(src_file->getFileName());
986cab0d23fSAaron Smith           decl.SetFile(spec);
987cab0d23fSAaron Smith           decl.SetColumn(first_line->getColumnNumber());
988cab0d23fSAaron Smith           decl.SetLine(first_line->getLineNumber());
989cab0d23fSAaron Smith         }
990cab0d23fSAaron Smith       }
991cab0d23fSAaron Smith     }
992cab0d23fSAaron Smith   }
993cab0d23fSAaron Smith 
994cab0d23fSAaron Smith   Variable::RangeList ranges;
995cab0d23fSAaron Smith   SymbolContextScope *context_scope = sc.comp_unit;
996758657e5SAleksandr Urakov   if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) {
997cab0d23fSAaron Smith     if (sc.function) {
998758657e5SAleksandr Urakov       Block &function_block = sc.function->GetBlock(true);
999758657e5SAleksandr Urakov       Block *block =
1000758657e5SAleksandr Urakov           function_block.FindBlockByID(pdb_data.getLexicalParentId());
1001758657e5SAleksandr Urakov       if (!block)
1002758657e5SAleksandr Urakov         block = &function_block;
1003758657e5SAleksandr Urakov 
1004758657e5SAleksandr Urakov       context_scope = block;
1005758657e5SAleksandr Urakov 
1006758657e5SAleksandr Urakov       for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges;
1007758657e5SAleksandr Urakov            ++i) {
1008758657e5SAleksandr Urakov         AddressRange range;
1009758657e5SAleksandr Urakov         if (!block->GetRangeAtIndex(i, range))
1010758657e5SAleksandr Urakov           continue;
1011758657e5SAleksandr Urakov 
1012758657e5SAleksandr Urakov         ranges.Append(range.GetBaseAddress().GetFileAddress(),
1013758657e5SAleksandr Urakov                       range.GetByteSize());
1014758657e5SAleksandr Urakov       }
1015cab0d23fSAaron Smith     }
1016cab0d23fSAaron Smith   }
1017cab0d23fSAaron Smith 
1018cab0d23fSAaron Smith   SymbolFileTypeSP type_sp =
1019cab0d23fSAaron Smith       std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
1020cab0d23fSAaron Smith 
1021cab0d23fSAaron Smith   auto var_name = pdb_data.getName();
1022cab0d23fSAaron Smith   auto mangled = GetMangledForPDBData(pdb_data);
1023cab0d23fSAaron Smith   auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
1024cab0d23fSAaron Smith 
1025924d5608SJonas Devlieghere   bool is_constant;
1026*b74a01a8SZequan Wu   ModuleSP module_sp = GetObjectFile()->GetModule();
1027*b74a01a8SZequan Wu   DWARFExpressionList location(module_sp,
1028*b74a01a8SZequan Wu                                ConvertPDBLocationToDWARFExpression(
1029*b74a01a8SZequan Wu                                    module_sp, pdb_data, ranges, is_constant),
1030*b74a01a8SZequan Wu                                nullptr);
1031cab0d23fSAaron Smith 
1032cab0d23fSAaron Smith   var_sp = std::make_shared<Variable>(
1033cab0d23fSAaron Smith       var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
10349b1c06c0SAdrian Prantl       ranges, &decl, location, is_external, is_artificial, is_constant,
10359b1c06c0SAdrian Prantl       is_static_member);
1036cab0d23fSAaron Smith 
1037cab0d23fSAaron Smith   m_variables.insert(std::make_pair(var_uid, var_sp));
1038cab0d23fSAaron Smith   return var_sp;
1039cab0d23fSAaron Smith }
1040cab0d23fSAaron Smith 
1041cab0d23fSAaron Smith size_t
ParseVariables(const lldb_private::SymbolContext & sc,const llvm::pdb::PDBSymbol & pdb_symbol,lldb_private::VariableList * variable_list)1042cab0d23fSAaron Smith SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
1043cab0d23fSAaron Smith                               const llvm::pdb::PDBSymbol &pdb_symbol,
1044cab0d23fSAaron Smith                               lldb_private::VariableList *variable_list) {
1045cab0d23fSAaron Smith   size_t num_added = 0;
1046cab0d23fSAaron Smith 
1047cab0d23fSAaron Smith   if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
1048cab0d23fSAaron Smith     VariableListSP local_variable_list_sp;
1049cab0d23fSAaron Smith 
1050cab0d23fSAaron Smith     auto result = m_variables.find(pdb_data->getSymIndexId());
1051cab0d23fSAaron Smith     if (result != m_variables.end()) {
1052cab0d23fSAaron Smith       if (variable_list)
1053cab0d23fSAaron Smith         variable_list->AddVariableIfUnique(result->second);
1054cab0d23fSAaron Smith     } else {
1055cab0d23fSAaron Smith       // Prepare right VariableList for this variable.
1056cab0d23fSAaron Smith       if (auto lexical_parent = pdb_data->getLexicalParent()) {
1057cab0d23fSAaron Smith         switch (lexical_parent->getSymTag()) {
1058cab0d23fSAaron Smith         case PDB_SymType::Exe:
1059cab0d23fSAaron Smith           assert(sc.comp_unit);
1060cab0d23fSAaron Smith           LLVM_FALLTHROUGH;
1061cab0d23fSAaron Smith         case PDB_SymType::Compiland: {
1062cab0d23fSAaron Smith           if (sc.comp_unit) {
1063cab0d23fSAaron Smith             local_variable_list_sp = sc.comp_unit->GetVariableList(false);
1064cab0d23fSAaron Smith             if (!local_variable_list_sp) {
1065cab0d23fSAaron Smith               local_variable_list_sp = std::make_shared<VariableList>();
1066cab0d23fSAaron Smith               sc.comp_unit->SetVariableList(local_variable_list_sp);
1067cab0d23fSAaron Smith             }
1068cab0d23fSAaron Smith           }
1069cab0d23fSAaron Smith         } break;
1070cab0d23fSAaron Smith         case PDB_SymType::Block:
1071cab0d23fSAaron Smith         case PDB_SymType::Function: {
1072cab0d23fSAaron Smith           if (sc.function) {
1073cab0d23fSAaron Smith             Block *block = sc.function->GetBlock(true).FindBlockByID(
1074cab0d23fSAaron Smith                 lexical_parent->getSymIndexId());
1075cab0d23fSAaron Smith             if (block) {
1076cab0d23fSAaron Smith               local_variable_list_sp = block->GetBlockVariableList(false);
1077cab0d23fSAaron Smith               if (!local_variable_list_sp) {
1078cab0d23fSAaron Smith                 local_variable_list_sp = std::make_shared<VariableList>();
1079cab0d23fSAaron Smith                 block->SetVariableList(local_variable_list_sp);
1080cab0d23fSAaron Smith               }
1081cab0d23fSAaron Smith             }
1082cab0d23fSAaron Smith           }
1083cab0d23fSAaron Smith         } break;
1084cab0d23fSAaron Smith         default:
1085cab0d23fSAaron Smith           break;
1086cab0d23fSAaron Smith         }
1087cab0d23fSAaron Smith       }
1088cab0d23fSAaron Smith 
1089cab0d23fSAaron Smith       if (local_variable_list_sp) {
1090cab0d23fSAaron Smith         if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1091cab0d23fSAaron Smith           local_variable_list_sp->AddVariableIfUnique(var_sp);
1092cab0d23fSAaron Smith           if (variable_list)
1093cab0d23fSAaron Smith             variable_list->AddVariableIfUnique(var_sp);
1094cab0d23fSAaron Smith           ++num_added;
1095c68925abSZachary Turner           PDBASTParser *ast = GetPDBAstParser();
1096c68925abSZachary Turner           if (ast)
1097c68925abSZachary Turner             ast->GetDeclForSymbol(*pdb_data);
1098cab0d23fSAaron Smith         }
1099cab0d23fSAaron Smith       }
1100cab0d23fSAaron Smith     }
1101cab0d23fSAaron Smith   }
1102cab0d23fSAaron Smith 
1103cab0d23fSAaron Smith   if (auto results = pdb_symbol.findAllChildren()) {
1104cab0d23fSAaron Smith     while (auto result = results->getNext())
1105cab0d23fSAaron Smith       num_added += ParseVariables(sc, *result, variable_list);
1106cab0d23fSAaron Smith   }
1107cab0d23fSAaron Smith 
1108cab0d23fSAaron Smith   return num_added;
1109cab0d23fSAaron Smith }
1110cab0d23fSAaron Smith 
FindGlobalVariables(lldb_private::ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,lldb_private::VariableList & variables)11111ad655e2SAdrian Prantl void SymbolFilePDB::FindGlobalVariables(
1112f9568a95SRaphael Isemann     lldb_private::ConstString name, const CompilerDeclContext &parent_decl_ctx,
1113b9c1b51eSKate Stone     uint32_t max_matches, lldb_private::VariableList &variables) {
1114656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1115cab0d23fSAaron Smith   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
11161ad655e2SAdrian Prantl     return;
1117cab0d23fSAaron Smith   if (name.IsEmpty())
11181ad655e2SAdrian Prantl     return;
1119cab0d23fSAaron Smith 
1120709426b3SAleksandr Urakov   auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1121cab0d23fSAaron Smith   if (!results)
11221ad655e2SAdrian Prantl     return;
1123cab0d23fSAaron Smith 
1124cab0d23fSAaron Smith   uint32_t matches = 0;
1125cab0d23fSAaron Smith   size_t old_size = variables.GetSize();
1126cab0d23fSAaron Smith   while (auto result = results->getNext()) {
1127cab0d23fSAaron Smith     auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1128cab0d23fSAaron Smith     if (max_matches > 0 && matches >= max_matches)
1129cab0d23fSAaron Smith       break;
1130cab0d23fSAaron Smith 
1131cab0d23fSAaron Smith     SymbolContext sc;
1132d2deeb44SPavel Labath     sc.module_sp = m_objfile_sp->GetModule();
1133cab0d23fSAaron Smith     lldbassert(sc.module_sp.get());
1134cab0d23fSAaron Smith 
1135709426b3SAleksandr Urakov     if (!name.GetStringRef().equals(
1136c1e530eeSAleksandr Urakov             MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
1137709426b3SAleksandr Urakov       continue;
1138709426b3SAleksandr Urakov 
1139356aa4a9SAleksandr Urakov     sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1140356aa4a9SAleksandr Urakov     // FIXME: We are not able to determine the compile unit.
1141356aa4a9SAleksandr Urakov     if (sc.comp_unit == nullptr)
1142356aa4a9SAleksandr Urakov       continue;
1143356aa4a9SAleksandr Urakov 
1144f9568a95SRaphael Isemann     if (parent_decl_ctx.IsValid() &&
1145f9568a95SRaphael Isemann         GetDeclContextContainingUID(result->getSymIndexId()) != parent_decl_ctx)
1146709426b3SAleksandr Urakov       continue;
1147709426b3SAleksandr Urakov 
1148cab0d23fSAaron Smith     ParseVariables(sc, *pdb_data, &variables);
1149cab0d23fSAaron Smith     matches = variables.GetSize() - old_size;
1150cab0d23fSAaron Smith   }
115174e08ca0SZachary Turner }
115274e08ca0SZachary Turner 
FindGlobalVariables(const lldb_private::RegularExpression & regex,uint32_t max_matches,lldb_private::VariableList & variables)11531ad655e2SAdrian Prantl void SymbolFilePDB::FindGlobalVariables(
11541ad655e2SAdrian Prantl     const lldb_private::RegularExpression &regex, uint32_t max_matches,
1155b9c1b51eSKate Stone     lldb_private::VariableList &variables) {
1156656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1157cab0d23fSAaron Smith   if (!regex.IsValid())
11581ad655e2SAdrian Prantl     return;
1159cab0d23fSAaron Smith   auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1160cab0d23fSAaron Smith   if (!results)
11611ad655e2SAdrian Prantl     return;
1162cab0d23fSAaron Smith 
1163cab0d23fSAaron Smith   uint32_t matches = 0;
1164cab0d23fSAaron Smith   size_t old_size = variables.GetSize();
1165cab0d23fSAaron Smith   while (auto pdb_data = results->getNext()) {
1166cab0d23fSAaron Smith     if (max_matches > 0 && matches >= max_matches)
1167cab0d23fSAaron Smith       break;
1168cab0d23fSAaron Smith 
1169cab0d23fSAaron Smith     auto var_name = pdb_data->getName();
1170cab0d23fSAaron Smith     if (var_name.empty())
1171cab0d23fSAaron Smith       continue;
1172cab0d23fSAaron Smith     if (!regex.Execute(var_name))
1173cab0d23fSAaron Smith       continue;
1174cab0d23fSAaron Smith     SymbolContext sc;
1175d2deeb44SPavel Labath     sc.module_sp = m_objfile_sp->GetModule();
1176cab0d23fSAaron Smith     lldbassert(sc.module_sp.get());
1177cab0d23fSAaron Smith 
1178356aa4a9SAleksandr Urakov     sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1179cab0d23fSAaron Smith     // FIXME: We are not able to determine the compile unit.
1180cab0d23fSAaron Smith     if (sc.comp_unit == nullptr)
1181cab0d23fSAaron Smith       continue;
1182cab0d23fSAaron Smith 
1183cab0d23fSAaron Smith     ParseVariables(sc, *pdb_data, &variables);
1184cab0d23fSAaron Smith     matches = variables.GetSize() - old_size;
1185cab0d23fSAaron Smith   }
1186b9c1b51eSKate Stone }
1187b9c1b51eSKate Stone 
ResolveFunction(const llvm::pdb::PDBSymbolFunc & pdb_func,bool include_inlines,lldb_private::SymbolContextList & sc_list)1188e664b5dcSAaron Smith bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
11897ac1c780SAaron Smith                                     bool include_inlines,
11907ac1c780SAaron Smith                                     lldb_private::SymbolContextList &sc_list) {
11917ac1c780SAaron Smith   lldb_private::SymbolContext sc;
1192a3a8cc80SAaron Smith   sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
11937ac1c780SAaron Smith   if (!sc.comp_unit)
11947ac1c780SAaron Smith     return false;
11957ac1c780SAaron Smith   sc.module_sp = sc.comp_unit->GetModule();
1196863f8c18SZachary Turner   sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit);
11977ac1c780SAaron Smith   if (!sc.function)
11987ac1c780SAaron Smith     return false;
11997ac1c780SAaron Smith 
12007ac1c780SAaron Smith   sc_list.Append(sc);
12017ac1c780SAaron Smith   return true;
12027ac1c780SAaron Smith }
12037ac1c780SAaron Smith 
ResolveFunction(uint32_t uid,bool include_inlines,lldb_private::SymbolContextList & sc_list)12047ac1c780SAaron Smith bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
12057ac1c780SAaron Smith                                     lldb_private::SymbolContextList &sc_list) {
1206c8316ed2SAaron Smith   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
12077ac1c780SAaron Smith   if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
12087ac1c780SAaron Smith     return false;
1209e664b5dcSAaron Smith   return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
12107ac1c780SAaron Smith }
12117ac1c780SAaron Smith 
CacheFunctionNames()12127ac1c780SAaron Smith void SymbolFilePDB::CacheFunctionNames() {
12137ac1c780SAaron Smith   if (!m_func_full_names.IsEmpty())
12147ac1c780SAaron Smith     return;
12157ac1c780SAaron Smith 
12167ac1c780SAaron Smith   std::map<uint64_t, uint32_t> addr_ids;
12177ac1c780SAaron Smith 
12187ac1c780SAaron Smith   if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
12197ac1c780SAaron Smith     while (auto pdb_func_up = results_up->getNext()) {
1220f76fe682SAaron Smith       if (pdb_func_up->isCompilerGenerated())
1221f76fe682SAaron Smith         continue;
1222f76fe682SAaron Smith 
12237ac1c780SAaron Smith       auto name = pdb_func_up->getName();
12247ac1c780SAaron Smith       auto demangled_name = pdb_func_up->getUndecoratedName();
12257ac1c780SAaron Smith       if (name.empty() && demangled_name.empty())
12267ac1c780SAaron Smith         continue;
12277ac1c780SAaron Smith 
1228f76fe682SAaron Smith       auto uid = pdb_func_up->getSymIndexId();
12297ac1c780SAaron Smith       if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
12307ac1c780SAaron Smith         addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
12317ac1c780SAaron Smith 
12327ac1c780SAaron Smith       if (auto parent = pdb_func_up->getClassParent()) {
12337ac1c780SAaron Smith 
12347ac1c780SAaron Smith         // PDB have symbols for class/struct methods or static methods in Enum
12357ac1c780SAaron Smith         // Class. We won't bother to check if the parent is UDT or Enum here.
12367ac1c780SAaron Smith         m_func_method_names.Append(ConstString(name), uid);
12377ac1c780SAaron Smith 
123805097246SAdrian Prantl         // To search a method name, like NS::Class:MemberFunc, LLDB searches
123905097246SAdrian Prantl         // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1240fa001767SAdrian Prantl         // not have information of this, we extract base names and cache them
124105097246SAdrian Prantl         // by our own effort.
1242c1e530eeSAleksandr Urakov         llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
12437ac1c780SAaron Smith         if (!basename.empty())
12447ac1c780SAaron Smith           m_func_base_names.Append(ConstString(basename), uid);
12457ac1c780SAaron Smith         else {
12467ac1c780SAaron Smith           m_func_base_names.Append(ConstString(name), uid);
12477ac1c780SAaron Smith         }
12487ac1c780SAaron Smith 
12497ac1c780SAaron Smith         if (!demangled_name.empty())
12507ac1c780SAaron Smith           m_func_full_names.Append(ConstString(demangled_name), uid);
12517ac1c780SAaron Smith 
12527ac1c780SAaron Smith       } else {
12537ac1c780SAaron Smith         // Handle not-method symbols.
12547ac1c780SAaron Smith 
1255c1e530eeSAleksandr Urakov         // The function name might contain namespace, or its lexical scope.
1256c1e530eeSAleksandr Urakov         llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1257c1e530eeSAleksandr Urakov         if (!basename.empty())
1258c1e530eeSAleksandr Urakov           m_func_base_names.Append(ConstString(basename), uid);
1259c1e530eeSAleksandr Urakov         else
12607ac1c780SAaron Smith           m_func_base_names.Append(ConstString(name), uid);
12617ac1c780SAaron Smith 
12627ac1c780SAaron Smith         if (name == "main") {
12637ac1c780SAaron Smith           m_func_full_names.Append(ConstString(name), uid);
12647ac1c780SAaron Smith 
12657ac1c780SAaron Smith           if (!demangled_name.empty() && name != demangled_name) {
12667ac1c780SAaron Smith             m_func_full_names.Append(ConstString(demangled_name), uid);
12677ac1c780SAaron Smith             m_func_base_names.Append(ConstString(demangled_name), uid);
12687ac1c780SAaron Smith           }
12697ac1c780SAaron Smith         } else if (!demangled_name.empty()) {
12707ac1c780SAaron Smith           m_func_full_names.Append(ConstString(demangled_name), uid);
12717ac1c780SAaron Smith         } else {
12727ac1c780SAaron Smith           m_func_full_names.Append(ConstString(name), uid);
12737ac1c780SAaron Smith         }
12747ac1c780SAaron Smith       }
12757ac1c780SAaron Smith     }
12767ac1c780SAaron Smith   }
12777ac1c780SAaron Smith 
12787ac1c780SAaron Smith   if (auto results_up =
12797ac1c780SAaron Smith           m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
12807ac1c780SAaron Smith     while (auto pub_sym_up = results_up->getNext()) {
12817ac1c780SAaron Smith       if (!pub_sym_up->isFunction())
12827ac1c780SAaron Smith         continue;
12837ac1c780SAaron Smith       auto name = pub_sym_up->getName();
12847ac1c780SAaron Smith       if (name.empty())
12857ac1c780SAaron Smith         continue;
12867ac1c780SAaron Smith 
12877ac1c780SAaron Smith       if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
12887ac1c780SAaron Smith         auto vm_addr = pub_sym_up->getVirtualAddress();
12897ac1c780SAaron Smith 
12907ac1c780SAaron Smith         // PDB public symbol has mangled name for its associated function.
12917ac1c780SAaron Smith         if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
12927ac1c780SAaron Smith           // Cache mangled name.
12937ac1c780SAaron Smith           m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
12947ac1c780SAaron Smith         }
12957ac1c780SAaron Smith       }
12967ac1c780SAaron Smith     }
12977ac1c780SAaron Smith   }
12987ac1c780SAaron Smith   // Sort them before value searching is working properly
12997ac1c780SAaron Smith   m_func_full_names.Sort();
13007ac1c780SAaron Smith   m_func_full_names.SizeToFit();
13017ac1c780SAaron Smith   m_func_method_names.Sort();
13027ac1c780SAaron Smith   m_func_method_names.SizeToFit();
13037ac1c780SAaron Smith   m_func_base_names.Sort();
13047ac1c780SAaron Smith   m_func_base_names.SizeToFit();
13057ac1c780SAaron Smith }
13067ac1c780SAaron Smith 
FindFunctions(lldb_private::ConstString name,const lldb_private::CompilerDeclContext & parent_decl_ctx,FunctionNameType name_type_mask,bool include_inlines,lldb_private::SymbolContextList & sc_list)13071ad655e2SAdrian Prantl void SymbolFilePDB::FindFunctions(
13080e4c4821SAdrian Prantl     lldb_private::ConstString name,
1309f9568a95SRaphael Isemann     const lldb_private::CompilerDeclContext &parent_decl_ctx,
13101ad655e2SAdrian Prantl     FunctionNameType name_type_mask, bool include_inlines,
1311b9c1b51eSKate Stone     lldb_private::SymbolContextList &sc_list) {
1312656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
13137ac1c780SAaron Smith   lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
13147ac1c780SAaron Smith 
13157ac1c780SAaron Smith   if (name_type_mask == eFunctionNameTypeNone)
13161ad655e2SAdrian Prantl     return;
13177ac1c780SAaron Smith   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
13181ad655e2SAdrian Prantl     return;
13197ac1c780SAaron Smith   if (name.IsEmpty())
13201ad655e2SAdrian Prantl     return;
13217ac1c780SAaron Smith 
13224d4d63eeSPavel Labath   if (name_type_mask & eFunctionNameTypeFull ||
13234d4d63eeSPavel Labath       name_type_mask & eFunctionNameTypeBase ||
13247ac1c780SAaron Smith       name_type_mask & eFunctionNameTypeMethod) {
13257ac1c780SAaron Smith     CacheFunctionNames();
13267ac1c780SAaron Smith 
13277ac1c780SAaron Smith     std::set<uint32_t> resolved_ids;
1328a5235af9SAleksandr Urakov     auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1329a5235af9SAleksandr Urakov                       &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
13307ac1c780SAaron Smith       std::vector<uint32_t> ids;
1331a5235af9SAleksandr Urakov       if (!Names.GetValues(name, ids))
1332a5235af9SAleksandr Urakov         return;
1333a5235af9SAleksandr Urakov 
1334a5235af9SAleksandr Urakov       for (uint32_t id : ids) {
1335a5235af9SAleksandr Urakov         if (resolved_ids.find(id) != resolved_ids.end())
1336a5235af9SAleksandr Urakov           continue;
1337a5235af9SAleksandr Urakov 
1338f9568a95SRaphael Isemann         if (parent_decl_ctx.IsValid() &&
1339f9568a95SRaphael Isemann             GetDeclContextContainingUID(id) != parent_decl_ctx)
1340a5235af9SAleksandr Urakov           continue;
1341a5235af9SAleksandr Urakov 
13427ac1c780SAaron Smith         if (ResolveFunction(id, include_inlines, sc_list))
13437ac1c780SAaron Smith           resolved_ids.insert(id);
13447ac1c780SAaron Smith       }
13457ac1c780SAaron Smith     };
13467ac1c780SAaron Smith     if (name_type_mask & eFunctionNameTypeFull) {
13477ac1c780SAaron Smith       ResolveFn(m_func_full_names);
1348a5235af9SAleksandr Urakov       ResolveFn(m_func_base_names);
1349a5235af9SAleksandr Urakov       ResolveFn(m_func_method_names);
13507ac1c780SAaron Smith     }
13511ad655e2SAdrian Prantl     if (name_type_mask & eFunctionNameTypeBase)
13527ac1c780SAaron Smith       ResolveFn(m_func_base_names);
13531ad655e2SAdrian Prantl     if (name_type_mask & eFunctionNameTypeMethod)
13547ac1c780SAaron Smith       ResolveFn(m_func_method_names);
13557ac1c780SAaron Smith   }
13567ac1c780SAaron Smith }
135774e08ca0SZachary Turner 
FindFunctions(const lldb_private::RegularExpression & regex,bool include_inlines,lldb_private::SymbolContextList & sc_list)13581ad655e2SAdrian Prantl void SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
13591ad655e2SAdrian Prantl                                   bool include_inlines,
1360b9c1b51eSKate Stone                                   lldb_private::SymbolContextList &sc_list) {
1361656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
13627ac1c780SAaron Smith   if (!regex.IsValid())
13631ad655e2SAdrian Prantl     return;
13647ac1c780SAaron Smith 
13657ac1c780SAaron Smith   CacheFunctionNames();
13667ac1c780SAaron Smith 
13677ac1c780SAaron Smith   std::set<uint32_t> resolved_ids;
1368c8316ed2SAaron Smith   auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1369c8316ed2SAaron Smith                     this](UniqueCStringMap<uint32_t> &Names) {
13707ac1c780SAaron Smith     std::vector<uint32_t> ids;
13717ac1c780SAaron Smith     if (Names.GetValues(regex, ids)) {
13727ac1c780SAaron Smith       for (auto id : ids) {
13737ac1c780SAaron Smith         if (resolved_ids.find(id) == resolved_ids.end())
13747ac1c780SAaron Smith           if (ResolveFunction(id, include_inlines, sc_list))
13757ac1c780SAaron Smith             resolved_ids.insert(id);
13767ac1c780SAaron Smith       }
13777ac1c780SAaron Smith     }
13787ac1c780SAaron Smith   };
13797ac1c780SAaron Smith   ResolveFn(m_func_full_names);
13807ac1c780SAaron Smith   ResolveFn(m_func_base_names);
138174e08ca0SZachary Turner }
138274e08ca0SZachary Turner 
GetMangledNamesForFunction(const std::string & scope_qualified_name,std::vector<lldb_private::ConstString> & mangled_names)1383b9c1b51eSKate Stone void SymbolFilePDB::GetMangledNamesForFunction(
1384b9c1b51eSKate Stone     const std::string &scope_qualified_name,
1385b9c1b51eSKate Stone     std::vector<lldb_private::ConstString> &mangled_names) {}
138674e08ca0SZachary Turner 
AddSymbols(lldb_private::Symtab & symtab)13878cfb12b9SAleksandr Urakov void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
13888cfb12b9SAleksandr Urakov   std::set<lldb::addr_t> sym_addresses;
13898cfb12b9SAleksandr Urakov   for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
13908cfb12b9SAleksandr Urakov     sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
13918cfb12b9SAleksandr Urakov 
13928cfb12b9SAleksandr Urakov   auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
13938cfb12b9SAleksandr Urakov   if (!results)
13948cfb12b9SAleksandr Urakov     return;
13958cfb12b9SAleksandr Urakov 
1396d2deeb44SPavel Labath   auto section_list = m_objfile_sp->GetSectionList();
13978cfb12b9SAleksandr Urakov   if (!section_list)
13988cfb12b9SAleksandr Urakov     return;
13998cfb12b9SAleksandr Urakov 
14008cfb12b9SAleksandr Urakov   while (auto pub_symbol = results->getNext()) {
14017db8b5c4SPavel Labath     auto section_id = pub_symbol->getAddressSection();
14028cfb12b9SAleksandr Urakov 
14037db8b5c4SPavel Labath     auto section = section_list->FindSectionByID(section_id);
14048cfb12b9SAleksandr Urakov     if (!section)
14058cfb12b9SAleksandr Urakov       continue;
14068cfb12b9SAleksandr Urakov 
14078cfb12b9SAleksandr Urakov     auto offset = pub_symbol->getAddressOffset();
14088cfb12b9SAleksandr Urakov 
14098cfb12b9SAleksandr Urakov     auto file_addr = section->GetFileAddress() + offset;
14108cfb12b9SAleksandr Urakov     if (sym_addresses.find(file_addr) != sym_addresses.end())
14118cfb12b9SAleksandr Urakov       continue;
14128cfb12b9SAleksandr Urakov     sym_addresses.insert(file_addr);
14138cfb12b9SAleksandr Urakov 
14148cfb12b9SAleksandr Urakov     auto size = pub_symbol->getLength();
14158cfb12b9SAleksandr Urakov     symtab.AddSymbol(
14168cfb12b9SAleksandr Urakov         Symbol(pub_symbol->getSymIndexId(),   // symID
14178cfb12b9SAleksandr Urakov                pub_symbol->getName().c_str(), // name
14188cfb12b9SAleksandr Urakov                pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
14198cfb12b9SAleksandr Urakov                true,      // external
14208cfb12b9SAleksandr Urakov                false,     // is_debug
14218cfb12b9SAleksandr Urakov                false,     // is_trampoline
14228cfb12b9SAleksandr Urakov                false,     // is_artificial
14238cfb12b9SAleksandr Urakov                section,   // section_sp
14248cfb12b9SAleksandr Urakov                offset,    // value
14258cfb12b9SAleksandr Urakov                size,      // size
14268cfb12b9SAleksandr Urakov                size != 0, // size_is_valid
14278cfb12b9SAleksandr Urakov                false,     // contains_linker_annotations
14288cfb12b9SAleksandr Urakov                0          // flags
14298cfb12b9SAleksandr Urakov                ));
14308cfb12b9SAleksandr Urakov   }
14318cfb12b9SAleksandr Urakov 
14328cfb12b9SAleksandr Urakov   symtab.Finalize();
14338cfb12b9SAleksandr Urakov }
14348cfb12b9SAleksandr Urakov 
FindTypes(lldb_private::ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,llvm::DenseSet<lldb_private::SymbolFile * > & searched_symbol_files,lldb_private::TypeMap & types)1435796cd312SAdrian Prantl void SymbolFilePDB::FindTypes(
1436f9568a95SRaphael Isemann     lldb_private::ConstString name, const CompilerDeclContext &parent_decl_ctx,
1437b9c1b51eSKate Stone     uint32_t max_matches,
143874e08ca0SZachary Turner     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1439b9c1b51eSKate Stone     lldb_private::TypeMap &types) {
1440656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
144142dff790SZachary Turner   if (!name)
1442796cd312SAdrian Prantl     return;
14437ac1c780SAaron Smith   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1444796cd312SAdrian Prantl     return;
144542dff790SZachary Turner 
144642dff790SZachary Turner   searched_symbol_files.clear();
144742dff790SZachary Turner   searched_symbol_files.insert(this);
144842dff790SZachary Turner 
144986e9434dSAaron Smith   // There is an assumption 'name' is not a regex
1450c1e530eeSAleksandr Urakov   FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
145142dff790SZachary Turner }
145242dff790SZachary Turner 
DumpClangAST(Stream & s)14534911023fSZachary Turner void SymbolFilePDB::DumpClangAST(Stream &s) {
14540e252e38SAlex Langford   auto type_system_or_err =
14550e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
14560e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
1457a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
1458a007a6d8SPavel Labath                    "Unable to dump ClangAST");
14594911023fSZachary Turner     return;
14600e252e38SAlex Langford   }
14610e252e38SAlex Langford 
14620e252e38SAlex Langford   auto *clang_type_system =
14636e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
14640e252e38SAlex Langford   if (!clang_type_system)
14650e252e38SAlex Langford     return;
14669a57d1e5SRaphael Isemann   clang_type_system->Dump(s.AsRawOstream());
14674911023fSZachary Turner }
14684911023fSZachary Turner 
FindTypesByRegex(const lldb_private::RegularExpression & regex,uint32_t max_matches,lldb_private::TypeMap & types)1469c8316ed2SAaron Smith void SymbolFilePDB::FindTypesByRegex(
1470c8316ed2SAaron Smith     const lldb_private::RegularExpression &regex, uint32_t max_matches,
1471b9c1b51eSKate Stone     lldb_private::TypeMap &types) {
1472b9c1b51eSKate Stone   // When searching by regex, we need to go out of our way to limit the search
14739d0eb996SAdrian McCarthy   // space as much as possible since this searches EVERYTHING in the PDB,
14749d0eb996SAdrian McCarthy   // manually doing regex comparisons.  PDB library isn't optimized for regex
14759d0eb996SAdrian McCarthy   // searches or searches across multiple symbol types at the same time, so the
1476b9c1b51eSKate Stone   // best we can do is to search enums, then typedefs, then classes one by one,
14779d0eb996SAdrian McCarthy   // and do a regex comparison against each of them.
1478b9c1b51eSKate Stone   PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1479b9c1b51eSKate Stone                                   PDB_SymType::UDT};
148054fd7ff6SZachary Turner   std::unique_ptr<IPDBEnumSymbols> results;
148142dff790SZachary Turner 
148242dff790SZachary Turner   uint32_t matches = 0;
148342dff790SZachary Turner 
1484b9c1b51eSKate Stone   for (auto tag : tags_to_search) {
148510a02577SAaron Smith     results = m_global_scope_up->findAllChildren(tag);
148610a02577SAaron Smith     if (!results)
148710a02577SAaron Smith       continue;
148810a02577SAaron Smith 
1489b9c1b51eSKate Stone     while (auto result = results->getNext()) {
149042dff790SZachary Turner       if (max_matches > 0 && matches >= max_matches)
149142dff790SZachary Turner         break;
149242dff790SZachary Turner 
149342dff790SZachary Turner       std::string type_name;
149454fd7ff6SZachary Turner       if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
149542dff790SZachary Turner         type_name = enum_type->getName();
1496b9c1b51eSKate Stone       else if (auto typedef_type =
1497b9c1b51eSKate Stone                    llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
149842dff790SZachary Turner         type_name = typedef_type->getName();
149954fd7ff6SZachary Turner       else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
150042dff790SZachary Turner         type_name = class_type->getName();
1501b9c1b51eSKate Stone       else {
15029d0eb996SAdrian McCarthy         // We're looking only for types that have names.  Skip symbols, as well
15039d0eb996SAdrian McCarthy         // as unnamed types such as arrays, pointers, etc.
150442dff790SZachary Turner         continue;
150542dff790SZachary Turner       }
150642dff790SZachary Turner 
150786e9434dSAaron Smith       if (!regex.Execute(type_name))
150842dff790SZachary Turner         continue;
150942dff790SZachary Turner 
1510b9c1b51eSKate Stone       // This should cause the type to get cached and stored in the `m_types`
1511b9c1b51eSKate Stone       // lookup.
151242dff790SZachary Turner       if (!ResolveTypeUID(result->getSymIndexId()))
151342dff790SZachary Turner         continue;
151442dff790SZachary Turner 
151542dff790SZachary Turner       auto iter = m_types.find(result->getSymIndexId());
151642dff790SZachary Turner       if (iter == m_types.end())
151742dff790SZachary Turner         continue;
151842dff790SZachary Turner       types.Insert(iter->second);
151942dff790SZachary Turner       ++matches;
152042dff790SZachary Turner     }
152142dff790SZachary Turner   }
152242dff790SZachary Turner }
152342dff790SZachary Turner 
FindTypesByName(llvm::StringRef name,const lldb_private::CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,lldb_private::TypeMap & types)1524709426b3SAleksandr Urakov void SymbolFilePDB::FindTypesByName(
1525c1e530eeSAleksandr Urakov     llvm::StringRef name,
1526f9568a95SRaphael Isemann     const lldb_private::CompilerDeclContext &parent_decl_ctx,
1527709426b3SAleksandr Urakov     uint32_t max_matches, lldb_private::TypeMap &types) {
152854fd7ff6SZachary Turner   std::unique_ptr<IPDBEnumSymbols> results;
1529f76fe682SAaron Smith   if (name.empty())
1530f76fe682SAaron Smith     return;
1531709426b3SAleksandr Urakov   results = m_global_scope_up->findAllChildren(PDB_SymType::None);
153210a02577SAaron Smith   if (!results)
153310a02577SAaron Smith     return;
153442dff790SZachary Turner 
153542dff790SZachary Turner   uint32_t matches = 0;
153642dff790SZachary Turner 
1537b9c1b51eSKate Stone   while (auto result = results->getNext()) {
153842dff790SZachary Turner     if (max_matches > 0 && matches >= max_matches)
153942dff790SZachary Turner       break;
1540709426b3SAleksandr Urakov 
1541c1e530eeSAleksandr Urakov     if (MSVCUndecoratedNameParser::DropScope(
1542c1e530eeSAleksandr Urakov             result->getRawSymbol().getName()) != name)
1543709426b3SAleksandr Urakov       continue;
1544709426b3SAleksandr Urakov 
1545b9c1b51eSKate Stone     switch (result->getSymTag()) {
154654fd7ff6SZachary Turner     case PDB_SymType::Enum:
154754fd7ff6SZachary Turner     case PDB_SymType::UDT:
154854fd7ff6SZachary Turner     case PDB_SymType::Typedef:
154942dff790SZachary Turner       break;
155042dff790SZachary Turner     default:
155105097246SAdrian Prantl       // We're looking only for types that have names.  Skip symbols, as well
155205097246SAdrian Prantl       // as unnamed types such as arrays, pointers, etc.
155342dff790SZachary Turner       continue;
155442dff790SZachary Turner     }
155542dff790SZachary Turner 
1556b9c1b51eSKate Stone     // This should cause the type to get cached and stored in the `m_types`
1557b9c1b51eSKate Stone     // lookup.
155842dff790SZachary Turner     if (!ResolveTypeUID(result->getSymIndexId()))
155942dff790SZachary Turner       continue;
156042dff790SZachary Turner 
1561f9568a95SRaphael Isemann     if (parent_decl_ctx.IsValid() &&
1562f9568a95SRaphael Isemann         GetDeclContextContainingUID(result->getSymIndexId()) != parent_decl_ctx)
1563709426b3SAleksandr Urakov       continue;
1564709426b3SAleksandr Urakov 
156542dff790SZachary Turner     auto iter = m_types.find(result->getSymIndexId());
156642dff790SZachary Turner     if (iter == m_types.end())
156742dff790SZachary Turner       continue;
156842dff790SZachary Turner     types.Insert(iter->second);
156942dff790SZachary Turner     ++matches;
157042dff790SZachary Turner   }
157174e08ca0SZachary Turner }
157274e08ca0SZachary Turner 
FindTypes(llvm::ArrayRef<CompilerContext> pattern,LanguageSet languages,llvm::DenseSet<SymbolFile * > & searched_symbol_files,lldb_private::TypeMap & types)15733b73dcdcSAdrian Prantl void SymbolFilePDB::FindTypes(
15743b73dcdcSAdrian Prantl     llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
15753b73dcdcSAdrian Prantl     llvm::DenseSet<SymbolFile *> &searched_symbol_files,
1576796cd312SAdrian Prantl     lldb_private::TypeMap &types) {}
157774e08ca0SZachary Turner 
GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol & pdb_symbol,uint32_t type_mask,TypeCollection & type_collection)1578c8316ed2SAaron Smith void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
15797ac1c780SAaron Smith                                          uint32_t type_mask,
15807ac1c780SAaron Smith                                          TypeCollection &type_collection) {
15817ac1c780SAaron Smith   bool can_parse = false;
1582e664b5dcSAaron Smith   switch (pdb_symbol.getSymTag()) {
15837ac1c780SAaron Smith   case PDB_SymType::ArrayType:
15847ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassArray) != 0);
15857ac1c780SAaron Smith     break;
15867ac1c780SAaron Smith   case PDB_SymType::BuiltinType:
15877ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassBuiltin) != 0);
15887ac1c780SAaron Smith     break;
15897ac1c780SAaron Smith   case PDB_SymType::Enum:
15907ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassEnumeration) != 0);
15917ac1c780SAaron Smith     break;
15927ac1c780SAaron Smith   case PDB_SymType::Function:
15937ac1c780SAaron Smith   case PDB_SymType::FunctionSig:
15947ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassFunction) != 0);
15957ac1c780SAaron Smith     break;
15967ac1c780SAaron Smith   case PDB_SymType::PointerType:
15977ac1c780SAaron Smith     can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
15987ac1c780SAaron Smith                                eTypeClassMemberPointer)) != 0);
15997ac1c780SAaron Smith     break;
16007ac1c780SAaron Smith   case PDB_SymType::Typedef:
16017ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassTypedef) != 0);
16027ac1c780SAaron Smith     break;
16037ac1c780SAaron Smith   case PDB_SymType::UDT: {
1604e664b5dcSAaron Smith     auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
16057ac1c780SAaron Smith     assert(udt);
16067ac1c780SAaron Smith     can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
16077ac1c780SAaron Smith                  ((type_mask & (eTypeClassClass | eTypeClassStruct |
16087ac1c780SAaron Smith                                 eTypeClassUnion)) != 0));
16097ac1c780SAaron Smith   } break;
1610c8316ed2SAaron Smith   default:
1611c8316ed2SAaron Smith     break;
16127ac1c780SAaron Smith   }
16137ac1c780SAaron Smith 
16147ac1c780SAaron Smith   if (can_parse) {
1615e664b5dcSAaron Smith     if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
16167ac1c780SAaron Smith       auto result =
16177ac1c780SAaron Smith           std::find(type_collection.begin(), type_collection.end(), type);
16187ac1c780SAaron Smith       if (result == type_collection.end())
16197ac1c780SAaron Smith         type_collection.push_back(type);
16207ac1c780SAaron Smith     }
16217ac1c780SAaron Smith   }
16227ac1c780SAaron Smith 
1623e664b5dcSAaron Smith   auto results_up = pdb_symbol.findAllChildren();
16247ac1c780SAaron Smith   while (auto symbol_up = results_up->getNext())
1625e664b5dcSAaron Smith     GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
16267ac1c780SAaron Smith }
16277ac1c780SAaron Smith 
GetTypes(lldb_private::SymbolContextScope * sc_scope,TypeClass type_mask,lldb_private::TypeList & type_list)1628796cd312SAdrian Prantl void SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1629117b1fa1SZachary Turner                              TypeClass type_mask,
1630b9c1b51eSKate Stone                              lldb_private::TypeList &type_list) {
1631656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
16327ac1c780SAaron Smith   TypeCollection type_collection;
1633c8316ed2SAaron Smith   CompileUnit *cu =
1634c8316ed2SAaron Smith       sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
16357ac1c780SAaron Smith   if (cu) {
16367ac1c780SAaron Smith     auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1637e664b5dcSAaron Smith     if (!compiland_up)
1638796cd312SAdrian Prantl       return;
1639e664b5dcSAaron Smith     GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
16407ac1c780SAaron Smith   } else {
16417ac1c780SAaron Smith     for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
16427ac1c780SAaron Smith       auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1643d5a925f4SAaron Smith       if (cu_sp) {
1644e664b5dcSAaron Smith         if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1645e664b5dcSAaron Smith           GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
16467ac1c780SAaron Smith       }
16477ac1c780SAaron Smith     }
16487ac1c780SAaron Smith   }
16497ac1c780SAaron Smith 
16507ac1c780SAaron Smith   for (auto type : type_collection) {
16517ac1c780SAaron Smith     type->GetForwardCompilerType();
16527ac1c780SAaron Smith     type_list.Insert(type->shared_from_this());
16537ac1c780SAaron Smith   }
165474e08ca0SZachary Turner }
165574e08ca0SZachary Turner 
16560e252e38SAlex Langford llvm::Expected<lldb_private::TypeSystem &>
GetTypeSystemForLanguage(lldb::LanguageType language)1657b9c1b51eSKate Stone SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
16580e252e38SAlex Langford   auto type_system_or_err =
1659d2deeb44SPavel Labath       m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
16600e252e38SAlex Langford   if (type_system_or_err) {
16610e252e38SAlex Langford     type_system_or_err->SetSymbolFile(this);
16620e252e38SAlex Langford   }
16630e252e38SAlex Langford   return type_system_or_err;
166474e08ca0SZachary Turner }
166574e08ca0SZachary Turner 
GetPDBAstParser()1666c68925abSZachary Turner PDBASTParser *SymbolFilePDB::GetPDBAstParser() {
16670e252e38SAlex Langford   auto type_system_or_err =
16680e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
16690e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
1670a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
1671a007a6d8SPavel Labath                    "Unable to get PDB AST parser");
16720e252e38SAlex Langford     return nullptr;
16730e252e38SAlex Langford   }
16740e252e38SAlex Langford 
16750e252e38SAlex Langford   auto *clang_type_system =
16766e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
1677c68925abSZachary Turner   if (!clang_type_system)
1678c68925abSZachary Turner     return nullptr;
1679c68925abSZachary Turner 
1680c68925abSZachary Turner   return clang_type_system->GetPDBParser();
1681c68925abSZachary Turner }
1682c68925abSZachary Turner 
1683f9568a95SRaphael Isemann lldb_private::CompilerDeclContext
FindNamespace(lldb_private::ConstString name,const CompilerDeclContext & parent_decl_ctx)1684f9568a95SRaphael Isemann SymbolFilePDB::FindNamespace(lldb_private::ConstString name,
1685f9568a95SRaphael Isemann                              const CompilerDeclContext &parent_decl_ctx) {
1686656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
16870e252e38SAlex Langford   auto type_system_or_err =
16880e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
16890e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
1690a007a6d8SPavel Labath     LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
1691a007a6d8SPavel Labath                    "Unable to find namespace {}", name.AsCString());
16920e252e38SAlex Langford     return CompilerDeclContext();
16930e252e38SAlex Langford   }
16940e252e38SAlex Langford 
16950e252e38SAlex Langford   auto *clang_type_system =
16966e3b0cc2SRaphael Isemann       llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
1697709426b3SAleksandr Urakov   if (!clang_type_system)
1698709426b3SAleksandr Urakov     return CompilerDeclContext();
1699709426b3SAleksandr Urakov 
1700709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_type_system->GetPDBParser();
1701709426b3SAleksandr Urakov   if (!pdb)
1702709426b3SAleksandr Urakov     return CompilerDeclContext();
1703709426b3SAleksandr Urakov 
1704709426b3SAleksandr Urakov   clang::DeclContext *decl_context = nullptr;
1705709426b3SAleksandr Urakov   if (parent_decl_ctx)
1706709426b3SAleksandr Urakov     decl_context = static_cast<clang::DeclContext *>(
1707f9568a95SRaphael Isemann         parent_decl_ctx.GetOpaqueDeclContext());
1708709426b3SAleksandr Urakov 
1709709426b3SAleksandr Urakov   auto namespace_decl =
1710709426b3SAleksandr Urakov       pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1711709426b3SAleksandr Urakov   if (!namespace_decl)
1712709426b3SAleksandr Urakov     return CompilerDeclContext();
1713709426b3SAleksandr Urakov 
171442ec584aSRaphael Isemann   return clang_type_system->CreateDeclContext(namespace_decl);
171574e08ca0SZachary Turner }
171674e08ca0SZachary Turner 
GetPDBSession()1717b9c1b51eSKate Stone IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1718b9c1b51eSKate Stone 
GetPDBSession() const1719b9c1b51eSKate Stone const IPDBSession &SymbolFilePDB::GetPDBSession() const {
172042dff790SZachary Turner   return *m_session_up;
172142dff790SZachary Turner }
172242dff790SZachary Turner 
ParseCompileUnitForUID(uint32_t id,uint32_t index)1723c8316ed2SAaron Smith lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1724c8316ed2SAaron Smith                                                        uint32_t index) {
172574e08ca0SZachary Turner   auto found_cu = m_comp_units.find(id);
172674e08ca0SZachary Turner   if (found_cu != m_comp_units.end())
172774e08ca0SZachary Turner     return found_cu->second;
172874e08ca0SZachary Turner 
172910a02577SAaron Smith   auto compiland_up = GetPDBCompilandByUID(id);
173010a02577SAaron Smith   if (!compiland_up)
173110a02577SAaron Smith     return CompUnitSP();
173274e08ca0SZachary Turner 
173374e08ca0SZachary Turner   lldb::LanguageType lang;
173410a02577SAaron Smith   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
173574e08ca0SZachary Turner   if (!details)
173674e08ca0SZachary Turner     lang = lldb::eLanguageTypeC_plus_plus;
173774e08ca0SZachary Turner   else
173874e08ca0SZachary Turner     lang = TranslateLanguage(details->getLanguage());
173974e08ca0SZachary Turner 
1740f76fe682SAaron Smith   if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1741f76fe682SAaron Smith     return CompUnitSP();
1742f76fe682SAaron Smith 
1743487b0c6bSAaron Smith   std::string path = compiland_up->getSourceFileFullPath();
1744f76fe682SAaron Smith   if (path.empty())
1745f76fe682SAaron Smith     return CompUnitSP();
1746f76fe682SAaron Smith 
1747b9c1b51eSKate Stone   // Don't support optimized code for now, DebugInfoPDB does not return this
1748b9c1b51eSKate Stone   // information.
1749ad2b63cbSGreg Clayton   LazyBool optimized = eLazyBoolNo;
1750d2deeb44SPavel Labath   auto cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
1751c8316ed2SAaron Smith                                              path.c_str(), id, lang, optimized);
175210a02577SAaron Smith 
175310a02577SAaron Smith   if (!cu_sp)
175410a02577SAaron Smith     return CompUnitSP();
175510a02577SAaron Smith 
175610a02577SAaron Smith   m_comp_units.insert(std::make_pair(id, cu_sp));
175710a02577SAaron Smith   if (index == UINT32_MAX)
1758e664b5dcSAaron Smith     GetCompileUnitIndex(*compiland_up, index);
175910a02577SAaron Smith   lldbassert(index != UINT32_MAX);
1760e0119909SPavel Labath   SetCompileUnitAtIndex(index, cu_sp);
176110a02577SAaron Smith   return cu_sp;
176274e08ca0SZachary Turner }
176374e08ca0SZachary Turner 
ParseCompileUnitLineTable(CompileUnit & comp_unit,uint32_t match_line)1764863f8c18SZachary Turner bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit,
1765863f8c18SZachary Turner                                               uint32_t match_line) {
1766863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
176710a02577SAaron Smith   if (!compiland_up)
176810a02577SAaron Smith     return false;
176974e08ca0SZachary Turner 
1770b9c1b51eSKate Stone   // LineEntry needs the *index* of the file into the list of support files
17719d0eb996SAdrian McCarthy   // returned by ParseCompileUnitSupportFiles.  But the underlying SDK gives us
177205097246SAdrian Prantl   // a globally unique idenfitifier in the namespace of the PDB.  So, we have
177305097246SAdrian Prantl   // to do a mapping so that we can hand out indices.
177442dff790SZachary Turner   llvm::DenseMap<uint32_t, uint32_t> index_map;
177510a02577SAaron Smith   BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
1776c4267b7bSPavel Labath   auto line_table = std::make_unique<LineTable>(&comp_unit);
177774e08ca0SZachary Turner 
177810a02577SAaron Smith   // Find contributions to `compiland` from all source and header files.
177910a02577SAaron Smith   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
178010a02577SAaron Smith   if (!files)
178110a02577SAaron Smith     return false;
178274e08ca0SZachary Turner 
178305097246SAdrian Prantl   // For each source and header file, create a LineSequence for contributions
178405097246SAdrian Prantl   // to the compiland from that file, and add the sequence.
1785b9c1b51eSKate Stone   while (auto file = files->getNext()) {
1786c4267b7bSPavel Labath     std::unique_ptr<LineSequence> sequence(
1787c4267b7bSPavel Labath         line_table->CreateLineSequenceContainer());
178810a02577SAaron Smith     auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
178910a02577SAaron Smith     if (!lines)
179010a02577SAaron Smith       continue;
179174e08ca0SZachary Turner     int entry_count = lines->getChildCount();
179274e08ca0SZachary Turner 
17937e8c7beaSZachary Turner     uint64_t prev_addr;
17947e8c7beaSZachary Turner     uint32_t prev_length;
17957e8c7beaSZachary Turner     uint32_t prev_line;
17967e8c7beaSZachary Turner     uint32_t prev_source_idx;
17977e8c7beaSZachary Turner 
1798b9c1b51eSKate Stone     for (int i = 0; i < entry_count; ++i) {
179974e08ca0SZachary Turner       auto line = lines->getChildAtIndex(i);
180074e08ca0SZachary Turner 
18017e8c7beaSZachary Turner       uint64_t lno = line->getLineNumber();
18027e8c7beaSZachary Turner       uint64_t addr = line->getVirtualAddress();
18037e8c7beaSZachary Turner       uint32_t length = line->getLength();
180474e08ca0SZachary Turner       uint32_t source_id = line->getSourceFileId();
18057e8c7beaSZachary Turner       uint32_t col = line->getColumnNumber();
180674e08ca0SZachary Turner       uint32_t source_idx = index_map[source_id];
180774e08ca0SZachary Turner 
180805097246SAdrian Prantl       // There was a gap between the current entry and the previous entry if
180905097246SAdrian Prantl       // the addresses don't perfectly line up.
18107e8c7beaSZachary Turner       bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
18117e8c7beaSZachary Turner 
1812b9c1b51eSKate Stone       // Before inserting the current entry, insert a terminal entry at the end
18139d0eb996SAdrian McCarthy       // of the previous entry's address range if the current entry resulted in
18149d0eb996SAdrian McCarthy       // a gap from the previous entry.
1815b9c1b51eSKate Stone       if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1816c4267b7bSPavel Labath         line_table->AppendLineEntryToSequence(
1817b9c1b51eSKate Stone             sequence.get(), prev_addr + prev_length, prev_line, 0,
18187e8c7beaSZachary Turner             prev_source_idx, false, false, false, false, true);
1819010edd37SAaron Smith 
1820e3dd82aeSRaphael Isemann         line_table->InsertSequence(sequence.get());
1821c4267b7bSPavel Labath         sequence = line_table->CreateLineSequenceContainer();
18227e8c7beaSZachary Turner       }
18237e8c7beaSZachary Turner 
1824b9c1b51eSKate Stone       if (ShouldAddLine(match_line, lno, length)) {
18257e8c7beaSZachary Turner         bool is_statement = line->isStatement();
182674e08ca0SZachary Turner         bool is_prologue = false;
182774e08ca0SZachary Turner         bool is_epilogue = false;
1828b9c1b51eSKate Stone         auto func =
1829b9c1b51eSKate Stone             m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1830b9c1b51eSKate Stone         if (func) {
183154fd7ff6SZachary Turner           auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
183210a02577SAaron Smith           if (prologue)
18337e8c7beaSZachary Turner             is_prologue = (addr == prologue->getVirtualAddress());
183474e08ca0SZachary Turner 
183554fd7ff6SZachary Turner           auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
183610a02577SAaron Smith           if (epilogue)
18377e8c7beaSZachary Turner             is_epilogue = (addr == epilogue->getVirtualAddress());
18387e8c7beaSZachary Turner         }
183974e08ca0SZachary Turner 
1840c4267b7bSPavel Labath         line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1841b9c1b51eSKate Stone                                               source_idx, is_statement, false,
18427e8c7beaSZachary Turner                                               is_prologue, is_epilogue, false);
18437e8c7beaSZachary Turner       }
18447e8c7beaSZachary Turner 
18457e8c7beaSZachary Turner       prev_addr = addr;
18467e8c7beaSZachary Turner       prev_length = length;
18477e8c7beaSZachary Turner       prev_line = lno;
18487e8c7beaSZachary Turner       prev_source_idx = source_idx;
18497e8c7beaSZachary Turner     }
18507e8c7beaSZachary Turner 
1851b9c1b51eSKate Stone     if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
18527e8c7beaSZachary Turner       // The end is always a terminal entry, so insert it regardless.
1853c4267b7bSPavel Labath       line_table->AppendLineEntryToSequence(
1854b9c1b51eSKate Stone           sequence.get(), prev_addr + prev_length, prev_line, 0,
18557e8c7beaSZachary Turner           prev_source_idx, false, false, false, false, true);
185674e08ca0SZachary Turner     }
185774e08ca0SZachary Turner 
1858c4267b7bSPavel Labath     line_table->InsertSequence(sequence.get());
185974e08ca0SZachary Turner   }
186074e08ca0SZachary Turner 
186110a02577SAaron Smith   if (line_table->GetSize()) {
1862863f8c18SZachary Turner     comp_unit.SetLineTable(line_table.release());
186374e08ca0SZachary Turner     return true;
186474e08ca0SZachary Turner   }
186510a02577SAaron Smith   return false;
186610a02577SAaron Smith }
186774e08ca0SZachary Turner 
BuildSupportFileIdToSupportFileIndexMap(const PDBSymbolCompiland & compiland,llvm::DenseMap<uint32_t,uint32_t> & index_map) const1868b9c1b51eSKate Stone void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
186910a02577SAaron Smith     const PDBSymbolCompiland &compiland,
1870b9c1b51eSKate Stone     llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
187105097246SAdrian Prantl   // This is a hack, but we need to convert the source id into an index into
187205097246SAdrian Prantl   // the support files array.  We don't want to do path comparisons to avoid
18739d0eb996SAdrian McCarthy   // basename / full path issues that may or may not even be a problem, so we
18749d0eb996SAdrian McCarthy   // use the globally unique source file identifiers.  Ideally we could use the
18759d0eb996SAdrian McCarthy   // global identifiers everywhere, but LineEntry currently assumes indices.
187610a02577SAaron Smith   auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
187710a02577SAaron Smith   if (!source_files)
187810a02577SAaron Smith     return;
18799ea80d25SPavel Labath 
188057f8a998SPavel Labath   int index = 0;
1881b9c1b51eSKate Stone   while (auto file = source_files->getNext()) {
188274e08ca0SZachary Turner     uint32_t source_id = file->getUniqueId();
188374e08ca0SZachary Turner     index_map[source_id] = index++;
188474e08ca0SZachary Turner   }
188574e08ca0SZachary Turner }
18867ac1c780SAaron Smith 
GetCompileUnitContainsAddress(const lldb_private::Address & so_addr)18877ac1c780SAaron Smith lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
18887ac1c780SAaron Smith     const lldb_private::Address &so_addr) {
18897ac1c780SAaron Smith   lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1890308e39caSAaron Smith   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
18917ac1c780SAaron Smith     return nullptr;
18927ac1c780SAaron Smith 
1893308e39caSAaron Smith   // If it is a PDB function's vm addr, this is the first sure bet.
1894308e39caSAaron Smith   if (auto lines =
1895308e39caSAaron Smith           m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1896308e39caSAaron Smith     if (auto first_line = lines->getNext())
1897308e39caSAaron Smith       return ParseCompileUnitForUID(first_line->getCompilandId());
18987ac1c780SAaron Smith   }
18997ac1c780SAaron Smith 
1900308e39caSAaron Smith   // Otherwise we resort to section contributions.
1901308e39caSAaron Smith   if (auto sec_contribs = m_session_up->getSectionContribs()) {
1902308e39caSAaron Smith     while (auto section = sec_contribs->getNext()) {
1903308e39caSAaron Smith       auto va = section->getVirtualAddress();
1904308e39caSAaron Smith       if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1905308e39caSAaron Smith         return ParseCompileUnitForUID(section->getCompilandId());
1906308e39caSAaron Smith     }
1907308e39caSAaron Smith   }
19087ac1c780SAaron Smith   return nullptr;
19097ac1c780SAaron Smith }
19107ac1c780SAaron Smith 
19117ac1c780SAaron Smith Mangled
GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc & pdb_func)1912e664b5dcSAaron Smith SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
19137ac1c780SAaron Smith   Mangled mangled;
1914e664b5dcSAaron Smith   auto func_name = pdb_func.getName();
1915e664b5dcSAaron Smith   auto func_undecorated_name = pdb_func.getUndecoratedName();
19167ac1c780SAaron Smith   std::string func_decorated_name;
19177ac1c780SAaron Smith 
19187ac1c780SAaron Smith   // Seek from public symbols for non-static function's decorated name if any.
19197ac1c780SAaron Smith   // For static functions, they don't have undecorated names and aren't exposed
19207ac1c780SAaron Smith   // in Public Symbols either.
19217ac1c780SAaron Smith   if (!func_undecorated_name.empty()) {
1922c8316ed2SAaron Smith     auto result_up = m_global_scope_up->findChildren(
1923c8316ed2SAaron Smith         PDB_SymType::PublicSymbol, func_undecorated_name,
19247ac1c780SAaron Smith         PDB_NameSearchFlags::NS_UndecoratedName);
19257ac1c780SAaron Smith     if (result_up) {
19267ac1c780SAaron Smith       while (auto symbol_up = result_up->getNext()) {
19277ac1c780SAaron Smith         // For a public symbol, it is unique.
19287ac1c780SAaron Smith         lldbassert(result_up->getChildCount() == 1);
19297ac1c780SAaron Smith         if (auto *pdb_public_sym =
1930c8316ed2SAaron Smith                 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1931c8316ed2SAaron Smith                     symbol_up.get())) {
19327ac1c780SAaron Smith           if (pdb_public_sym->isFunction()) {
19337ac1c780SAaron Smith             func_decorated_name = pdb_public_sym->getName();
1934f76fe682SAaron Smith             break;
19357ac1c780SAaron Smith           }
19367ac1c780SAaron Smith         }
19377ac1c780SAaron Smith       }
19387ac1c780SAaron Smith     }
19397ac1c780SAaron Smith   }
19407ac1c780SAaron Smith   if (!func_decorated_name.empty()) {
19417ac1c780SAaron Smith     mangled.SetMangledName(ConstString(func_decorated_name));
19427ac1c780SAaron Smith 
19437ac1c780SAaron Smith     // For MSVC, format of C funciton's decorated name depends on calling
1944e9264b74SKazuaki Ishizaki     // convention. Unfortunately none of the format is recognized by current
19457ac1c780SAaron Smith     // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
194605097246SAdrian Prantl     // `__purecall` is retrieved as both its decorated and undecorated name
194705097246SAdrian Prantl     // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
194805097246SAdrian Prantl     // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
194905097246SAdrian Prantl     // Mangled::GetDemangledName method will fail internally and caches an
1950e9264b74SKazuaki Ishizaki     // empty string as its undecorated name. So we will face a contradiction
195105097246SAdrian Prantl     // here for the same symbol:
19527ac1c780SAaron Smith     //   non-empty undecorated name from PDB
19537ac1c780SAaron Smith     //   empty undecorated name from LLDB
195422b04487SAlex Langford     if (!func_undecorated_name.empty() && mangled.GetDemangledName().IsEmpty())
19557ac1c780SAaron Smith       mangled.SetDemangledName(ConstString(func_undecorated_name));
19567ac1c780SAaron Smith 
19577ac1c780SAaron Smith     // LLDB uses several flags to control how a C++ decorated name is
195805097246SAdrian Prantl     // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
195905097246SAdrian Prantl     // yielded name could be different from what we retrieve from
19607ac1c780SAaron Smith     // PDB source unless we also apply same flags in getting undecorated
19617ac1c780SAaron Smith     // name through PDBSymbolFunc::getUndecoratedNameEx method.
19627ac1c780SAaron Smith     if (!func_undecorated_name.empty() &&
196322b04487SAlex Langford         mangled.GetDemangledName() != ConstString(func_undecorated_name))
19647ac1c780SAaron Smith       mangled.SetDemangledName(ConstString(func_undecorated_name));
19657ac1c780SAaron Smith   } else if (!func_undecorated_name.empty()) {
19667ac1c780SAaron Smith     mangled.SetDemangledName(ConstString(func_undecorated_name));
19677ac1c780SAaron Smith   } else if (!func_name.empty())
19687ac1c780SAaron Smith     mangled.SetValue(ConstString(func_name), false);
19697ac1c780SAaron Smith 
19707ac1c780SAaron Smith   return mangled;
19717ac1c780SAaron Smith }
19727ac1c780SAaron Smith 
DeclContextMatchesThisSymbolFile(const lldb_private::CompilerDeclContext & decl_ctx)19737ac1c780SAaron Smith bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1974f9568a95SRaphael Isemann     const lldb_private::CompilerDeclContext &decl_ctx) {
1975f9568a95SRaphael Isemann   if (!decl_ctx.IsValid())
19767ac1c780SAaron Smith     return true;
19777ac1c780SAaron Smith 
1978f9568a95SRaphael Isemann   TypeSystem *decl_ctx_type_system = decl_ctx.GetTypeSystem();
19797ac1c780SAaron Smith   if (!decl_ctx_type_system)
19807ac1c780SAaron Smith     return false;
19810e252e38SAlex Langford   auto type_system_or_err = GetTypeSystemForLanguage(
19827ac1c780SAaron Smith       decl_ctx_type_system->GetMinimumLanguage(nullptr));
19830e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
19840e252e38SAlex Langford     LLDB_LOG_ERROR(
1985a007a6d8SPavel Labath         GetLog(LLDBLog::Symbols), std::move(err),
19860e252e38SAlex Langford         "Unable to determine if DeclContext matches this symbol file");
19870e252e38SAlex Langford     return false;
19880e252e38SAlex Langford   }
19890e252e38SAlex Langford 
19900e252e38SAlex Langford   if (decl_ctx_type_system == &type_system_or_err.get())
19917ac1c780SAaron Smith     return true; // The type systems match, return true
19927ac1c780SAaron Smith 
19937ac1c780SAaron Smith   return false;
19947ac1c780SAaron Smith }
1995356aa4a9SAleksandr Urakov 
GetCompilandId(const llvm::pdb::PDBSymbolData & data)1996356aa4a9SAleksandr Urakov uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1997356aa4a9SAleksandr Urakov   static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1998356aa4a9SAleksandr Urakov     return lhs < rhs.Offset;
1999356aa4a9SAleksandr Urakov   };
2000356aa4a9SAleksandr Urakov 
2001356aa4a9SAleksandr Urakov   // Cache section contributions
2002356aa4a9SAleksandr Urakov   if (m_sec_contribs.empty()) {
2003356aa4a9SAleksandr Urakov     if (auto SecContribs = m_session_up->getSectionContribs()) {
2004356aa4a9SAleksandr Urakov       while (auto SectionContrib = SecContribs->getNext()) {
2005356aa4a9SAleksandr Urakov         auto comp_id = SectionContrib->getCompilandId();
2006356aa4a9SAleksandr Urakov         if (!comp_id)
2007356aa4a9SAleksandr Urakov           continue;
2008356aa4a9SAleksandr Urakov 
2009356aa4a9SAleksandr Urakov         auto sec = SectionContrib->getAddressSection();
2010356aa4a9SAleksandr Urakov         auto &sec_cs = m_sec_contribs[sec];
2011356aa4a9SAleksandr Urakov 
2012356aa4a9SAleksandr Urakov         auto offset = SectionContrib->getAddressOffset();
2013356aa4a9SAleksandr Urakov         auto it =
2014356aa4a9SAleksandr Urakov             std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
2015356aa4a9SAleksandr Urakov 
2016356aa4a9SAleksandr Urakov         auto size = SectionContrib->getLength();
2017356aa4a9SAleksandr Urakov         sec_cs.insert(it, {offset, size, comp_id});
2018356aa4a9SAleksandr Urakov       }
2019356aa4a9SAleksandr Urakov     }
2020356aa4a9SAleksandr Urakov   }
2021356aa4a9SAleksandr Urakov 
2022356aa4a9SAleksandr Urakov   // Check by line number
2023356aa4a9SAleksandr Urakov   if (auto Lines = data.getLineNumbers()) {
2024356aa4a9SAleksandr Urakov     if (auto FirstLine = Lines->getNext())
2025356aa4a9SAleksandr Urakov       return FirstLine->getCompilandId();
2026356aa4a9SAleksandr Urakov   }
2027356aa4a9SAleksandr Urakov 
2028356aa4a9SAleksandr Urakov   // Retrieve section + offset
2029356aa4a9SAleksandr Urakov   uint32_t DataSection = data.getAddressSection();
2030356aa4a9SAleksandr Urakov   uint32_t DataOffset = data.getAddressOffset();
2031356aa4a9SAleksandr Urakov   if (DataSection == 0) {
2032356aa4a9SAleksandr Urakov     if (auto RVA = data.getRelativeVirtualAddress())
2033356aa4a9SAleksandr Urakov       m_session_up->addressForRVA(RVA, DataSection, DataOffset);
2034356aa4a9SAleksandr Urakov   }
2035356aa4a9SAleksandr Urakov 
2036356aa4a9SAleksandr Urakov   if (DataSection) {
2037356aa4a9SAleksandr Urakov     // Search by section contributions
2038356aa4a9SAleksandr Urakov     auto &sec_cs = m_sec_contribs[DataSection];
2039356aa4a9SAleksandr Urakov     auto it =
2040356aa4a9SAleksandr Urakov         std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
2041356aa4a9SAleksandr Urakov     if (it != sec_cs.begin()) {
2042356aa4a9SAleksandr Urakov       --it;
2043356aa4a9SAleksandr Urakov       if (DataOffset < it->Offset + it->Size)
2044356aa4a9SAleksandr Urakov         return it->CompilandId;
2045356aa4a9SAleksandr Urakov     }
2046356aa4a9SAleksandr Urakov   } else {
2047356aa4a9SAleksandr Urakov     // Search in lexical tree
2048356aa4a9SAleksandr Urakov     auto LexParentId = data.getLexicalParentId();
2049356aa4a9SAleksandr Urakov     while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
2050356aa4a9SAleksandr Urakov       if (LexParent->getSymTag() == PDB_SymType::Exe)
2051356aa4a9SAleksandr Urakov         break;
2052356aa4a9SAleksandr Urakov       if (LexParent->getSymTag() == PDB_SymType::Compiland)
2053356aa4a9SAleksandr Urakov         return LexParentId;
2054356aa4a9SAleksandr Urakov       LexParentId = LexParent->getRawSymbol().getLexicalParentId();
2055356aa4a9SAleksandr Urakov     }
2056356aa4a9SAleksandr Urakov   }
2057356aa4a9SAleksandr Urakov 
2058356aa4a9SAleksandr Urakov   return 0;
2059356aa4a9SAleksandr Urakov }
2060