clang  15.0.0git
PCHContainerOperations.h
Go to the documentation of this file.
1 //===--- Serialization/PCHContainerOperations.h - PCH Containers --*- C++ -*-===//
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 
9 #ifndef LLVM_CLANG_SERIALIZATION_PCHCONTAINEROPERATIONS_H
10 #define LLVM_CLANG_SERIALIZATION_PCHCONTAINEROPERATIONS_H
11 
12 #include "clang/Basic/Module.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include <memory>
17 
18 namespace llvm {
19 class raw_pwrite_stream;
20 }
21 
22 namespace clang {
23 
24 class ASTConsumer;
25 class CompilerInstance;
26 
27 struct PCHBuffer {
30  bool IsComplete;
31 };
32 
33 /// This abstract interface provides operations for creating
34 /// containers for serialized ASTs (precompiled headers and clang
35 /// modules).
37 public:
38  virtual ~PCHContainerWriter() = 0;
39  virtual llvm::StringRef getFormat() const = 0;
40 
41  /// Return an ASTConsumer that can be chained with a
42  /// PCHGenerator that produces a wrapper file format containing a
43  /// serialized AST bitstream.
44  virtual std::unique_ptr<ASTConsumer>
46  const std::string &MainFileName,
47  const std::string &OutputFileName,
48  std::unique_ptr<llvm::raw_pwrite_stream> OS,
49  std::shared_ptr<PCHBuffer> Buffer) const = 0;
50 };
51 
52 /// This abstract interface provides operations for unwrapping
53 /// containers for serialized ASTs (precompiled headers and clang
54 /// modules).
56 public:
57  virtual ~PCHContainerReader() = 0;
58  /// Equivalent to the format passed to -fmodule-format=
59  virtual llvm::StringRef getFormat() const = 0;
60 
61  /// Returns the serialized AST inside the PCH container Buffer.
62  virtual llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const = 0;
63 };
64 
65 /// Implements write operations for a raw pass-through PCH container.
67  llvm::StringRef getFormat() const override { return "raw"; }
68 
69  /// Return an ASTConsumer that can be chained with a
70  /// PCHGenerator that writes the module to a flat file.
71  std::unique_ptr<ASTConsumer>
72  CreatePCHContainerGenerator(CompilerInstance &CI,
73  const std::string &MainFileName,
74  const std::string &OutputFileName,
75  std::unique_ptr<llvm::raw_pwrite_stream> OS,
76  std::shared_ptr<PCHBuffer> Buffer) const override;
77 };
78 
79 /// Implements read operations for a raw pass-through PCH container.
81  llvm::StringRef getFormat() const override { return "raw"; }
82 
83  /// Simply returns the buffer contained in Buffer.
84  llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const override;
85 };
86 
87 /// A registry of PCHContainerWriter and -Reader objects for different formats.
89  llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
90  llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
91 public:
92  /// Automatically registers a RawPCHContainerWriter and
93  /// RawPCHContainerReader.
95  void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
96  Writers[Writer->getFormat()] = std::move(Writer);
97  }
98  void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
99  Readers[Reader->getFormat()] = std::move(Reader);
100  }
101  const PCHContainerWriter *getWriterOrNull(llvm::StringRef Format) {
102  return Writers[Format].get();
103  }
104  const PCHContainerReader *getReaderOrNull(llvm::StringRef Format) {
105  return Readers[Format].get();
106  }
108  return *getReaderOrNull("raw");
109  }
110 };
111 
112 }
113 
114 #endif
clang::PCHContainerOperations
A registry of PCHContainerWriter and -Reader objects for different formats.
Definition: PCHContainerOperations.h:88
clang::PCHContainerOperations::getReaderOrNull
const PCHContainerReader * getReaderOrNull(llvm::StringRef Format)
Definition: PCHContainerOperations.h:104
llvm
YAML serialization mapping.
Definition: Dominators.h:30
clang::PCHContainerOperations::getRawReader
const PCHContainerReader & getRawReader()
Definition: PCHContainerOperations.h:107
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PCHContainerOperations::PCHContainerOperations
PCHContainerOperations()
Automatically registers a RawPCHContainerWriter and RawPCHContainerReader.
Definition: PCHContainerOperations.cpp:65
llvm::SmallVector< char, 0 >
Module.h
clang::PCHContainerReader
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
Definition: PCHContainerOperations.h:55
clang::PCHContainerOperations::getWriterOrNull
const PCHContainerWriter * getWriterOrNull(llvm::StringRef Format)
Definition: PCHContainerOperations.h:101
clang::PCHContainerWriter
This abstract interface provides operations for creating containers for serialized ASTs (precompiled ...
Definition: PCHContainerOperations.h:36
clang::PCHContainerReader::getFormat
virtual llvm::StringRef getFormat() const =0
Equivalent to the format passed to -fmodule-format=.
clang::RawPCHContainerWriter
Implements write operations for a raw pass-through PCH container.
Definition: PCHContainerOperations.h:66
clang::ASTFileSignature
The signature of a module, which is a hash of the AST content.
Definition: Module.h:56
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::PCHContainerOperations::registerReader
void registerReader(std::unique_ptr< PCHContainerReader > Reader)
Definition: PCHContainerOperations.h:98
clang::PCHContainerWriter::~PCHContainerWriter
virtual ~PCHContainerWriter()=0
Definition: PCHContainerOperations.cpp:22
clang::PCHContainerOperations::registerWriter
void registerWriter(std::unique_ptr< PCHContainerWriter > Writer)
Definition: PCHContainerOperations.h:95
clang::PCHBuffer
Definition: PCHContainerOperations.h:27
clang
Definition: CalledOnceCheck.h:17
clang::PCHContainerWriter::CreatePCHContainerGenerator
virtual std::unique_ptr< ASTConsumer > CreatePCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName, const std::string &OutputFileName, std::unique_ptr< llvm::raw_pwrite_stream > OS, std::shared_ptr< PCHBuffer > Buffer) const =0
Return an ASTConsumer that can be chained with a PCHGenerator that produces a wrapper file format con...
clang::PCHBuffer::Signature
ASTFileSignature Signature
Definition: PCHContainerOperations.h:28
clang::PCHContainerReader::ExtractPCH
virtual llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const =0
Returns the serialized AST inside the PCH container Buffer.
clang::PCHBuffer::IsComplete
bool IsComplete
Definition: PCHContainerOperations.h:30
clang::PCHContainerWriter::getFormat
virtual llvm::StringRef getFormat() const =0
clang::PCHBuffer::Data
llvm::SmallVector< char, 0 > Data
Definition: PCHContainerOperations.h:29
clang::RawPCHContainerReader
Implements read operations for a raw pass-through PCH container.
Definition: PCHContainerOperations.h:80
clang::PCHContainerReader::~PCHContainerReader
virtual ~PCHContainerReader()=0
Definition: PCHContainerOperations.cpp:23