aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/lib/Serialization/InMemoryModuleCache.cpp
blob: d35fa2a807f4dc33caa12f6c3594f6ca0d27d69d (plain) (blame)
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
//===- InMemoryModuleCache.cpp - Cache for loaded memory buffers ----------===//
//
// 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"

using namespace clang;

InMemoryModuleCache::State
InMemoryModuleCache::getPCMState(llvm::StringRef Filename) const {
  auto I = PCMs.find(Filename);
  if (I == PCMs.end())
    return Unknown;
  if (I->second.IsFinal)
    return Final;
  return I->second.Buffer ? Tentative : ToBuild;
}

llvm::MemoryBuffer &
InMemoryModuleCache::addPCM(llvm::StringRef Filename,
                            std::unique_ptr<llvm::MemoryBuffer> Buffer) {
  auto Insertion = PCMs.insert(std::make_pair(Filename, std::move(Buffer)));
  assert(Insertion.second && "Already has a PCM");
  return *Insertion.first->second.Buffer;
}

llvm::MemoryBuffer &
InMemoryModuleCache::addBuiltPCM(llvm::StringRef Filename,
                                 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
  auto &PCM = PCMs[Filename];
  assert(!PCM.IsFinal && "Trying to override finalized PCM?");
  assert(!PCM.Buffer && "Trying to override tentative PCM?");
  PCM.Buffer = std::move(Buffer);
  PCM.IsFinal = true;
  return *PCM.Buffer;
}

llvm::MemoryBuffer *
InMemoryModuleCache::lookupPCM(llvm::StringRef Filename) const {
  auto I = PCMs.find(Filename);
  if (I == PCMs.end())
    return nullptr;
  return I->second.Buffer.get();
}

bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const {
  return getPCMState(Filename) == Final;
}

bool InMemoryModuleCache::shouldBuildPCM(llvm::StringRef Filename) const {
  return getPCMState(Filename) == ToBuild;
}

bool InMemoryModuleCache::tryToDropPCM(llvm::StringRef Filename) {
  auto I = PCMs.find(Filename);
  assert(I != PCMs.end() && "PCM to remove is unknown...");

  auto &PCM = I->second;
  assert(PCM.Buffer && "PCM to remove is scheduled to be built...");

  if (PCM.IsFinal)
    return true;

  PCM.Buffer.reset();
  return false;
}

void InMemoryModuleCache::finalizePCM(llvm::StringRef Filename) {
  auto I = PCMs.find(Filename);
  assert(I != PCMs.end() && "PCM to finalize is unknown...");

  auto &PCM = I->second;
  assert(PCM.Buffer && "Trying to finalize a dropped PCM...");
  PCM.IsFinal = true;
}