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