blob: d4990fce2d99d9f5308951cbe426741d6b6351b9 [file] [log] [blame]
Richard Trieuf3b00462018-12-12 02:53:59 +00001//=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//
Adrian Prantlbb165fb2015-06-20 18:53:08 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Adrian Prantlbb165fb2015-06-20 18:53:08 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines PCHContainerOperations and RawPCHContainerOperation.
10//
11//===----------------------------------------------------------------------===//
12
Richard Trieuf3b00462018-12-12 02:53:59 +000013#include "clang/Serialization/PCHContainerOperations.h"
Adrian Prantlbb165fb2015-06-20 18:53:08 +000014#include "clang/AST/ASTConsumer.h"
Benjamin Kramercfeacf52016-05-27 14:27:13 +000015#include "clang/Lex/ModuleLoader.h"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +000016#include "llvm/Bitstream/BitstreamReader.h"
Adrian Prantlbb165fb2015-06-20 18:53:08 +000017#include "llvm/Support/raw_ostream.h"
Benjamin Kramercfeacf52016-05-27 14:27:13 +000018#include <utility>
Hans Wennborg7eb54642015-09-10 17:07:54 +000019
Adrian Prantlbb165fb2015-06-20 18:53:08 +000020using namespace clang;
21
Benjamin Kramer6277b182016-02-01 13:22:39 +000022PCHContainerWriter::~PCHContainerWriter() {}
23PCHContainerReader::~PCHContainerReader() {}
24
Adrian Prantlbb165fb2015-06-20 18:53:08 +000025namespace {
26
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000027/// A PCHContainerGenerator that writes out the PCH to a flat file.
Adrian Prantlfb2398d2015-07-17 01:19:54 +000028class RawPCHContainerGenerator : public ASTConsumer {
Adrian Prantlbb165fb2015-06-20 18:53:08 +000029 std::shared_ptr<PCHBuffer> Buffer;
Peter Collingbourne03f89072016-07-15 00:55:40 +000030 std::unique_ptr<raw_pwrite_stream> OS;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000031
32public:
Peter Collingbourne03f89072016-07-15 00:55:40 +000033 RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
Adrian Prantlfb2398d2015-07-17 01:19:54 +000034 std::shared_ptr<PCHBuffer> Buffer)
Peter Collingbourne03f89072016-07-15 00:55:40 +000035 : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
Adrian Prantlbb165fb2015-06-20 18:53:08 +000036
Hans Wennborg7eb54642015-09-10 17:07:54 +000037 ~RawPCHContainerGenerator() override = default;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000038
39 void HandleTranslationUnit(ASTContext &Ctx) override {
40 if (Buffer->IsComplete) {
41 // Make sure it hits disk now.
42 *OS << Buffer->Data;
43 OS->flush();
44 }
45 // Free the space of the temporary buffer.
46 llvm::SmallVector<char, 0> Empty;
47 Buffer->Data = std::move(Empty);
48 }
49};
Hans Wennborg7eb54642015-09-10 17:07:54 +000050
51} // anonymous namespace
Adrian Prantlbb165fb2015-06-20 18:53:08 +000052
Adrian Prantlfb2398d2015-07-17 01:19:54 +000053std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantl1e63b2b2015-09-19 21:42:52 +000054 CompilerInstance &CI, const std::string &MainFileName,
Peter Collingbourne03f89072016-07-15 00:55:40 +000055 const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
Adrian Prantl1e63b2b2015-09-19 21:42:52 +000056 std::shared_ptr<PCHBuffer> Buffer) const {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +000057 return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
Adrian Prantlbb165fb2015-06-20 18:53:08 +000058}
59
Peter Collingbourne77c89b62016-11-08 04:17:11 +000060StringRef
61RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
62 return Buffer.getBuffer();
Adrian Prantlbb165fb2015-06-20 18:53:08 +000063}
Adrian Prantlfb2398d2015-07-17 01:19:54 +000064
65PCHContainerOperations::PCHContainerOperations() {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +000066 registerWriter(std::make_unique<RawPCHContainerWriter>());
67 registerReader(std::make_unique<RawPCHContainerReader>());
Adrian Prantlfb2398d2015-07-17 01:19:54 +000068}