blob: 15afea6729295b9cd52bbae8e5c78d0168191ea6 [file] [log] [blame]
Zachary Turner7999b4f2018-09-05 23:30:38 +00001//===- 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
23using namespace llvm;
24using namespace llvm::pdb;
25
Fangrui Song95dd7a22018-09-06 19:51:20 +000026extern const char *TestMainArgv0;
27
Zachary Turner7999b4f2018-09-05 23:30:38 +000028TEST(NativeSymbolReuseTest, GlobalSymbolReuse) {
Fangrui Song95dd7a22018-09-06 19:51:20 +000029 SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
Zachary Turner7999b4f2018-09-05 23:30:38 +000030 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
55TEST(NativeSymbolReuseTest, CompilandSymbolReuse) {
Fangrui Song95dd7a22018-09-06 19:51:20 +000056 SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
Zachary Turner7999b4f2018-09-05 23:30:38 +000057 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);
Zachary Turnerd62d0522018-09-06 16:34:56 +000070 ASSERT_EQ(2U, Compilands->getChildCount());
Zachary Turner7999b4f2018-09-05 23:30:38 +000071 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
97TEST(NativeSymbolReuseTest, CompilandSymbolReuseBackwards) {
Fangrui Song95dd7a22018-09-06 19:51:20 +000098 SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
Zachary Turner7999b4f2018-09-05 23:30:38 +000099 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}