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