clang  3.9.0
PCHContainerOperations.h
Go to the documentation of this file.
1 //===--- Frontend/PCHContainerOperations.h - PCH Containers -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_PCH_CONTAINER_OPERATIONS_H
11 #define LLVM_CLANG_PCH_CONTAINER_OPERATIONS_H
12 
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 class BitstreamReader;
21 }
22 
23 using llvm::StringRef;
24 
25 namespace clang {
26 
27 class ASTConsumer;
28 class CodeGenOptions;
29 class DiagnosticsEngine;
30 class CompilerInstance;
31 
32 struct PCHBuffer {
33  uint64_t Signature;
35  bool IsComplete;
36 };
37 
38 /// This abstract interface provides operations for creating
39 /// containers for serialized ASTs (precompiled headers and clang
40 /// modules).
42 public:
43  virtual ~PCHContainerWriter() = 0;
44  virtual StringRef getFormat() const = 0;
45 
46  /// Return an ASTConsumer that can be chained with a
47  /// PCHGenerator that produces a wrapper file format containing a
48  /// serialized AST bitstream.
49  virtual std::unique_ptr<ASTConsumer>
51  const std::string &MainFileName,
52  const std::string &OutputFileName,
53  std::unique_ptr<llvm::raw_pwrite_stream> OS,
54  std::shared_ptr<PCHBuffer> Buffer) const = 0;
55 };
56 
57 /// This abstract interface provides operations for unwrapping
58 /// containers for serialized ASTs (precompiled headers and clang
59 /// modules).
61 public:
62  virtual ~PCHContainerReader() = 0;
63  /// Equivalent to the format passed to -fmodule-format=
64  virtual StringRef getFormat() const = 0;
65 
66  /// Initialize an llvm::BitstreamReader with the serialized AST inside
67  /// the PCH container Buffer.
68  virtual void ExtractPCH(llvm::MemoryBufferRef Buffer,
69  llvm::BitstreamReader &StreamFile) const = 0;
70 };
71 
72 /// Implements write operations for a raw pass-through PCH container.
74  StringRef getFormat() const override { return "raw"; }
75 
76  /// Return an ASTConsumer that can be chained with a
77  /// PCHGenerator that writes the module to a flat file.
78  std::unique_ptr<ASTConsumer>
79  CreatePCHContainerGenerator(CompilerInstance &CI,
80  const std::string &MainFileName,
81  const std::string &OutputFileName,
82  std::unique_ptr<llvm::raw_pwrite_stream> OS,
83  std::shared_ptr<PCHBuffer> Buffer) const override;
84 };
85 
86 /// Implements read operations for a raw pass-through PCH container.
88  StringRef getFormat() const override { return "raw"; }
89 
90  /// Initialize an llvm::BitstreamReader with Buffer.
91  void ExtractPCH(llvm::MemoryBufferRef Buffer,
92  llvm::BitstreamReader &StreamFile) const override;
93 };
94 
95 /// A registry of PCHContainerWriter and -Reader objects for different formats.
97  llvm::StringMap<std::unique_ptr<PCHContainerWriter>> Writers;
98  llvm::StringMap<std::unique_ptr<PCHContainerReader>> Readers;
99 public:
100  /// Automatically registers a RawPCHContainerWriter and
101  /// RawPCHContainerReader.
103  void registerWriter(std::unique_ptr<PCHContainerWriter> Writer) {
104  Writers[Writer->getFormat()] = std::move(Writer);
105  }
106  void registerReader(std::unique_ptr<PCHContainerReader> Reader) {
107  Readers[Reader->getFormat()] = std::move(Reader);
108  }
109  const PCHContainerWriter *getWriterOrNull(StringRef Format) {
110  return Writers[Format].get();
111  }
112  const PCHContainerReader *getReaderOrNull(StringRef Format) {
113  return Readers[Format].get();
114  }
116  return *getReaderOrNull("raw");
117  }
118 };
119 
120 }
121 
122 #endif
std::unique_ptr< llvm::MemoryBuffer > Buffer
A registry of PCHContainerWriter and -Reader objects for different formats.
This abstract interface provides operations for creating containers for serialized ASTs (precompiled ...
const PCHContainerWriter * getWriterOrNull(StringRef Format)
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
void registerReader(std::unique_ptr< PCHContainerReader > Reader)
virtual StringRef getFormat() const =0
const PCHContainerReader & getRawReader()
virtual void ExtractPCH(llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const =0
Initialize an llvm::BitstreamReader with the serialized AST inside the PCH container Buffer...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
const PCHContainerReader * getReaderOrNull(StringRef Format)
virtual StringRef getFormat() const =0
Equivalent to the format passed to -fmodule-format=.
PCHContainerOperations()
Automatically registers a RawPCHContainerWriter and RawPCHContainerReader.
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...
Implements read operations for a raw pass-through PCH container.
llvm::SmallVector< char, 0 > Data
void registerWriter(std::unique_ptr< PCHContainerWriter > Writer)
Implements write operations for a raw pass-through PCH container.