clang  14.0.0git
InMemoryModuleCache.cpp
Go to the documentation of this file.
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 
10 #include "llvm/Support/MemoryBuffer.h"
11 
12 using namespace clang;
13 
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;
22 }
23 
24 llvm::MemoryBuffer &
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;
30 }
31 
32 llvm::MemoryBuffer &
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;
41 }
42 
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();
49 }
50 
51 bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const {
52  return getPCMState(Filename) == Final;
53 }
54 
55 bool InMemoryModuleCache::shouldBuildPCM(llvm::StringRef Filename) const {
56  return getPCMState(Filename) == ToBuild;
57 }
58 
60  auto I = PCMs.find(Filename);
61  assert(I != PCMs.end() && "PCM to remove is unknown...");
62 
63  auto &PCM = I->second;
64  assert(PCM.Buffer && "PCM to remove is scheduled to be built...");
65 
66  if (PCM.IsFinal)
67  return true;
68 
69  PCM.Buffer.reset();
70  return false;
71 }
72 
74  auto I = PCMs.find(Filename);
75  assert(I != PCMs.end() && "PCM to finalize is unknown...");
76 
77  auto &PCM = I->second;
78  assert(PCM.Buffer && "Trying to finalize a dropped PCM...");
79  PCM.IsFinal = true;
80 }
clang::InMemoryModuleCache::shouldBuildPCM
bool shouldBuildPCM(llvm::StringRef Filename) const
Check whether the PCM is waiting to be built.
Definition: InMemoryModuleCache.cpp:55
clang::InMemoryModuleCache::lookupPCM
llvm::MemoryBuffer * lookupPCM(llvm::StringRef Filename) const
Get a pointer to the pCM if it exists; else nullptr.
Definition: InMemoryModuleCache.cpp:44
clang::InMemoryModuleCache::State
State
There are four states for a PCM.
Definition: InMemoryModuleCache.h:57
Filename
StringRef Filename
Definition: Format.cpp:2333
clang::InMemoryModuleCache::addPCM
llvm::MemoryBuffer & addPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Store the PCM under the Filename.
Definition: InMemoryModuleCache.cpp:25
clang::InMemoryModuleCache::finalizePCM
void finalizePCM(llvm::StringRef Filename)
Mark a PCM as final.
Definition: InMemoryModuleCache.cpp:73
clang::InMemoryModuleCache::Final
@ Final
Definition: InMemoryModuleCache.h:57
InMemoryModuleCache.h
clang::InMemoryModuleCache::Tentative
@ Tentative
Definition: InMemoryModuleCache.h:57
clang::InMemoryModuleCache::tryToDropPCM
bool tryToDropPCM(llvm::StringRef Filename)
Try to remove a buffer from the cache.
Definition: InMemoryModuleCache.cpp:59
clang
Definition: CalledOnceCheck.h:17
clang::InMemoryModuleCache::Unknown
@ Unknown
Definition: InMemoryModuleCache.h:57
clang::InMemoryModuleCache::getPCMState
State getPCMState(llvm::StringRef Filename) const
Get the state of the PCM.
Definition: InMemoryModuleCache.cpp:15
clang::InMemoryModuleCache::isPCMFinal
bool isPCMFinal(llvm::StringRef Filename) const
Check whether the PCM is final and has been shown to work.
Definition: InMemoryModuleCache.cpp:51
clang::InMemoryModuleCache::addBuiltPCM
llvm::MemoryBuffer & addBuiltPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Store a just-built PCM under the Filename.
Definition: InMemoryModuleCache.cpp:33
clang::InMemoryModuleCache::ToBuild
@ ToBuild
Definition: InMemoryModuleCache.h:57