PCHContainerOperations.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //=== Serialization/PCHContainerOperations.cpp - PCH Containers -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines PCHContainerOperations and RawPCHContainerOperation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Serialization/PCHContainerOperations.h"
  13. #include "clang/AST/ASTConsumer.h"
  14. #include "clang/Lex/ModuleLoader.h"
  15. #include "llvm/Bitstream/BitstreamReader.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <utility>
  18. using namespace clang;
  19. PCHContainerWriter::~PCHContainerWriter() {}
  20. PCHContainerReader::~PCHContainerReader() {}
  21. namespace {
  22. /// A PCHContainerGenerator that writes out the PCH to a flat file.
  23. class RawPCHContainerGenerator : public ASTConsumer {
  24. std::shared_ptr<PCHBuffer> Buffer;
  25. std::unique_ptr<raw_pwrite_stream> OS;
  26. public:
  27. RawPCHContainerGenerator(std::unique_ptr<llvm::raw_pwrite_stream> OS,
  28. std::shared_ptr<PCHBuffer> Buffer)
  29. : Buffer(std::move(Buffer)), OS(std::move(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, std::unique_ptr<llvm::raw_pwrite_stream> OS,
  46. std::shared_ptr<PCHBuffer> Buffer) const {
  47. return std::make_unique<RawPCHContainerGenerator>(std::move(OS), Buffer);
  48. }
  49. StringRef
  50. RawPCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
  51. return Buffer.getBuffer();
  52. }
  53. PCHContainerOperations::PCHContainerOperations() {
  54. registerWriter(std::make_unique<RawPCHContainerWriter>());
  55. registerReader(std::make_unique<RawPCHContainerReader>());
  56. }