GeneratePCH.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===--- GeneratePCH.cpp - AST Consumer for PCH Generation ------*- 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 the CreatePCHGenerate function, which creates an
  11. // ASTConsumer that generates a PCH file.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Frontend/ASTConsumers.h"
  15. #include "clang/Serialization/ASTWriter.h"
  16. #include "clang/Sema/SemaConsumer.h"
  17. #include "clang/AST/ASTContext.h"
  18. #include "clang/AST/ASTConsumer.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Basic/FileManager.h"
  21. #include "clang/Basic/FileSystemStatCache.h"
  22. #include "llvm/Bitcode/BitstreamWriter.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <string>
  25. using namespace clang;
  26. PCHGenerator::PCHGenerator(const Preprocessor &PP,
  27. StringRef OutputFile,
  28. clang::Module *Module,
  29. StringRef isysroot,
  30. raw_ostream *OS)
  31. : PP(PP), OutputFile(OutputFile), Module(Module),
  32. isysroot(isysroot.str()), Out(OS),
  33. SemaPtr(0), StatCalls(0), Stream(Buffer), Writer(Stream) {
  34. // Install a stat() listener to keep track of all of the stat()
  35. // calls.
  36. StatCalls = new MemorizeStatCalls();
  37. PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false);
  38. }
  39. PCHGenerator::~PCHGenerator() {
  40. }
  41. void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
  42. if (PP.getDiagnostics().hasErrorOccurred())
  43. return;
  44. // Emit the PCH file
  45. assert(SemaPtr && "No Sema?");
  46. Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, Module, isysroot);
  47. // Write the generated bitstream to "Out".
  48. Out->write((char *)&Buffer.front(), Buffer.size());
  49. // Make sure it hits disk now.
  50. Out->flush();
  51. // Free up some memory, in case the process is kept alive.
  52. Buffer.clear();
  53. }
  54. ASTMutationListener *PCHGenerator::GetASTMutationListener() {
  55. return &Writer;
  56. }
  57. ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
  58. return &Writer;
  59. }