clang  19.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 
14 #include "clang/AST/ASTConsumer.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <utility>
17 
18 using namespace clang;
19 
22 
23 namespace {
24 
25 /// A PCHContainerGenerator that writes out the PCH to a flat file.
26 class RawPCHContainerGenerator : public ASTConsumer {
27  std::shared_ptr<PCHBuffer> Buffer;
28  std::unique_ptr<raw_pwrite_stream> OS;
29 
30 public:
31  RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
32  std::shared_ptr<PCHBuffer> Buffer)
33  : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
34 
35  ~RawPCHContainerGenerator() override = default;
36 
37  void HandleTranslationUnit(ASTContext &Ctx) override {
38  if (Buffer->IsComplete) {
39  // Make sure it hits disk now.
40  *OS << Buffer->Data;
41  OS->flush();
42  }
43  // Free the space of the temporary buffer.
45  Buffer->Data = std::move(Empty);
46  }
47 };
48 
49 } // anonymous namespace
50 
51 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
52  CompilerInstance &CI, const std::string &MainFileName,
53  const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
54  std::shared_ptr<PCHBuffer> Buffer) const {
55  return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
56 }
57 
58 ArrayRef<llvm::StringRef> RawPCHContainerReader::getFormats() const {
59  static StringRef Raw("raw");
60  return ArrayRef(Raw);
61 }
62 
63 StringRef
64 RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
65  return Buffer.getBuffer();
66 }
67 
69  registerWriter(std::make_unique<RawPCHContainerWriter>());
70  registerReader(std::make_unique<RawPCHContainerReader>());
71 }
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:33
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
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)
The JSON file list parser is used to communicate input to InstallAPI.
Definition: Format.h:5433