1 //===--- Frontend/PCHContainerOperations.cpp - PCH Containers ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines PCHContainerOperations and RawPCHContainerOperation.
11 //
12 //===----------------------------------------------------------------------===//
13
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;
20
21 namespace {
22
23 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
24 class RawPCHContainerGenerator : public ASTConsumer {
25 std::shared_ptr<PCHBuffer> Buffer;
26 raw_pwrite_stream *OS;
27
28 public:
RawPCHContainerGenerator(DiagnosticsEngine & Diags,const HeaderSearchOptions & HSO,const PreprocessorOptions & PPO,const TargetOptions & TO,const LangOptions & LO,const std::string & MainFileName,const std::string & OutputFileName,llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer)29 RawPCHContainerGenerator(DiagnosticsEngine &Diags,
30 const HeaderSearchOptions &HSO,
31 const PreprocessorOptions &PPO,
32 const TargetOptions &TO, const LangOptions &LO,
33 const std::string &MainFileName,
34 const std::string &OutputFileName,
35 llvm::raw_pwrite_stream *OS,
36 std::shared_ptr<PCHBuffer> Buffer)
37 : Buffer(Buffer), OS(OS) {}
38
~RawPCHContainerGenerator()39 virtual ~RawPCHContainerGenerator() {}
40
HandleTranslationUnit(ASTContext & Ctx)41 void HandleTranslationUnit(ASTContext &Ctx) override {
42 if (Buffer->IsComplete) {
43 // Make sure it hits disk now.
44 *OS << Buffer->Data;
45 OS->flush();
46 }
47 // Free the space of the temporary buffer.
48 llvm::SmallVector<char, 0> Empty;
49 Buffer->Data = std::move(Empty);
50 }
51 };
52 }
53
CreatePCHContainerGenerator(DiagnosticsEngine & Diags,const HeaderSearchOptions & HSO,const PreprocessorOptions & PPO,const TargetOptions & TO,const LangOptions & LO,const std::string & MainFileName,const std::string & OutputFileName,llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer) const54 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::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<RawPCHContainerGenerator>(
61 Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
62 }
63
ExtractPCH(llvm::MemoryBufferRef Buffer,llvm::BitstreamReader & StreamFile) const64 void RawPCHContainerReader::ExtractPCH(
65 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
66 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
67 (const unsigned char *)Buffer.getBufferEnd());
68 }
69
PCHContainerOperations()70 PCHContainerOperations::PCHContainerOperations() {
71 registerWriter(llvm::make_unique<RawPCHContainerWriter>());
72 registerReader(llvm::make_unique<RawPCHContainerReader>());
73 }
74