summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/DebugInfo/PDB/NativeSymbolReuseTest.cpp
blob: 91c7db4fc393fb253fe7bb98972d7f29d72617a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//===- NativeSymbolReuseTest.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/PDB/PDB.h"

#include "llvm/DebugInfo/PDB/IPDBSession.h"
#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
#include "llvm/Support/Path.h"

#include "llvm/Testing/Support/Error.h"
#include "llvm/Testing/Support/SupportHelpers.h"

#include "gtest/gtest.h"

using namespace llvm;
using namespace llvm::pdb;

extern const char *TestMainArgv0;

TEST(NativeSymbolReuseTest, GlobalSymbolReuse) {
  SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
  llvm::sys::path::append(InputsDir, "empty.pdb");

  std::unique_ptr<IPDBSession> S;
  Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);

  ASSERT_THAT_ERROR(std::move(E), Succeeded());

  SymIndexId GlobalId;
  {
    auto GS1 = S->getGlobalScope();
    auto GS2 = S->getGlobalScope();

    GlobalId = GS1->getSymIndexId();
    SymIndexId Id2 = GS1->getSymIndexId();
    EXPECT_EQ(GlobalId, Id2);
  }

  {
    auto GS3 = S->getGlobalScope();

    SymIndexId Id3 = GS3->getSymIndexId();
    EXPECT_EQ(GlobalId, Id3);
  }
}

TEST(NativeSymbolReuseTest, CompilandSymbolReuse) {
  SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
  llvm::sys::path::append(InputsDir, "empty.pdb");

  std::unique_ptr<IPDBSession> S;
  Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);

  ASSERT_THAT_ERROR(std::move(E), Succeeded());

  auto GS = S->getGlobalScope();

  std::vector<SymIndexId> CompilandIds;
  {
    auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
    ASSERT_NE(nullptr, Compilands);
    ASSERT_EQ(2U, Compilands->getChildCount());
    std::vector<SymIndexId> Ids2;

    // First try resetting the enumerator, then try destroying the enumerator
    // and constructing another one.
    while (auto Compiland = Compilands->getNext())
      CompilandIds.push_back(Compiland->getSymIndexId());
    Compilands->reset();
    while (auto Compiland = Compilands->getNext())
      Ids2.push_back(Compiland->getSymIndexId());

    EXPECT_EQ(CompilandIds, Ids2);
  }

  {
    auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
    ASSERT_NE(nullptr, Compilands);
    ASSERT_EQ(2U, Compilands->getChildCount());

    std::vector<SymIndexId> Ids3;
    while (auto Compiland = Compilands->getNext())
      Ids3.push_back(Compiland->getSymIndexId());

    EXPECT_EQ(CompilandIds, Ids3);
  }
}

TEST(NativeSymbolReuseTest, CompilandSymbolReuseBackwards) {
  SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
  llvm::sys::path::append(InputsDir, "empty.pdb");

  std::unique_ptr<IPDBSession> S;
  Error E = pdb::loadDataForPDB(PDB_ReaderType::Native, InputsDir, S);

  ASSERT_THAT_ERROR(std::move(E), Succeeded());

  auto GS = S->getGlobalScope();

  // This time do the first iteration backwards, and make sure that when you
  // then iterate them forwards, the IDs come out in reverse.
  std::vector<SymIndexId> CompilandIds;
  {
    auto Compilands = GS->findAllChildren<PDBSymbolCompiland>();
    ASSERT_NE(nullptr, Compilands);
    ASSERT_EQ(2U, Compilands->getChildCount());

    std::vector<SymIndexId> Ids2;

    for (int I = Compilands->getChildCount() - 1; I >= 0; --I) {
      auto Compiland = Compilands->getChildAtIndex(I);
      CompilandIds.push_back(Compiland->getSymIndexId());
    }

    while (auto Compiland = Compilands->getNext())
      Ids2.push_back(Compiland->getSymIndexId());

    auto ReversedIter = llvm::reverse(Ids2);
    std::vector<SymIndexId> Reversed{ReversedIter.begin(), ReversedIter.end()};
    EXPECT_EQ(CompilandIds, Reversed);
  }
}
OpenPOWER on IntegriCloud