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