xref: /llvm-project/llvm/unittests/DebugInfo/PDB/NativeSymbolReuseTest.cpp (revision 7999b4fa48b31f67efa3662443a5c78343eb6f19)
1 //===- NativeSymbolReuseTest.cpp ------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/PDB/PDB.h"
11 
12 #include "llvm/DebugInfo/PDB/IPDBSession.h"
13 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
14 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
16 #include "llvm/Support/Path.h"
17 
18 #include "llvm/Testing/Support/Error.h"
19 #include "llvm/Testing/Support/SupportHelpers.h"
20 
21 #include "gtest/gtest.h"
22 
23 using namespace llvm;
24 using namespace llvm::pdb;
25 
26 TEST(NativeSymbolReuseTest, GlobalSymbolReuse) {
27   SmallString<128> InputsDir = unittest::getInputFileDirectory();
28   llvm::sys::path::append(InputsDir, "empty.pdb");
29 
30   std::unique_ptr<IPDBSession> S;
31   Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);
32 
33   ASSERT_THAT_ERROR(std::move(E), Succeeded());
34 
35   SymIndexId GlobalId;
36   {
37     auto GS1 = S->getGlobalScope();
38     auto GS2 = S->getGlobalScope();
39 
40     GlobalId = GS1->getSymIndexId();
41     SymIndexId Id2 = GS1->getSymIndexId();
42     EXPECT_EQ(GlobalId, Id2);
43   }
44 
45   {
46     auto GS3 = S->getGlobalScope();
47 
48     SymIndexId Id3 = GS3->getSymIndexId();
49     EXPECT_EQ(GlobalId, Id3);
50   }
51 }
52 
53 TEST(NativeSymbolReuseTest, CompilandSymbolReuse) {
54   SmallString<128> InputsDir = unittest::getInputFileDirectory();
55   llvm::sys::path::append(InputsDir, "empty.pdb");
56 
57   std::unique_ptr<IPDBSession> S;
58   Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);
59 
60   ASSERT_THAT_ERROR(std::move(E), Succeeded());
61 
62   auto GS = S->getGlobalScope();
63 
64   std::vector<SymIndexId> CompilandIds;
65   {
66     auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
67     ASSERT_NE(nullptr, Compilands);
68     ASSERT_EQ(2, Compilands->getChildCount());
69     std::vector<SymIndexId> Ids2;
70 
71     // First try resetting the enumerator, then try destroying the enumerator
72     // and constructing another one.
73     while (auto Compiland = Compilands->getNext())
74       CompilandIds.push_back(Compiland->getSymIndexId());
75     Compilands->reset();
76     while (auto Compiland = Compilands->getNext())
77       Ids2.push_back(Compiland->getSymIndexId());
78 
79     EXPECT_EQ(CompilandIds, Ids2);
80   }
81 
82   {
83     auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
84     ASSERT_NE(nullptr, Compilands);
85     ASSERT_EQ(2U, Compilands->getChildCount());
86 
87     std::vector<SymIndexId> Ids3;
88     while (auto Compiland = Compilands->getNext())
89       Ids3.push_back(Compiland->getSymIndexId());
90 
91     EXPECT_EQ(CompilandIds, Ids3);
92   }
93 }
94 
95 TEST(NativeSymbolReuseTest, CompilandSymbolReuseBackwards) {
96   SmallString<128> InputsDir = unittest::getInputFileDirectory();
97   llvm::sys::path::append(InputsDir, "empty.pdb");
98 
99   std::unique_ptr<IPDBSession> S;
100   Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);
101 
102   ASSERT_THAT_ERROR(std::move(E), Succeeded());
103 
104   auto GS = S->getGlobalScope();
105 
106   // This time do the first iteration backwards, and make sure that when you
107   // then iterate them forwards, the IDs come out in reverse.
108   std::vector<SymIndexId> CompilandIds;
109   {
110     auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
111     ASSERT_NE(nullptr, Compilands);
112     ASSERT_EQ(2U, Compilands->getChildCount());
113 
114     std::vector<SymIndexId> Ids2;
115 
116     for (int I = Compilands->getChildCount() - 1; I >= 0; --I) {
117       auto Compiland = Compilands->getChildAtIndex(I);
118       CompilandIds.push_back(Compiland->getSymIndexId());
119     }
120 
121     while (auto Compiland = Compilands->getNext())
122       Ids2.push_back(Compiland->getSymIndexId());
123 
124     auto ReversedIter = llvm::reverse(Ids2);
125     std::vector<SymIndexId> Reversed{ReversedIter.begin(), ReversedIter.end()};
126     EXPECT_EQ(CompilandIds, Reversed);
127   }
128 }
129