1 //===- NativeSymbolReuseTest.cpp ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/PDB.h"
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"
17 #include "llvm/Testing/Support/Error.h"
18 #include "llvm/Testing/Support/SupportHelpers.h"
20 #include "gtest/gtest.h"
23 using namespace llvm::pdb
;
25 extern const char *TestMainArgv0
;
27 TEST(NativeSymbolReuseTest
, GlobalSymbolReuse
) {
28 SmallString
<128> InputsDir
= unittest::getInputFileDirectory(TestMainArgv0
);
29 llvm::sys::path::append(InputsDir
, "empty.pdb");
31 std::unique_ptr
<IPDBSession
> S
;
32 Error E
= pdb::loadDataForPDB(PDB_ReaderType::Native
, InputsDir
, S
);
34 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
38 auto GS1
= S
->getGlobalScope();
39 auto GS2
= S
->getGlobalScope();
41 GlobalId
= GS1
->getSymIndexId();
42 SymIndexId Id2
= GS1
->getSymIndexId();
43 EXPECT_EQ(GlobalId
, Id2
);
47 auto GS3
= S
->getGlobalScope();
49 SymIndexId Id3
= GS3
->getSymIndexId();
50 EXPECT_EQ(GlobalId
, Id3
);
54 TEST(NativeSymbolReuseTest
, CompilandSymbolReuse
) {
55 SmallString
<128> InputsDir
= unittest::getInputFileDirectory(TestMainArgv0
);
56 llvm::sys::path::append(InputsDir
, "empty.pdb");
58 std::unique_ptr
<IPDBSession
> S
;
59 Error E
= pdb::loadDataForPDB(PDB_ReaderType::Native
, InputsDir
, S
);
61 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
63 auto GS
= S
->getGlobalScope();
65 std::vector
<SymIndexId
> CompilandIds
;
67 auto Compilands
= GS
->findAllChildren
<PDBSymbolCompiland
>();
68 ASSERT_NE(nullptr, Compilands
);
69 ASSERT_EQ(2U, Compilands
->getChildCount());
70 std::vector
<SymIndexId
> Ids2
;
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());
77 while (auto Compiland
= Compilands
->getNext())
78 Ids2
.push_back(Compiland
->getSymIndexId());
80 EXPECT_EQ(CompilandIds
, Ids2
);
84 auto Compilands
= GS
->findAllChildren
<PDBSymbolCompiland
>();
85 ASSERT_NE(nullptr, Compilands
);
86 ASSERT_EQ(2U, Compilands
->getChildCount());
88 std::vector
<SymIndexId
> Ids3
;
89 while (auto Compiland
= Compilands
->getNext())
90 Ids3
.push_back(Compiland
->getSymIndexId());
92 EXPECT_EQ(CompilandIds
, Ids3
);
96 TEST(NativeSymbolReuseTest
, CompilandSymbolReuseBackwards
) {
97 SmallString
<128> InputsDir
= unittest::getInputFileDirectory(TestMainArgv0
);
98 llvm::sys::path::append(InputsDir
, "empty.pdb");
100 std::unique_ptr
<IPDBSession
> S
;
101 Error E
= pdb::loadDataForPDB(PDB_ReaderType::Native
, InputsDir
, S
);
103 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
105 auto GS
= S
->getGlobalScope();
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
;
111 auto Compilands
= GS
->findAllChildren
<PDBSymbolCompiland
>();
112 ASSERT_NE(nullptr, Compilands
);
113 ASSERT_EQ(2U, Compilands
->getChildCount());
115 std::vector
<SymIndexId
> Ids2
;
117 for (int I
= Compilands
->getChildCount() - 1; I
>= 0; --I
) {
118 auto Compiland
= Compilands
->getChildAtIndex(I
);
119 CompilandIds
.push_back(Compiland
->getSymIndexId());
122 while (auto Compiland
= Compilands
->getNext())
123 Ids2
.push_back(Compiland
->getSymIndexId());
125 auto ReversedIter
= llvm::reverse(Ids2
);
126 std::vector
<SymIndexId
> Reversed
{ReversedIter
.begin(), ReversedIter
.end()};
127 EXPECT_EQ(CompilandIds
, Reversed
);