Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 1 | //===- NativeSymbolReuseTest.cpp ------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 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 | |
Fangrui Song | 95dd7a2 | 2018-09-06 19:51:20 +0000 | [diff] [blame] | 25 | extern const char *TestMainArgv0; |
| 26 | |
Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 27 | TEST(NativeSymbolReuseTest, GlobalSymbolReuse) { |
Fangrui Song | 95dd7a2 | 2018-09-06 19:51:20 +0000 | [diff] [blame] | 28 | SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0); |
Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 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 | |
| 54 | TEST(NativeSymbolReuseTest, CompilandSymbolReuse) { |
Fangrui Song | 95dd7a2 | 2018-09-06 19:51:20 +0000 | [diff] [blame] | 55 | SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0); |
Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 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); |
Zachary Turner | d62d052 | 2018-09-06 16:34:56 +0000 | [diff] [blame] | 69 | ASSERT_EQ(2U, Compilands->getChildCount()); |
Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 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 | |
| 96 | TEST(NativeSymbolReuseTest, CompilandSymbolReuseBackwards) { |
Fangrui Song | 95dd7a2 | 2018-09-06 19:51:20 +0000 | [diff] [blame] | 97 | SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0); |
Zachary Turner | 7999b4f | 2018-09-05 23:30:38 +0000 | [diff] [blame] | 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 | } |