xref: /freebsd-src/contrib/llvm-project/clang/lib/Serialization/PCHContainerOperations.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file defines PCHContainerOperations and RawPCHContainerOperation.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "clang/Serialization/PCHContainerOperations.h"
140b57cec5SDimitry Andric #include "clang/AST/ASTConsumer.h"
150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
160b57cec5SDimitry Andric #include <utility>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace clang;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric PCHContainerWriter::~PCHContainerWriter() {}
210b57cec5SDimitry Andric PCHContainerReader::~PCHContainerReader() {}
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace {
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric /// A PCHContainerGenerator that writes out the PCH to a flat file.
260b57cec5SDimitry Andric class RawPCHContainerGenerator : public ASTConsumer {
270b57cec5SDimitry Andric   std::shared_ptr<PCHBuffer> Buffer;
280b57cec5SDimitry Andric   std::unique_ptr<raw_pwrite_stream> OS;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric public:
310b57cec5SDimitry Andric   RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
320b57cec5SDimitry Andric                            std::shared_ptr<PCHBuffer> Buffer)
330b57cec5SDimitry Andric       : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   ~RawPCHContainerGenerator() override = default;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   void HandleTranslationUnit(ASTContext &Ctx) override {
380b57cec5SDimitry Andric     if (Buffer->IsComplete) {
390b57cec5SDimitry Andric       // Make sure it hits disk now.
400b57cec5SDimitry Andric       *OS << Buffer->Data;
410b57cec5SDimitry Andric       OS->flush();
420b57cec5SDimitry Andric     }
430b57cec5SDimitry Andric     // Free the space of the temporary buffer.
440b57cec5SDimitry Andric     llvm::SmallVector<char, 0> Empty;
450b57cec5SDimitry Andric     Buffer->Data = std::move(Empty);
460b57cec5SDimitry Andric   }
470b57cec5SDimitry Andric };
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric } // anonymous namespace
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
520b57cec5SDimitry Andric     CompilerInstance &CI, const std::string &MainFileName,
530b57cec5SDimitry Andric     const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
540b57cec5SDimitry Andric     std::shared_ptr<PCHBuffer> Buffer) const {
55a7dea167SDimitry Andric   return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
58*06c3fb27SDimitry Andric ArrayRef<llvm::StringRef> RawPCHContainerReader::getFormats() const {
59*06c3fb27SDimitry Andric   static StringRef Raw("raw");
60*06c3fb27SDimitry Andric   return ArrayRef(Raw);
61*06c3fb27SDimitry Andric }
62*06c3fb27SDimitry Andric 
630b57cec5SDimitry Andric StringRef
640b57cec5SDimitry Andric RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
650b57cec5SDimitry Andric   return Buffer.getBuffer();
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric PCHContainerOperations::PCHContainerOperations() {
69a7dea167SDimitry Andric   registerWriter(std::make_unique<RawPCHContainerWriter>());
70a7dea167SDimitry Andric   registerReader(std::make_unique<RawPCHContainerReader>());
710b57cec5SDimitry Andric }
72