1 //===- InMemoryModuleCacheTest.cpp - InMemoryModuleCache tests ------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/Serialization/InMemoryModuleCache.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "gtest/gtest.h"
14 using namespace clang
;
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);
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"));
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");
37 TEST(InMemoryModuleCacheTest
, addPCM
) {
38 auto B
= getBuffer(1);
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"));
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");
55 TEST(InMemoryModuleCacheTest
, addBuiltPCM
) {
56 auto B
= getBuffer(1);
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"));
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");
73 TEST(InMemoryModuleCacheTest
, tryToDropPCM
) {
74 auto B1
= getBuffer(1);
75 auto B2
= getBuffer(2);
76 auto *RawB1
= B1
.get();
77 auto *RawB2
= B2
.get();
78 ASSERT_NE(RawB1
, RawB2
);
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"));
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");
97 EXPECT_EQ(RawB2
, &Cache
.addBuiltPCM("B", std::move(B2
)));
98 EXPECT_TRUE(Cache
.isPCMFinal("B"));
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"));
105 TEST(InMemoryModuleCacheTest
, finalizePCM
) {
106 auto B
= getBuffer(1);
107 auto *RawB
= B
.get();
109 InMemoryModuleCache Cache
;
110 EXPECT_EQ(InMemoryModuleCache::Unknown
, Cache
.getPCMState("B"));
111 EXPECT_EQ(RawB
, &Cache
.addPCM("B", std::move(B
)));
114 Cache
.finalizePCM("B");
115 EXPECT_EQ(InMemoryModuleCache::Final
, Cache
.getPCMState("B"));
116 EXPECT_TRUE(Cache
.isPCMFinal("B"));