[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / lib / Serialization / InMemoryModuleCache.cpp
blobd35fa2a807f4dc33caa12f6c3594f6ca0d27d69d
1 //===- InMemoryModuleCache.cpp - Cache for loaded memory buffers ----------===//
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 //===----------------------------------------------------------------------===//
9 #include "clang/Serialization/InMemoryModuleCache.h"
10 #include "llvm/Support/MemoryBuffer.h"
12 using namespace clang;
14 InMemoryModuleCache::State
15 InMemoryModuleCache::getPCMState(llvm::StringRef Filename) const {
16 auto I = PCMs.find(Filename);
17 if (I == PCMs.end())
18 return Unknown;
19 if (I->second.IsFinal)
20 return Final;
21 return I->second.Buffer ? Tentative : ToBuild;
24 llvm::MemoryBuffer &
25 InMemoryModuleCache::addPCM(llvm::StringRef Filename,
26 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
27 auto Insertion = PCMs.insert(std::make_pair(Filename, std::move(Buffer)));
28 assert(Insertion.second && "Already has a PCM");
29 return *Insertion.first->second.Buffer;
32 llvm::MemoryBuffer &
33 InMemoryModuleCache::addBuiltPCM(llvm::StringRef Filename,
34 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
35 auto &PCM = PCMs[Filename];
36 assert(!PCM.IsFinal && "Trying to override finalized PCM?");
37 assert(!PCM.Buffer && "Trying to override tentative PCM?");
38 PCM.Buffer = std::move(Buffer);
39 PCM.IsFinal = true;
40 return *PCM.Buffer;
43 llvm::MemoryBuffer *
44 InMemoryModuleCache::lookupPCM(llvm::StringRef Filename) const {
45 auto I = PCMs.find(Filename);
46 if (I == PCMs.end())
47 return nullptr;
48 return I->second.Buffer.get();
51 bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const {
52 return getPCMState(Filename) == Final;
55 bool InMemoryModuleCache::shouldBuildPCM(llvm::StringRef Filename) const {
56 return getPCMState(Filename) == ToBuild;
59 bool InMemoryModuleCache::tryToDropPCM(llvm::StringRef Filename) {
60 auto I = PCMs.find(Filename);
61 assert(I != PCMs.end() && "PCM to remove is unknown...");
63 auto &PCM = I->second;
64 assert(PCM.Buffer && "PCM to remove is scheduled to be built...");
66 if (PCM.IsFinal)
67 return true;
69 PCM.Buffer.reset();
70 return false;
73 void InMemoryModuleCache::finalizePCM(llvm::StringRef Filename) {
74 auto I = PCMs.find(Filename);
75 assert(I != PCMs.end() && "PCM to finalize is unknown...");
77 auto &PCM = I->second;
78 assert(PCM.Buffer && "Trying to finalize a dropped PCM...");
79 PCM.IsFinal = true;