diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp index 71a77a4ed7458..28e7cceb39715 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp @@ -3633,8 +3633,7 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes( static_cast( SymbolFileDWARF::GetDWARFParser(*dst_class_die.GetCU())); auto link = [&](DWARFDIE src, DWARFDIE dst) { - SymbolFileDWARF::DIEToTypePtr &die_to_type = - dst_class_die.GetDWARF()->GetDIEToType(); + auto &die_to_type = dst_class_die.GetDWARF()->GetDIEToType(); clang::DeclContext *dst_decl_ctx = dst_dwarf_ast_parser->m_die_to_decl_ctx[dst.GetDIE()]; if (dst_decl_ctx) diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp index 87517266fced5..360dbaa1beb5e 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -482,6 +482,13 @@ static ConstString GetDWARFMachOSegmentName() { return g_dwarf_section_name; } +llvm::DenseMap & +SymbolFileDWARF::GetDIEToType() { + if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile()) + return debug_map_symfile->GetDIEToType(); + return m_die_to_type; +} + llvm::DenseMap & SymbolFileDWARF::GetForwardDeclCompilerTypeToDIE() { if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile()) @@ -1594,6 +1601,8 @@ bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) { if (!dwarf_ast) return false; Type *type = GetDIEToType().lookup(decl_die.GetDIE()); + assert(type); + if (decl_die != def_die) { GetDIEToType()[def_die.GetDIE()] = type; DWARFASTParserClang *ast_parser = diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h index 6ecc885538041..7309f7a86b659 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h @@ -335,9 +335,7 @@ class SymbolFileDWARF : public SymbolFileCommon { m_file_index = file_index; } - typedef llvm::DenseMap DIEToTypePtr; - - virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; } + virtual llvm::DenseMap &GetDIEToType(); virtual llvm::DenseMap & GetForwardDeclCompilerTypeToDIE(); @@ -529,7 +527,7 @@ class SymbolFileDWARF : public SymbolFileCommon { UniqueDWARFASTTypeMap m_unique_ast_type_map; // A map from DIE to lldb_private::Type. For record type, the key might be // either declaration DIE or definition DIE. - DIEToTypePtr m_die_to_type; + llvm::DenseMap m_die_to_type; DIEToVariableSP m_die_to_variable_sp; // A map from CompilerType to the struct/class/union/enum DIE (might be a // declaration or a definition) that is used to construct it. diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h index 0ebcad2866a72..df41d6a2a4e42 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h @@ -291,6 +291,10 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon { return m_unique_ast_type_map; } + llvm::DenseMap &GetDIEToType() { + return m_die_to_type; + } + // OSOEntry class OSOEntry { public: @@ -329,6 +333,8 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon { llvm::DenseMap m_forward_decl_compiler_type_to_die; UniqueDWARFASTTypeMap m_unique_ast_type_map; + llvm::DenseMap m_die_to_type; + DebugMap m_debug_map; // When an object file from the debug map gets parsed in diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp index 49632e1d8911c..c1829abe72933 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp @@ -102,7 +102,8 @@ bool SymbolFileDWARFDwo::ParseVendorDWARFOpcode( return GetBaseSymbolFile().ParseVendorDWARFOpcode(op, opcodes, offset, stack); } -SymbolFileDWARF::DIEToTypePtr &SymbolFileDWARFDwo::GetDIEToType() { +llvm::DenseMap & +SymbolFileDWARFDwo::GetDIEToType() { return GetBaseSymbolFile().GetDIEToType(); } diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h index 15c28fefd81f9..75f5986f14014 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h @@ -70,7 +70,7 @@ class SymbolFileDWARFDwo : public SymbolFileDWARF { SymbolFileDWARF *GetDIERefSymbolFile(const DIERef &die_ref) override; protected: - DIEToTypePtr &GetDIEToType() override; + llvm::DenseMap &GetDIEToType() override; DIEToVariableSP &GetDIEToVariable() override; diff --git a/lldb/test/API/lang/cpp/typedef-to-outer-fwd/Makefile b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/Makefile new file mode 100644 index 0000000000000..d9db5666f9b6c --- /dev/null +++ b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := lib.cpp main.cpp + +include Makefile.rules diff --git a/lldb/test/API/lang/cpp/typedef-to-outer-fwd/TestTypedefToOuterFwd.py b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/TestTypedefToOuterFwd.py new file mode 100644 index 0000000000000..7e9f6967a1288 --- /dev/null +++ b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/TestTypedefToOuterFwd.py @@ -0,0 +1,38 @@ +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class TestCaseTypedefToOuterFwd(TestBase): + """ + We find a global variable whose type is forward declared + (whose definition is in either main.o or lib.o). We then + try to get the 'Ref' typedef nested within that forward + declared type. This test makes sure we correctly resolve + this typedef. + + We test this for two cases, where the definition lives + in main.o or lib.o. + """ + + def check_global_var(self, target, name: str): + var = target.FindFirstGlobalVariable(name) + self.assertSuccess(var.GetError(), f"Found {name}") + + var_type = var.GetType() + self.assertTrue(var_type) + + impl = var_type.GetPointeeType() + self.assertTrue(impl) + + ref = impl.FindDirectNestedType("Ref") + self.assertTrue(ref) + + self.assertEqual(ref.GetCanonicalType(), var_type) + + def test(self): + self.build() + target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) + self.check_global_var(target, "gLibExternalDef") + self.check_global_var(target, "gMainExternalDef") diff --git a/lldb/test/API/lang/cpp/typedef-to-outer-fwd/lib.cpp b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/lib.cpp new file mode 100644 index 0000000000000..b50a13afe1acc --- /dev/null +++ b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/lib.cpp @@ -0,0 +1,10 @@ +#include "lib.h" + +template struct FooImpl { + using Ref = FooImpl *; + + Ref Create() { return new FooImpl(); } +}; + +FooImpl gLibLocalDef; +BarImpl *gLibExternalDef = nullptr; diff --git a/lldb/test/API/lang/cpp/typedef-to-outer-fwd/lib.h b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/lib.h new file mode 100644 index 0000000000000..d909fe58df12b --- /dev/null +++ b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/lib.h @@ -0,0 +1,7 @@ +#ifndef LIB_H_IN +#define LIB_H_IN + +template struct FooImpl; +template struct BarImpl; + +#endif // LIB_H_IN diff --git a/lldb/test/API/lang/cpp/typedef-to-outer-fwd/main.cpp b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/main.cpp new file mode 100644 index 0000000000000..acf8337a69d0b --- /dev/null +++ b/lldb/test/API/lang/cpp/typedef-to-outer-fwd/main.cpp @@ -0,0 +1,12 @@ +#include "lib.h" + +template struct BarImpl { + using Ref = BarImpl *; + + Ref Create() { return new BarImpl(); } +}; + +BarImpl gMainLocalDef; +FooImpl *gMainExternalDef = nullptr; + +int main() { return 0; }