guiandrade updated this revision to Diff 215966. guiandrade added a comment. Herald added a subscriber: mgorny.
I tried to write a unit test following @labath's suggestion of creating a test class that inherits from `DWARFASTParserClang`. Please let me know what you guys think. Thanks! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D66357/new/ https://reviews.llvm.org/D66357 Files: lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp lldb/unittests/SymbolFile/DWARF/CMakeLists.txt lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp Index: lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp =================================================================== --- /dev/null +++ lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp @@ -0,0 +1,47 @@ +//===-- DWARFASTParserClangTests.cpp ----------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" + +#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" +#include "Plugins/SymbolFile/DWARF/DWARFDIE.h" + +using namespace lldb; +using namespace lldb_private; + +namespace { +class DWARFASTParserClangTests : public testing::Test {}; + +class DWARFASTParserClangStub : public DWARFASTParserClang { +public: + using DWARFASTParserClang::DWARFASTParserClang; + using DWARFASTParserClang::LinkDeclContextToDIE; +}; +} // namespace + +TEST_F(DWARFASTParserClangTests, + TestGetDIEForDeclContextReturnsOnlyMatchingEntries) { + ClangASTContext ast_ctx; + DWARFASTParserClangStub ast_parser(ast_ctx); + + auto unit = (DWARFUnit *)nullptr; + auto die1 = DWARFDIE(unit, (DWARFDebugInfoEntry *)1LL); + auto die2 = DWARFDIE(unit, (DWARFDebugInfoEntry *)2LL); + auto die3 = DWARFDIE(unit, (DWARFDebugInfoEntry *)3LL); + auto die4 = DWARFDIE(unit, (DWARFDebugInfoEntry *)4LL); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)1LL, die1); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)2LL, die2); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)2LL, die3); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)3LL, die4); + + auto die_list = ast_parser.GetDIEForDeclContext( + CompilerDeclContext(nullptr, (clang::DeclContext *)2LL)); + EXPECT_EQ(2u, die_list.size()); + EXPECT_EQ(die2, die_list[0]); + EXPECT_EQ(die3, die_list[1]); +} Index: lldb/unittests/SymbolFile/DWARF/CMakeLists.txt =================================================================== --- lldb/unittests/SymbolFile/DWARF/CMakeLists.txt +++ lldb/unittests/SymbolFile/DWARF/CMakeLists.txt @@ -1,4 +1,5 @@ add_lldb_unittest(SymbolFileDWARFTests + DWARFASTParserClangTests.cpp SymbolFileDWARFTests.cpp LINK_LIBS Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp +++ lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp @@ -2183,9 +2183,10 @@ std::vector<DWARFDIE> DWARFASTParserClang::GetDIEForDeclContext( lldb_private::CompilerDeclContext decl_context) { std::vector<DWARFDIE> result; - for (auto it = m_decl_ctx_to_die.find( - (clang::DeclContext *)decl_context.GetOpaqueDeclContext()); - it != m_decl_ctx_to_die.end(); it++) + auto opaque_decl_ctx = + (clang::DeclContext *)decl_context.GetOpaqueDeclContext(); + for (auto it = m_decl_ctx_to_die.find(opaque_decl_ctx); + it != m_decl_ctx_to_die.end() && it->first == opaque_decl_ctx; it++) result.push_back(it->second); return result; }
Index: lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp =================================================================== --- /dev/null +++ lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp @@ -0,0 +1,47 @@ +//===-- DWARFASTParserClangTests.cpp ----------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" + +#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" +#include "Plugins/SymbolFile/DWARF/DWARFDIE.h" + +using namespace lldb; +using namespace lldb_private; + +namespace { +class DWARFASTParserClangTests : public testing::Test {}; + +class DWARFASTParserClangStub : public DWARFASTParserClang { +public: + using DWARFASTParserClang::DWARFASTParserClang; + using DWARFASTParserClang::LinkDeclContextToDIE; +}; +} // namespace + +TEST_F(DWARFASTParserClangTests, + TestGetDIEForDeclContextReturnsOnlyMatchingEntries) { + ClangASTContext ast_ctx; + DWARFASTParserClangStub ast_parser(ast_ctx); + + auto unit = (DWARFUnit *)nullptr; + auto die1 = DWARFDIE(unit, (DWARFDebugInfoEntry *)1LL); + auto die2 = DWARFDIE(unit, (DWARFDebugInfoEntry *)2LL); + auto die3 = DWARFDIE(unit, (DWARFDebugInfoEntry *)3LL); + auto die4 = DWARFDIE(unit, (DWARFDebugInfoEntry *)4LL); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)1LL, die1); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)2LL, die2); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)2LL, die3); + ast_parser.LinkDeclContextToDIE((clang::DeclContext *)3LL, die4); + + auto die_list = ast_parser.GetDIEForDeclContext( + CompilerDeclContext(nullptr, (clang::DeclContext *)2LL)); + EXPECT_EQ(2u, die_list.size()); + EXPECT_EQ(die2, die_list[0]); + EXPECT_EQ(die3, die_list[1]); +} Index: lldb/unittests/SymbolFile/DWARF/CMakeLists.txt =================================================================== --- lldb/unittests/SymbolFile/DWARF/CMakeLists.txt +++ lldb/unittests/SymbolFile/DWARF/CMakeLists.txt @@ -1,4 +1,5 @@ add_lldb_unittest(SymbolFileDWARFTests + DWARFASTParserClangTests.cpp SymbolFileDWARFTests.cpp LINK_LIBS Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp +++ lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp @@ -2183,9 +2183,10 @@ std::vector<DWARFDIE> DWARFASTParserClang::GetDIEForDeclContext( lldb_private::CompilerDeclContext decl_context) { std::vector<DWARFDIE> result; - for (auto it = m_decl_ctx_to_die.find( - (clang::DeclContext *)decl_context.GetOpaqueDeclContext()); - it != m_decl_ctx_to_die.end(); it++) + auto opaque_decl_ctx = + (clang::DeclContext *)decl_context.GetOpaqueDeclContext(); + for (auto it = m_decl_ctx_to_die.find(opaque_decl_ctx); + it != m_decl_ctx_to_die.end() && it->first == opaque_decl_ctx; it++) result.push_back(it->second); return result; }
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits