clang  3.8.0
PCHContainerOperations.cpp
Go to the documentation of this file.
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 
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 
20 using namespace clang;
21 
22 namespace {
23 
24 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
25 class RawPCHContainerGenerator : public ASTConsumer {
26  std::shared_ptr<PCHBuffer> Buffer;
27  raw_pwrite_stream *OS;
28 
29 public:
30  RawPCHContainerGenerator(llvm::raw_pwrite_stream *OS,
31  std::shared_ptr<PCHBuffer> Buffer)
32  : Buffer(Buffer), OS(OS) {}
33 
34  ~RawPCHContainerGenerator() override = default;
35 
36  void HandleTranslationUnit(ASTContext &Ctx) override {
37  if (Buffer->IsComplete) {
38  // Make sure it hits disk now.
39  *OS << Buffer->Data;
40  OS->flush();
41  }
42  // Free the space of the temporary buffer.
44  Buffer->Data = std::move(Empty);
45  }
46 };
47 
48 } // anonymous namespace
49 
50 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
51  CompilerInstance &CI, const std::string &MainFileName,
52  const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
53  std::shared_ptr<PCHBuffer> Buffer) const {
54  return llvm::make_unique<RawPCHContainerGenerator>(OS, Buffer);
55 }
56 
57 void RawPCHContainerReader::ExtractPCH(
58  llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
59  StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
60  (const unsigned char *)Buffer.getBufferEnd());
61 }
62 
64  registerWriter(llvm::make_unique<RawPCHContainerWriter>());
65  registerReader(llvm::make_unique<RawPCHContainerReader>());
66 }
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:36
std::unique_ptr< llvm::MemoryBuffer > Buffer
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:91
void registerReader(std::unique_ptr< PCHContainerReader > Reader)
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
PCHContainerOperations()
Automatically registers a RawPCHContainerWriter and RawPCHContainerReader.
void registerWriter(std::unique_ptr< PCHContainerWriter > Writer)