reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
//===- InMemoryModuleCacheTest.cpp - InMemoryModuleCache tests ------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Serialization/InMemoryModuleCache.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"

using namespace llvm;
using namespace clang;

namespace {

std::unique_ptr<MemoryBuffer> getBuffer(int I) {
  SmallVector<char, 8> Bytes;
  raw_svector_ostream(Bytes) << "data:" << I;
  return MemoryBuffer::getMemBuffer(StringRef(Bytes.data(), Bytes.size()), "",
                                    /* RequiresNullTerminator = */ false);
}

TEST(InMemoryModuleCacheTest, initialState) {
  InMemoryModuleCache Cache;
  EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
  EXPECT_FALSE(Cache.isPCMFinal("B"));
  EXPECT_FALSE(Cache.shouldBuildPCM("B"));

#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  EXPECT_DEATH(Cache.tryToDropPCM("B"), "PCM to remove is unknown");
  EXPECT_DEATH(Cache.finalizePCM("B"), "PCM to finalize is unknown");
#endif
}

TEST(InMemoryModuleCacheTest, addPCM) {
  auto B = getBuffer(1);
  auto *RawB = B.get();

  InMemoryModuleCache Cache;
  EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B)));
  EXPECT_EQ(InMemoryModuleCache::Tentative, Cache.getPCMState("B"));
  EXPECT_EQ(RawB, Cache.lookupPCM("B"));
  EXPECT_FALSE(Cache.isPCMFinal("B"));
  EXPECT_FALSE(Cache.shouldBuildPCM("B"));

#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
  EXPECT_DEATH(Cache.addBuiltPCM("B", getBuffer(2)),
               "Trying to override tentative PCM");
#endif
}

TEST(InMemoryModuleCacheTest, addBuiltPCM) {
  auto B = getBuffer(1);
  auto *RawB = B.get();

  InMemoryModuleCache Cache;
  EXPECT_EQ(RawB, &Cache.addBuiltPCM("B", std::move(B)));
  EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B"));
  EXPECT_EQ(RawB, Cache.lookupPCM("B"));
  EXPECT_TRUE(Cache.isPCMFinal("B"));
  EXPECT_FALSE(Cache.shouldBuildPCM("B"));

#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
  EXPECT_DEATH(Cache.addBuiltPCM("B", getBuffer(2)),
               "Trying to override finalized PCM");
#endif
}

TEST(InMemoryModuleCacheTest, tryToDropPCM) {
  auto B1 = getBuffer(1);
  auto B2 = getBuffer(2);
  auto *RawB1 = B1.get();
  auto *RawB2 = B2.get();
  ASSERT_NE(RawB1, RawB2);

  InMemoryModuleCache Cache;
  EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
  EXPECT_EQ(RawB1, &Cache.addPCM("B", std::move(B1)));
  EXPECT_FALSE(Cache.tryToDropPCM("B"));
  EXPECT_EQ(nullptr, Cache.lookupPCM("B"));
  EXPECT_EQ(InMemoryModuleCache::ToBuild, Cache.getPCMState("B"));
  EXPECT_FALSE(Cache.isPCMFinal("B"));
  EXPECT_TRUE(Cache.shouldBuildPCM("B"));

#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
  EXPECT_DEATH(Cache.tryToDropPCM("B"),
               "PCM to remove is scheduled to be built");
  EXPECT_DEATH(Cache.finalizePCM("B"), "Trying to finalize a dropped PCM");
#endif

  // Add a new one.
  EXPECT_EQ(RawB2, &Cache.addBuiltPCM("B", std::move(B2)));
  EXPECT_TRUE(Cache.isPCMFinal("B"));

  // Can try to drop again, but this should error and do nothing.
  EXPECT_TRUE(Cache.tryToDropPCM("B"));
  EXPECT_EQ(RawB2, Cache.lookupPCM("B"));
}

TEST(InMemoryModuleCacheTest, finalizePCM) {
  auto B = getBuffer(1);
  auto *RawB = B.get();

  InMemoryModuleCache Cache;
  EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
  EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B)));

  // Call finalize.
  Cache.finalizePCM("B");
  EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B"));
  EXPECT_TRUE(Cache.isPCMFinal("B"));
}

} // namespace