150
|
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 //===----------------------------------------------------------------------===//
|
|
8
|
|
9 #include "clang/Serialization/InMemoryModuleCache.h"
|
|
10 #include "llvm/Support/MemoryBuffer.h"
|
|
11
|
|
12 using namespace clang;
|
|
13
|
|
14 llvm::MemoryBuffer &
|
|
15 InMemoryModuleCache::addPCM(llvm::StringRef Filename,
|
|
16 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
|
|
17 auto Insertion = PCMs.insert(std::make_pair(Filename, std::move(Buffer)));
|
|
18 assert(Insertion.second && "Already has a PCM");
|
|
19 return *Insertion.first->second.Buffer;
|
|
20 }
|
|
21
|
|
22 llvm::MemoryBuffer &
|
|
23 InMemoryModuleCache::addFinalPCM(llvm::StringRef Filename,
|
|
24 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
|
|
25 auto &PCM = PCMs[Filename];
|
|
26 assert(!PCM.IsFinal && "Trying to override finalized PCM?");
|
|
27 assert(!PCM.Buffer && "Already has a non-final PCM");
|
|
28 PCM.Buffer = std::move(Buffer);
|
|
29 PCM.IsFinal = true;
|
|
30 return *PCM.Buffer;
|
|
31 }
|
|
32
|
|
33 llvm::MemoryBuffer *
|
|
34 InMemoryModuleCache::lookupPCM(llvm::StringRef Filename) const {
|
|
35 auto I = PCMs.find(Filename);
|
|
36 if (I == PCMs.end())
|
|
37 return nullptr;
|
|
38 return I->second.Buffer.get();
|
|
39 }
|
|
40
|
|
41 bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const {
|
|
42 auto I = PCMs.find(Filename);
|
|
43 if (I == PCMs.end())
|
|
44 return false;
|
|
45 return I->second.IsFinal;
|
|
46 }
|
|
47
|
|
48 bool InMemoryModuleCache::tryToRemovePCM(llvm::StringRef Filename) {
|
|
49 auto I = PCMs.find(Filename);
|
|
50 assert(I != PCMs.end() && "PCM to remove is unknown...");
|
|
51
|
|
52 auto &PCM = I->second;
|
|
53 if (PCM.IsFinal)
|
|
54 return true;
|
|
55
|
|
56 PCMs.erase(I);
|
|
57 return false;
|
|
58 }
|
|
59
|
|
60 void InMemoryModuleCache::finalizePCM(llvm::StringRef Filename) {
|
|
61 auto I = PCMs.find(Filename);
|
|
62 assert(I != PCMs.end() && "PCM to finalize is unknown...");
|
|
63
|
|
64 auto &PCM = I->second;
|
|
65 assert(PCM.Buffer && "Trying to finalize a dropped PCM...");
|
|
66 PCM.IsFinal = true;
|
|
67 }
|