1 //===--- Frontend/PCHContainerOperations.cpp - PCH Containers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines PCHContainerOperations and RawPCHContainerOperation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/PCHContainerOperations.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "llvm/Bitcode/BitstreamReader.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "clang/Lex/ModuleLoader.h"
19 using namespace clang;
23 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
24 class PCHContainerGenerator : public ASTConsumer {
25 std::shared_ptr<PCHBuffer> Buffer;
26 raw_pwrite_stream *OS;
29 PCHContainerGenerator(DiagnosticsEngine &Diags,
30 const HeaderSearchOptions &HSO,
31 const PreprocessorOptions &PPO, const TargetOptions &TO,
32 const LangOptions &LO, const std::string &MainFileName,
33 const std::string &OutputFileName,
34 llvm::raw_pwrite_stream *OS,
35 std::shared_ptr<PCHBuffer> Buffer)
36 : Buffer(Buffer), OS(OS) {}
38 virtual ~PCHContainerGenerator() {}
40 void HandleTranslationUnit(ASTContext &Ctx) override {
41 if (Buffer->IsComplete) {
42 // Make sure it hits disk now.
46 // Free the space of the temporary buffer.
47 llvm::SmallVector<char, 0> Empty;
48 Buffer->Data = std::move(Empty);
53 std::unique_ptr<ASTConsumer>
54 RawPCHContainerOperations::CreatePCHContainerGenerator(
55 DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
56 const PreprocessorOptions &PPO, const TargetOptions &TO,
57 const LangOptions &LO, const std::string &MainFileName,
58 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
59 std::shared_ptr<PCHBuffer> Buffer) const {
60 return llvm::make_unique<PCHContainerGenerator>(
61 Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
64 void RawPCHContainerOperations::ExtractPCH(
65 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
66 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
67 (const unsigned char *)Buffer.getBufferEnd());