InMemoryModuleCache.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InMemoryModuleCache.h - In-memory cache for modules ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
  14. #define LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
  15. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include <memory>
  19. namespace clang {
  20. /// In-memory cache for modules.
  21. ///
  22. /// This is a cache for modules for use across a compilation, sharing state
  23. /// between the CompilerInstances in an implicit modules build. It must be
  24. /// shared by each CompilerInstance, ASTReader, ASTWriter, and ModuleManager
  25. /// that are coordinating.
  26. ///
  27. /// Critically, it ensures that a single process has a consistent view of each
  28. /// PCM. This is used by \a CompilerInstance when building PCMs to ensure that
  29. /// each \a ModuleManager sees the same files.
  30. class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
  31. struct PCM {
  32. std::unique_ptr<llvm::MemoryBuffer> Buffer;
  33. /// Track whether this PCM is known to be good (either built or
  34. /// successfully imported by a CompilerInstance/ASTReader using this
  35. /// cache).
  36. bool IsFinal = false;
  37. PCM() = default;
  38. PCM(std::unique_ptr<llvm::MemoryBuffer> Buffer)
  39. : Buffer(std::move(Buffer)) {}
  40. };
  41. /// Cache of buffers.
  42. llvm::StringMap<PCM> PCMs;
  43. public:
  44. /// There are four states for a PCM. It must monotonically increase.
  45. ///
  46. /// 1. Unknown: the PCM has neither been read from disk nor built.
  47. /// 2. Tentative: the PCM has been read from disk but not yet imported or
  48. /// built. It might work.
  49. /// 3. ToBuild: the PCM read from disk did not work but a new one has not
  50. /// been built yet.
  51. /// 4. Final: indicating that the current PCM was either built in this
  52. /// process or has been successfully imported.
  53. enum State { Unknown, Tentative, ToBuild, Final };
  54. /// Get the state of the PCM.
  55. State getPCMState(llvm::StringRef Filename) const;
  56. /// Store the PCM under the Filename.
  57. ///
  58. /// \pre state is Unknown
  59. /// \post state is Tentative
  60. /// \return a reference to the buffer as a convenience.
  61. llvm::MemoryBuffer &addPCM(llvm::StringRef Filename,
  62. std::unique_ptr<llvm::MemoryBuffer> Buffer);
  63. /// Store a just-built PCM under the Filename.
  64. ///
  65. /// \pre state is Unknown or ToBuild.
  66. /// \pre state is not Tentative.
  67. /// \return a reference to the buffer as a convenience.
  68. llvm::MemoryBuffer &addBuiltPCM(llvm::StringRef Filename,
  69. std::unique_ptr<llvm::MemoryBuffer> Buffer);
  70. /// Try to remove a buffer from the cache. No effect if state is Final.
  71. ///
  72. /// \pre state is Tentative/Final.
  73. /// \post Tentative => ToBuild or Final => Final.
  74. /// \return false on success, i.e. if Tentative => ToBuild.
  75. bool tryToDropPCM(llvm::StringRef Filename);
  76. /// Mark a PCM as final.
  77. ///
  78. /// \pre state is Tentative or Final.
  79. /// \post state is Final.
  80. void finalizePCM(llvm::StringRef Filename);
  81. /// Get a pointer to the pCM if it exists; else nullptr.
  82. llvm::MemoryBuffer *lookupPCM(llvm::StringRef Filename) const;
  83. /// Check whether the PCM is final and has been shown to work.
  84. ///
  85. /// \return true iff state is Final.
  86. bool isPCMFinal(llvm::StringRef Filename) const;
  87. /// Check whether the PCM is waiting to be built.
  88. ///
  89. /// \return true iff state is ToBuild.
  90. bool shouldBuildPCM(llvm::StringRef Filename) const;
  91. };
  92. } // end namespace clang
  93. #endif // LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
  94. #ifdef __GNUC__
  95. #pragma GCC diagnostic pop
  96. #endif