1 //===--- Frontend/PCHContainerOperations.cpp - PCH Containers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines PCHContainerOperations and RawPCHContainerOperation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/PCHContainerOperations.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "llvm/Bitcode/BitstreamReader.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "clang/Lex/ModuleLoader.h"
20 using namespace clang;
24 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
25 class RawPCHContainerGenerator : public ASTConsumer {
26 std::shared_ptr<PCHBuffer> Buffer;
27 raw_pwrite_stream *OS;
30 RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
31 std::shared_ptr<PCHBuffer> Buffer)
32 : Buffer(Buffer), OS(OS) {}
34 ~RawPCHContainerGenerator() override = default;
36 void HandleTranslationUnit(ASTContext &Ctx) override {
37 if (Buffer->IsComplete) {
38 // Make sure it hits disk now.
42 // Free the space of the temporary buffer.
43 llvm::SmallVector<char, 0> Empty;
44 Buffer->Data = std::move(Empty);
48 } // anonymous namespace
50 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
51 CompilerInstance &CI, const std::string &MainFileName,
52 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
53 std::shared_ptr<PCHBuffer> Buffer) const {
54 return llvm::make_unique<RawPCHContainerGenerator>(OS, Buffer);
57 void RawPCHContainerReader::ExtractPCH(
58 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
59 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
60 (const unsigned char *)Buffer.getBufferEnd());
63 PCHContainerOperations::PCHContainerOperations() {
64 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
65 registerReader(llvm::make_unique<RawPCHContainerReader>());