[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / lib / Serialization / PCHContainerOperations.cpp
blobd4990fce2d99d9f5308951cbe426741d6b6351b9
1 //=== Serialization/PCHContainerOperations.cpp - 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 // This file defines PCHContainerOperations and RawPCHContainerOperation.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Serialization/PCHContainerOperations.h"
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/Lex/ModuleLoader.h"
16 #include "llvm/Bitstream/BitstreamReader.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <utility>
20 using namespace clang;
22 PCHContainerWriter::~PCHContainerWriter() {}
23 PCHContainerReader::~PCHContainerReader() {}
25 namespace {
27 /// A PCHContainerGenerator that writes out the PCH to a flat file.
28 class RawPCHContainerGenerator : public ASTConsumer {
29 std::shared_ptr<PCHBuffer> Buffer;
30 std::unique_ptr<raw_pwrite_stream> OS;
32 public:
33 RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
34 std::shared_ptr<PCHBuffer> Buffer)
35 : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
37 ~RawPCHContainerGenerator() override = default;
39 void HandleTranslationUnit(ASTContext &Ctx) override {
40 if (Buffer->IsComplete) {
41 // Make sure it hits disk now.
42 *OS << Buffer->Data;
43 OS->flush();
45 // Free the space of the temporary buffer.
46 llvm::SmallVector<char, 0> Empty;
47 Buffer->Data = std::move(Empty);
51 } // anonymous namespace
53 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
54 CompilerInstance &CI, const std::string &MainFileName,
55 const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
56 std::shared_ptr<PCHBuffer> Buffer) const {
57 return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
60 StringRef
61 RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
62 return Buffer.getBuffer();
65 PCHContainerOperations::PCHContainerOperations() {
66 registerWriter(std::make_unique<RawPCHContainerWriter>());
67 registerReader(std::make_unique<RawPCHContainerReader>());