GeneratePCH.cpp 2.1 KB

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