PCHContainerOperations.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. namespace {
  20. /// \brief A PCHContainerGenerator that writes out the PCH to a flat file.
  21. class RawPCHContainerGenerator : public ASTConsumer {
  22. std::shared_ptr<PCHBuffer> Buffer;
  23. raw_pwrite_stream *OS;
  24. public:
  25. RawPCHContainerGenerator(DiagnosticsEngine &Diags,
  26. const HeaderSearchOptions &HSO,
  27. const PreprocessorOptions &PPO,
  28. const TargetOptions &TO, const LangOptions &LO,
  29. const std::string &MainFileName,
  30. const std::string &OutputFileName,
  31. llvm::raw_pwrite_stream *OS,
  32. std::shared_ptr<PCHBuffer> Buffer)
  33. : Buffer(Buffer), OS(OS) {}
  34. virtual ~RawPCHContainerGenerator() {}
  35. void HandleTranslationUnit(ASTContext &Ctx) override {
  36. if (Buffer->IsComplete) {
  37. // Make sure it hits disk now.
  38. *OS << Buffer->Data;
  39. OS->flush();
  40. }
  41. // Free the space of the temporary buffer.
  42. llvm::SmallVector<char, 0> Empty;
  43. Buffer->Data = std::move(Empty);
  44. }
  45. };
  46. }
  47. std::unique_ptr<ASTConsumer> RawPCHContainerWriter::CreatePCHContainerGenerator(
  48. DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
  49. const PreprocessorOptions &PPO, const TargetOptions &TO,
  50. const LangOptions &LO, const std::string &MainFileName,
  51. const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
  52. std::shared_ptr<PCHBuffer> Buffer) const {
  53. return llvm::make_unique<RawPCHContainerGenerator>(
  54. Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
  55. }
  56. void RawPCHContainerReader::ExtractPCH(
  57. llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
  58. StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
  59. (const unsigned char *)Buffer.getBufferEnd());
  60. }
  61. PCHContainerOperations::PCHContainerOperations() {
  62. registerWriter(llvm::make_unique<RawPCHContainerWriter>());
  63. registerReader(llvm::make_unique<RawPCHContainerReader>());
  64. }