/src/llvm-project/clang/lib/Serialization/InMemoryModuleCache.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | InMemoryModuleCache::State |
15 | 0 | InMemoryModuleCache::getPCMState(llvm::StringRef Filename) const { |
16 | 0 | auto I = PCMs.find(Filename); |
17 | 0 | if (I == PCMs.end()) |
18 | 0 | return Unknown; |
19 | 0 | if (I->second.IsFinal) |
20 | 0 | return Final; |
21 | 0 | return I->second.Buffer ? Tentative : ToBuild; |
22 | 0 | } |
23 | | |
24 | | llvm::MemoryBuffer & |
25 | | InMemoryModuleCache::addPCM(llvm::StringRef Filename, |
26 | 0 | std::unique_ptr<llvm::MemoryBuffer> Buffer) { |
27 | 0 | auto Insertion = PCMs.insert(std::make_pair(Filename, std::move(Buffer))); |
28 | 0 | assert(Insertion.second && "Already has a PCM"); |
29 | 0 | return *Insertion.first->second.Buffer; |
30 | 0 | } |
31 | | |
32 | | llvm::MemoryBuffer & |
33 | | InMemoryModuleCache::addBuiltPCM(llvm::StringRef Filename, |
34 | 0 | std::unique_ptr<llvm::MemoryBuffer> Buffer) { |
35 | 0 | auto &PCM = PCMs[Filename]; |
36 | 0 | assert(!PCM.IsFinal && "Trying to override finalized PCM?"); |
37 | 0 | assert(!PCM.Buffer && "Trying to override tentative PCM?"); |
38 | 0 | PCM.Buffer = std::move(Buffer); |
39 | 0 | PCM.IsFinal = true; |
40 | 0 | return *PCM.Buffer; |
41 | 0 | } |
42 | | |
43 | | llvm::MemoryBuffer * |
44 | 0 | InMemoryModuleCache::lookupPCM(llvm::StringRef Filename) const { |
45 | 0 | auto I = PCMs.find(Filename); |
46 | 0 | if (I == PCMs.end()) |
47 | 0 | return nullptr; |
48 | 0 | return I->second.Buffer.get(); |
49 | 0 | } |
50 | | |
51 | 0 | bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const { |
52 | 0 | return getPCMState(Filename) == Final; |
53 | 0 | } |
54 | | |
55 | 0 | bool InMemoryModuleCache::shouldBuildPCM(llvm::StringRef Filename) const { |
56 | 0 | return getPCMState(Filename) == ToBuild; |
57 | 0 | } |
58 | | |
59 | 0 | bool InMemoryModuleCache::tryToDropPCM(llvm::StringRef Filename) { |
60 | 0 | auto I = PCMs.find(Filename); |
61 | 0 | assert(I != PCMs.end() && "PCM to remove is unknown..."); |
62 | | |
63 | 0 | auto &PCM = I->second; |
64 | 0 | assert(PCM.Buffer && "PCM to remove is scheduled to be built..."); |
65 | | |
66 | 0 | if (PCM.IsFinal) |
67 | 0 | return true; |
68 | | |
69 | 0 | PCM.Buffer.reset(); |
70 | 0 | return false; |
71 | 0 | } |
72 | | |
73 | 0 | void InMemoryModuleCache::finalizePCM(llvm::StringRef Filename) { |
74 | 0 | auto I = PCMs.find(Filename); |
75 | 0 | assert(I != PCMs.end() && "PCM to finalize is unknown..."); |
76 | | |
77 | 0 | auto &PCM = I->second; |
78 | 0 | assert(PCM.Buffer && "Trying to finalize a dropped PCM..."); |
79 | 0 | PCM.IsFinal = true; |
80 | 0 | } |