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(InputData: StringRef(Bytes.data(), Bytes.size()), BufferName: "" , |
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(I: 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(I: 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 B1 = getBuffer(I: 1); |
75 | auto B2 = getBuffer(I: 2); |
76 | auto *RawB1 = B1.get(); |
77 | auto *RawB2 = B2.get(); |
78 | ASSERT_NE(RawB1, RawB2); |
79 | |
80 | InMemoryModuleCache Cache; |
81 | EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B" )); |
82 | EXPECT_EQ(RawB1, &Cache.addPCM("B" , std::move(B1))); |
83 | EXPECT_FALSE(Cache.tryToDropPCM("B" )); |
84 | EXPECT_EQ(nullptr, Cache.lookupPCM("B" )); |
85 | EXPECT_EQ(InMemoryModuleCache::ToBuild, Cache.getPCMState("B" )); |
86 | EXPECT_FALSE(Cache.isPCMFinal("B" )); |
87 | EXPECT_TRUE(Cache.shouldBuildPCM("B" )); |
88 | |
89 | #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST |
90 | EXPECT_DEATH(Cache.addPCM("B" , getBuffer(2)), "Already has a PCM" ); |
91 | EXPECT_DEATH(Cache.tryToDropPCM("B" ), |
92 | "PCM to remove is scheduled to be built" ); |
93 | EXPECT_DEATH(Cache.finalizePCM("B" ), "Trying to finalize a dropped PCM" ); |
94 | #endif |
95 | |
96 | // Add a new one. |
97 | EXPECT_EQ(RawB2, &Cache.addBuiltPCM("B" , std::move(B2))); |
98 | EXPECT_TRUE(Cache.isPCMFinal("B" )); |
99 | |
100 | // Can try to drop again, but this should error and do nothing. |
101 | EXPECT_TRUE(Cache.tryToDropPCM("B" )); |
102 | EXPECT_EQ(RawB2, Cache.lookupPCM("B" )); |
103 | } |
104 | |
105 | TEST(InMemoryModuleCacheTest, finalizePCM) { |
106 | auto B = getBuffer(I: 1); |
107 | auto *RawB = B.get(); |
108 | |
109 | InMemoryModuleCache Cache; |
110 | EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B" )); |
111 | EXPECT_EQ(RawB, &Cache.addPCM("B" , std::move(B))); |
112 | |
113 | // Call finalize. |
114 | Cache.finalizePCM(Filename: "B" ); |
115 | EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B" )); |
116 | EXPECT_TRUE(Cache.isPCMFinal("B" )); |
117 | } |
118 | |
119 | } // namespace |
120 | |