GeneratePCH.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/AST/ASTConsumer.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/Basic/FileManager.h"
  18. #include "clang/Lex/Preprocessor.h"
  19. #include "clang/Sema/SemaConsumer.h"
  20. #include "llvm/Bitcode/BitstreamWriter.h"
  21. #include <string>
  22. using namespace clang;
  23. PCHGenerator::PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
  24. clang::Module *Module, StringRef isysroot,
  25. std::shared_ptr<PCHBuffer> Buffer,
  26. bool AllowASTWithErrors)
  27. : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
  28. SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data), Writer(Stream),
  29. AllowASTWithErrors(AllowASTWithErrors) {
  30. Buffer->IsComplete = false;
  31. }
  32. PCHGenerator::~PCHGenerator() {
  33. }
  34. void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
  35. // Don't create a PCH if there were fatal failures during module loading.
  36. if (PP.getModuleLoader().HadFatalFailure)
  37. return;
  38. bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
  39. if (hasErrors && !AllowASTWithErrors)
  40. return;
  41. // Emit the PCH file to the Buffer.
  42. assert(SemaPtr && "No Sema?");
  43. Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
  44. Buffer->IsComplete = true;
  45. }
  46. ASTMutationListener *PCHGenerator::GetASTMutationListener() {
  47. return &Writer;
  48. }
  49. ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
  50. return &Writer;
  51. }