PCHContainerOperations.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "clang/Frontend/PCHContainerOperations.h"
  14. #include "clang/AST/ASTConsumer.h"
  15. #include "clang/Lex/ModuleLoader.h"
  16. #include "llvm/Bitcode/BitstreamReader.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <utility>
  19. using namespace clang;
  20. PCHContainerWriter::~PCHContainerWriter() {}
  21. PCHContainerReader::~PCHContainerReader() {}
  22. namespace {
  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. std::unique_ptr<raw_pwrite_stream> OS;
  27. public:
  28. RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
  29. std::shared_ptr<PCHBuffer> Buffer)
  30. : Buffer(std::move(Buffer)), OS(std::move(OS)) {}
  31. ~RawPCHContainerGenerator() override = default;
  32. void HandleTranslationUnit(ASTContext &Ctx) override {
  33. if (Buffer->IsComplete) {
  34. // Make sure it hits disk now.
  35. *OS << Buffer->Data;
  36. OS->flush();
  37. }
  38. // Free the space of the temporary buffer.
  39. llvm::SmallVector<char, 0> Empty;
  40. Buffer->Data = std::move(Empty);
  41. }
  42. };
  43. } // anonymous namespace
  44. std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
  45. CompilerInstance &CI, const std::string &MainFileName,
  46. const std::string &OutputFileName, std::unique_ptr<llvm::raw_pwrite_stream> OS,
  47. std::shared_ptr<PCHBuffer> Buffer) const {
  48. return llvm::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
  49. }
  50. StringRef
  51. RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
  52. return Buffer.getBuffer();
  53. }
  54. PCHContainerOperations::PCHContainerOperations() {
  55. registerWriter(llvm::make_unique<RawPCHContainerWriter>());
  56. registerReader(llvm::make_unique<RawPCHContainerReader>());
  57. }