1 //===-- DWARFASTParserClangTests.cpp --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
10 #include "Plugins/SymbolFile/DWARF/DWARFDIE.h"
11 #include "TestingSupport/SubsystemRAII.h"
12 #include "lldb/Host/HostInfo.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 class DWARFASTParserClangTests : public testing::Test {
20   SubsystemRAII<FileSystem, HostInfo, TypeSystemClang> subsystems;
21 };
22 
23 namespace {
24 class DWARFASTParserClangStub : public DWARFASTParserClang {
25 public:
26   using DWARFASTParserClang::DWARFASTParserClang;
27   using DWARFASTParserClang::LinkDeclContextToDIE;
28 
29   std::vector<const clang::DeclContext *> GetDeclContextToDIEMapKeys() {
30     std::vector<const clang::DeclContext *> keys;
31     for (const auto &it : m_decl_ctx_to_die)
32       keys.push_back(it.first);
33     return keys;
34   }
35 };
36 } // namespace
37 
38 // If your implementation needs to dereference the dummy pointers we are
39 // defining here, causing this test to fail, feel free to delete it.
40 TEST_F(DWARFASTParserClangTests,
41        EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
42   TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple());
43   DWARFASTParserClangStub ast_parser(ast_ctx);
44 
45   DWARFUnit *unit = nullptr;
46   std::vector<DWARFDIE> dies = {DWARFDIE(unit, (DWARFDebugInfoEntry *)1LL),
47                                 DWARFDIE(unit, (DWARFDebugInfoEntry *)2LL),
48                                 DWARFDIE(unit, (DWARFDebugInfoEntry *)3LL),
49                                 DWARFDIE(unit, (DWARFDebugInfoEntry *)4LL)};
50   std::vector<clang::DeclContext *> decl_ctxs = {
51       (clang::DeclContext *)1LL, (clang::DeclContext *)2LL,
52       (clang::DeclContext *)2LL, (clang::DeclContext *)3LL};
53   for (int i = 0; i < 4; ++i)
54     ast_parser.LinkDeclContextToDIE(decl_ctxs[i], dies[i]);
55   ast_parser.EnsureAllDIEsInDeclContextHaveBeenParsed(
56       CompilerDeclContext(nullptr, decl_ctxs[1]));
57 
58   EXPECT_THAT(ast_parser.GetDeclContextToDIEMapKeys(),
59               testing::UnorderedElementsAre(decl_ctxs[0], decl_ctxs[3]));
60 }
61