InMemoryModuleCacheTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===- InMemoryModuleCacheTest.cpp - InMemoryModuleCache tests ------------===//
  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 "clang/Serialization/InMemoryModuleCache.h"
  9. #include "llvm/Support/MemoryBuffer.h"
  10. #include "gtest/gtest.h"
  11. using namespace llvm;
  12. using namespace clang;
  13. namespace {
  14. std::unique_ptr<MemoryBuffer> getBuffer(int I) {
  15. SmallVector<char, 8> Bytes;
  16. raw_svector_ostream(Bytes) << "data:" << I;
  17. return MemoryBuffer::getMemBuffer(StringRef(Bytes.data(), Bytes.size()), "",
  18. /* RequiresNullTerminator = */ false);
  19. }
  20. TEST(InMemoryModuleCacheTest, initialState) {
  21. InMemoryModuleCache Cache;
  22. EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
  23. EXPECT_FALSE(Cache.isPCMFinal("B"));
  24. EXPECT_FALSE(Cache.shouldBuildPCM("B"));
  25. #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  26. EXPECT_DEATH(Cache.tryToDropPCM("B"), "PCM to remove is unknown");
  27. EXPECT_DEATH(Cache.finalizePCM("B"), "PCM to finalize is unknown");
  28. #endif
  29. }
  30. TEST(InMemoryModuleCacheTest, addPCM) {
  31. auto B = getBuffer(1);
  32. auto *RawB = B.get();
  33. InMemoryModuleCache Cache;
  34. EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B)));
  35. EXPECT_EQ(InMemoryModuleCache::Tentative, Cache.getPCMState("B"));
  36. EXPECT_EQ(RawB, Cache.lookupPCM("B"));
  37. EXPECT_FALSE(Cache.isPCMFinal("B"));
  38. EXPECT_FALSE(Cache.shouldBuildPCM("B"));
  39. #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  40. EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
  41. EXPECT_DEATH(Cache.addBuiltPCM("B", getBuffer(2)),
  42. "Trying to override tentative PCM");
  43. #endif
  44. }
  45. TEST(InMemoryModuleCacheTest, addBuiltPCM) {
  46. auto B = getBuffer(1);
  47. auto *RawB = B.get();
  48. InMemoryModuleCache Cache;
  49. EXPECT_EQ(RawB, &Cache.addBuiltPCM("B", std::move(B)));
  50. EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B"));
  51. EXPECT_EQ(RawB, Cache.lookupPCM("B"));
  52. EXPECT_TRUE(Cache.isPCMFinal("B"));
  53. EXPECT_FALSE(Cache.shouldBuildPCM("B"));
  54. #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  55. EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
  56. EXPECT_DEATH(Cache.addBuiltPCM("B", getBuffer(2)),
  57. "Trying to override finalized PCM");
  58. #endif
  59. }
  60. TEST(InMemoryModuleCacheTest, tryToDropPCM) {
  61. auto B1 = getBuffer(1);
  62. auto B2 = getBuffer(2);
  63. auto *RawB1 = B1.get();
  64. auto *RawB2 = B2.get();
  65. ASSERT_NE(RawB1, RawB2);
  66. InMemoryModuleCache Cache;
  67. EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
  68. EXPECT_EQ(RawB1, &Cache.addPCM("B", std::move(B1)));
  69. EXPECT_FALSE(Cache.tryToDropPCM("B"));
  70. EXPECT_EQ(nullptr, Cache.lookupPCM("B"));
  71. EXPECT_EQ(InMemoryModuleCache::ToBuild, Cache.getPCMState("B"));
  72. EXPECT_FALSE(Cache.isPCMFinal("B"));
  73. EXPECT_TRUE(Cache.shouldBuildPCM("B"));
  74. #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  75. EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
  76. EXPECT_DEATH(Cache.tryToDropPCM("B"),
  77. "PCM to remove is scheduled to be built");
  78. EXPECT_DEATH(Cache.finalizePCM("B"), "Trying to finalize a dropped PCM");
  79. #endif
  80. // Add a new one.
  81. EXPECT_EQ(RawB2, &Cache.addBuiltPCM("B", std::move(B2)));
  82. EXPECT_TRUE(Cache.isPCMFinal("B"));
  83. // Can try to drop again, but this should error and do nothing.
  84. EXPECT_TRUE(Cache.tryToDropPCM("B"));
  85. EXPECT_EQ(RawB2, Cache.lookupPCM("B"));
  86. }
  87. TEST(InMemoryModuleCacheTest, finalizePCM) {
  88. auto B = getBuffer(1);
  89. auto *RawB = B.get();
  90. InMemoryModuleCache Cache;
  91. EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
  92. EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B)));
  93. // Call finalize.
  94. Cache.finalizePCM("B");
  95. EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B"));
  96. EXPECT_TRUE(Cache.isPCMFinal("B"));
  97. }
  98. } // namespace