clang  6.0.0svn
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 "clang/Lex/ModuleLoader.h"
17 #include "llvm/Bitcode/BitstreamReader.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <utility>
20 
21 using namespace clang;
22 
25 
26 namespace {
27 
28 /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
29 class RawPCHContainerGenerator : public ASTConsumer {
30  std::shared_ptr<PCHBuffer> Buffer;
31  std::unique_ptr<raw_pwrite_stream> OS;
32 
33 public:
34  RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
35  std::shared_ptr<PCHBuffer> Buffer)
36  : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
37 
38  ~RawPCHContainerGenerator() override = default;
39 
40  void HandleTranslationUnit(ASTContext &Ctx) override {
41  if (Buffer->IsComplete) {
42  // Make sure it hits disk now.
43  *OS << Buffer->Data;
44  OS->flush();
45  }
46  // Free the space of the temporary buffer.
48  Buffer->Data = std::move(Empty);
49  }
50 };
51 
52 } // anonymous namespace
53 
54 std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
55  CompilerInstance &CI, const std::string &MainFileName,
56  const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
57  std::shared_ptr<PCHBuffer> Buffer) const {
58  return llvm::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
59 }
60 
61 StringRef
62 RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
63  return Buffer.getBuffer();
64 }
65 
67  registerWriter(llvm::make_unique<RawPCHContainerWriter>());
68  registerReader(llvm::make_unique<RawPCHContainerReader>());
69 }
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:34
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Dataflow Directional Tag Classes.
PCHContainerOperations()
Automatically registers a RawPCHContainerWriter and RawPCHContainerReader.