Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:06

0001 //===-- PCHContainerOperations.h - PCH Containers ---------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_SERIALIZATION_PCHCONTAINEROPERATIONS_H
0010 #define LLVM_CLANG_SERIALIZATION_PCHCONTAINEROPERATIONS_H
0011 
0012 #include "clang/Basic/Module.h"
0013 #include "llvm/ADT/SmallVector.h"
0014 #include "llvm/ADT/StringMap.h"
0015 #include "llvm/Support/MemoryBufferRef.h"
0016 #include <memory>
0017 
0018 namespace llvm {
0019 class raw_pwrite_stream;
0020 }
0021 
0022 namespace clang {
0023 
0024 class ASTConsumer;
0025 class CompilerInstance;
0026 
0027 struct PCHBuffer {
0028   ASTFileSignature Signature;
0029   llvm::SmallVector<char, 0> Data;
0030   bool IsComplete;
0031 };
0032 
0033 /// This abstract interface provides operations for creating
0034 /// containers for serialized ASTs (precompiled headers and clang
0035 /// modules).
0036 class PCHContainerWriter {
0037 public:
0038   virtual ~PCHContainerWriter() = 0;
0039   virtual llvm::StringRef getFormat() const = 0;
0040 
0041   /// Return an ASTConsumer that can be chained with a
0042   /// PCHGenerator that produces a wrapper file format containing a
0043   /// serialized AST bitstream.
0044   virtual std::unique_ptr<ASTConsumer>
0045   CreatePCHContainerGenerator(CompilerInstance &CI,
0046                               const std::string &MainFileName,
0047                               const std::string &OutputFileName,
0048                               std::unique_ptr<llvm::raw_pwrite_stream> OS,
0049                               std::shared_ptr<PCHBuffer> Buffer) const = 0;
0050 };
0051 
0052 /// This abstract interface provides operations for unwrapping
0053 /// containers for serialized ASTs (precompiled headers and clang
0054 /// modules).
0055 class PCHContainerReader {
0056 public:
0057   virtual ~PCHContainerReader() = 0;
0058   /// Equivalent to the format passed to -fmodule-format=
0059   virtual llvm::ArrayRef<llvm::StringRef> getFormats() const = 0;
0060 
0061   /// Returns the serialized AST inside the PCH container Buffer.
0062   virtual llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const = 0;
0063 };
0064 
0065 /// Implements write operations for a raw pass-through PCH container.
0066 class RawPCHContainerWriter : public PCHContainerWriter {
0067   llvm::StringRef getFormat() const override { return "raw"; }
0068 
0069   /// Return an ASTConsumer that can be chained with a
0070   /// PCHGenerator that writes the module to a flat file.
0071   std::unique_ptr<ASTConsumer>
0072   CreatePCHContainerGenerator(CompilerInstance &CI,
0073                               const std::string &MainFileName,
0074                               const std::string &OutputFileName,
0075                               std::unique_ptr<llvm::raw_pwrite_stream> OS,
0076                               std::shared_ptr<PCHBuffer> Buffer) const override;
0077 };
0078 
0079 /// Implements read operations for a raw pass-through PCH container.
0080 class RawPCHContainerReader : public PCHContainerReader {
0081   llvm::ArrayRef<llvm::StringRef> getFormats() const override;
0082   /// Simply returns the buffer contained in Buffer.
0083   llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
0084 };
0085 
0086 /// A registry of PCHContainerWriter and -Reader objects for different formats.
0087 class PCHContainerOperations {
0088   llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
0089   llvm::StringMap<PCHContainerReader *> Readers;
0090   llvm::SmallVector<std::unique_ptr<PCHContainerReader>> OwnedReaders;
0091 
0092 public:
0093   /// Automatically registers a RawPCHContainerWriter and
0094   /// RawPCHContainerReader.
0095   PCHContainerOperations();
0096   void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
0097     Writers[Writer->getFormat()] = std::move(Writer);
0098   }
0099   void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
0100     assert(!Reader->getFormats().empty() &&
0101            "PCHContainerReader must handle >=1 format");
0102     for (llvm::StringRef Fmt : Reader->getFormats())
0103       Readers[Fmt] = Reader.get();
0104     OwnedReaders.push_back(std::move(Reader));
0105   }
0106   const PCHContainerWriter *getWriterOrNull(llvm::StringRef Format) {
0107     return Writers[Format].get();
0108   }
0109   const PCHContainerReader *getReaderOrNull(llvm::StringRef Format) {
0110     return Readers[Format];
0111   }
0112   const PCHContainerReader &getRawReader() {
0113     return *getReaderOrNull("raw");
0114   }
0115 };
0116 
0117 }
0118 
0119 #endif