NativeSymbolReuseTest.cpp 3.9 KB

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