InMemoryModuleCache.cpp 2.4 KB

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