Path: blob/main/contrib/llvm-project/clang/lib/Serialization/PCHContainerOperations.cpp
35234 views
//=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines PCHContainerOperations and RawPCHContainerOperation.9//10//===----------------------------------------------------------------------===//1112#include "clang/Serialization/PCHContainerOperations.h"13#include "clang/AST/ASTConsumer.h"14#include "llvm/Support/raw_ostream.h"15#include <utility>1617using namespace clang;1819PCHContainerWriter::~PCHContainerWriter() {}20PCHContainerReader::~PCHContainerReader() {}2122namespace {2324/// A PCHContainerGenerator that writes out the PCH to a flat file.25class RawPCHContainerGenerator : public ASTConsumer {26std::shared_ptr<PCHBuffer> Buffer;27std::unique_ptr<raw_pwrite_stream> OS;2829public:30RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,31std::shared_ptr<PCHBuffer> Buffer)32: Buffer(std::move(Buffer)), OS(std::move(OS)) {}3334~RawPCHContainerGenerator() override = default;3536void HandleTranslationUnit(ASTContext &Ctx) override {37if (Buffer->IsComplete) {38// Make sure it hits disk now.39*OS << Buffer->Data;40OS->flush();41}42// Free the space of the temporary buffer.43llvm::SmallVector<char, 0> Empty;44Buffer->Data = std::move(Empty);45}46};4748} // anonymous namespace4950std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(51CompilerInstance &CI, const std::string &MainFileName,52const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,53std::shared_ptr<PCHBuffer> Buffer) const {54return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);55}5657ArrayRef<llvm::StringRef> RawPCHContainerReader::getFormats() const {58static StringRef Raw("raw");59return ArrayRef(Raw);60}6162StringRef63RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {64return Buffer.getBuffer();65}6667PCHContainerOperations::PCHContainerOperations() {68registerWriter(std::make_unique<RawPCHContainerWriter>());69registerReader(std::make_unique<RawPCHContainerReader>());70}717273