xref: /llvm-project/llvm/unittests/DebugInfo/PDB/NativeSymbolReuseTest.cpp (revision 95dd7a25bab98a93bd58c47e0b6d6f91791bf987)
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 extern const char *TestMainArgv0;
27 
28 TEST(NativeSymbolReuseTest, GlobalSymbolReuse) {
29   SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
30   llvm::sys::path::append(InputsDir, "empty.pdb");
31 
32   std::unique_ptr<IPDBSession> S;
33   Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);
34 
35   ASSERT_THAT_ERROR(std::move(E), Succeeded());
36 
37   SymIndexId GlobalId;
38   {
39     auto GS1 = S->getGlobalScope();
40     auto GS2 = S->getGlobalScope();
41 
42     GlobalId = GS1->getSymIndexId();
43     SymIndexId Id2 = GS1->getSymIndexId();
44     EXPECT_EQ(GlobalId, Id2);
45   }
46 
47   {
48     auto GS3 = S->getGlobalScope();
49 
50     SymIndexId Id3 = GS3->getSymIndexId();
51     EXPECT_EQ(GlobalId, Id3);
52   }
53 }
54 
55 TEST(NativeSymbolReuseTest, CompilandSymbolReuse) {
56   SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
57   llvm::sys::path::append(InputsDir, "empty.pdb");
58 
59   std::unique_ptr<IPDBSession> S;
60   Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);
61 
62   ASSERT_THAT_ERROR(std::move(E), Succeeded());
63 
64   auto GS = S->getGlobalScope();
65 
66   std::vector<SymIndexId> CompilandIds;
67   {
68     auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
69     ASSERT_NE(nullptr, Compilands);
70     ASSERT_EQ(2U, Compilands->getChildCount());
71     std::vector<SymIndexId> Ids2;
72 
73     // First try resetting the enumerator, then try destroying the enumerator
74     // and constructing another one.
75     while (auto Compiland = Compilands->getNext())
76       CompilandIds.push_back(Compiland->getSymIndexId());
77     Compilands->reset();
78     while (auto Compiland = Compilands->getNext())
79       Ids2.push_back(Compiland->getSymIndexId());
80 
81     EXPECT_EQ(CompilandIds, Ids2);
82   }
83 
84   {
85     auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
86     ASSERT_NE(nullptr, Compilands);
87     ASSERT_EQ(2U, Compilands->getChildCount());
88 
89     std::vector<SymIndexId> Ids3;
90     while (auto Compiland = Compilands->getNext())
91       Ids3.push_back(Compiland->getSymIndexId());
92 
93     EXPECT_EQ(CompilandIds, Ids3);
94   }
95 }
96 
97 TEST(NativeSymbolReuseTest, CompilandSymbolReuseBackwards) {
98   SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
99   llvm::sys::path::append(InputsDir, "empty.pdb");
100 
101   std::unique_ptr<IPDBSession> S;
102   Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);
103 
104   ASSERT_THAT_ERROR(std::move(E), Succeeded());
105 
106   auto GS = S->getGlobalScope();
107 
108   // This time do the first iteration backwards, and make sure that when you
109   // then iterate them forwards, the IDs come out in reverse.
110   std::vector<SymIndexId> CompilandIds;
111   {
112     auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
113     ASSERT_NE(nullptr, Compilands);
114     ASSERT_EQ(2U, Compilands->getChildCount());
115 
116     std::vector<SymIndexId> Ids2;
117 
118     for (int I = Compilands->getChildCount() - 1; I >= 0; --I) {
119       auto Compiland = Compilands->getChildAtIndex(I);
120       CompilandIds.push_back(Compiland->getSymIndexId());
121     }
122 
123     while (auto Compiland = Compilands->getNext())
124       Ids2.push_back(Compiland->getSymIndexId());
125 
126     auto ReversedIter = llvm::reverse(Ids2);
127     std::vector<SymIndexId> Reversed{ReversedIter.begin(), ReversedIter.end()};
128     EXPECT_EQ(CompilandIds, Reversed);
129   }
130 }
131