PCHContainerOperations.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- Serialization/PCHContainerOperations.h - PCH Containers --*- 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_PCHCONTAINEROPERATIONS_H
  14. #define LLVM_CLANG_SERIALIZATION_PCHCONTAINEROPERATIONS_H
  15. #include "clang/Basic/Module.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include <memory>
  20. namespace llvm {
  21. class raw_pwrite_stream;
  22. }
  23. namespace clang {
  24. class ASTConsumer;
  25. class CompilerInstance;
  26. struct PCHBuffer {
  27. ASTFileSignature Signature;
  28. llvm::SmallVector<char, 0> Data;
  29. bool IsComplete;
  30. };
  31. /// This abstract interface provides operations for creating
  32. /// containers for serialized ASTs (precompiled headers and clang
  33. /// modules).
  34. class PCHContainerWriter {
  35. public:
  36. virtual ~PCHContainerWriter() = 0;
  37. virtual llvm::StringRef getFormat() const = 0;
  38. /// Return an ASTConsumer that can be chained with a
  39. /// PCHGenerator that produces a wrapper file format containing a
  40. /// serialized AST bitstream.
  41. virtual std::unique_ptr<ASTConsumer>
  42. CreatePCHContainerGenerator(CompilerInstance &CI,
  43. const std::string &MainFileName,
  44. const std::string &OutputFileName,
  45. std::unique_ptr<llvm::raw_pwrite_stream> OS,
  46. std::shared_ptr<PCHBuffer> Buffer) const = 0;
  47. };
  48. /// This abstract interface provides operations for unwrapping
  49. /// containers for serialized ASTs (precompiled headers and clang
  50. /// modules).
  51. class PCHContainerReader {
  52. public:
  53. virtual ~PCHContainerReader() = 0;
  54. /// Equivalent to the format passed to -fmodule-format=
  55. virtual llvm::StringRef getFormat() const = 0;
  56. /// Returns the serialized AST inside the PCH container Buffer.
  57. virtual llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const = 0;
  58. };
  59. /// Implements write operations for a raw pass-through PCH container.
  60. class RawPCHContainerWriter : public PCHContainerWriter {
  61. llvm::StringRef getFormat() const override { return "raw"; }
  62. /// Return an ASTConsumer that can be chained with a
  63. /// PCHGenerator that writes the module to a flat file.
  64. std::unique_ptr<ASTConsumer>
  65. CreatePCHContainerGenerator(CompilerInstance &CI,
  66. const std::string &MainFileName,
  67. const std::string &OutputFileName,
  68. std::unique_ptr<llvm::raw_pwrite_stream> OS,
  69. std::shared_ptr<PCHBuffer> Buffer) const override;
  70. };
  71. /// Implements read operations for a raw pass-through PCH container.
  72. class RawPCHContainerReader : public PCHContainerReader {
  73. llvm::StringRef getFormat() const override { return "raw"; }
  74. /// Simply returns the buffer contained in Buffer.
  75. llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
  76. };
  77. /// A registry of PCHContainerWriter and -Reader objects for different formats.
  78. class PCHContainerOperations {
  79. llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
  80. llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
  81. public:
  82. /// Automatically registers a RawPCHContainerWriter and
  83. /// RawPCHContainerReader.
  84. PCHContainerOperations();
  85. void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
  86. Writers[Writer->getFormat()] = std::move(Writer);
  87. }
  88. void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
  89. Readers[Reader->getFormat()] = std::move(Reader);
  90. }
  91. const PCHContainerWriter *getWriterOrNull(llvm::StringRef Format) {
  92. return Writers[Format].get();
  93. }
  94. const PCHContainerReader *getReaderOrNull(llvm::StringRef Format) {
  95. return Readers[Format].get();
  96. }
  97. const PCHContainerReader &getRawReader() {
  98. return *getReaderOrNull("raw");
  99. }
  100. };
  101. }
  102. #endif
  103. #ifdef __GNUC__
  104. #pragma GCC diagnostic pop
  105. #endif