clang 24.0.0git
PCHContainerOperations.cpp
Go to the documentation of this file.
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.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/Support/MemoryBuffer.h"
16#include "llvm/Support/raw_ostream.h"
17#include <utility>
18
19using namespace clang;
20
23
24namespace {
25
26/// A PCHContainerGenerator that writes out the PCH to a flat file.
27class RawPCHContainerGenerator : public ASTConsumer {
28 std::shared_ptr<PCHBuffer> Buffer;
29 std::unique_ptr<raw_pwrite_stream> OS;
30
31public:
32 RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
33 std::shared_ptr<PCHBuffer> Buffer)
34 : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
35
36 ~RawPCHContainerGenerator() override = default;
37
38 void HandleTranslationUnit(ASTContext &Ctx) override {
39 if (Buffer->IsComplete) {
40 // Make sure it hits disk now.
41 *OS << Buffer->Data;
42 OS->flush();
43 }
44 // Free the space of the temporary buffer.
45 llvm::SmallVector<char, 0> Empty;
46 Buffer->Data = std::move(Empty);
47 }
48};
49
50} // anonymous namespace
51
52std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
53 CompilerInstance &CI, const std::string &MainFileName,
54 const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
55 std::shared_ptr<PCHBuffer> Buffer) const {
56 return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
57}
58
60 static StringRef Raw("raw");
61 return ArrayRef(Raw);
62}
63
64std::unique_ptr<llvm::MemoryBuffer> RawPCHContainerReader::ExtractPCH(
65 std::unique_ptr<llvm::MemoryBuffer> Buffer) const {
66 return Buffer;
67}
68
70 registerWriter(std::make_unique<RawPCHContainerWriter>());
71 registerReader(std::make_unique<RawPCHContainerReader>());
72}
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition ASTConsumer.h:35
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:246
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void registerReader(std::unique_ptr< PCHContainerReader > Reader)
PCHContainerOperations()
Automatically registers a RawPCHContainerWriter and RawPCHContainerReader.
void registerWriter(std::unique_ptr< PCHContainerWriter > Writer)
virtual llvm::ArrayRef< llvm::StringRef > getFormats() const =0
Equivalent to the format passed to -fmodule-format=.
Top level wrappers for InstallAPI frontend operations.