GeneratePCH.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- 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 the PCHGenerator, which as a SemaConsumer that generates
  10. // a PCH file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/Lex/HeaderSearch.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include "clang/Sema/SemaConsumer.h"
  17. #include "clang/Serialization/ASTWriter.h"
  18. #include "llvm/Bitstream/BitstreamWriter.h"
  19. using namespace clang;
  20. PCHGenerator::PCHGenerator(
  21. const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
  22. StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
  23. ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
  24. bool AllowASTWithErrors, bool IncludeTimestamps,
  25. bool ShouldCacheASTInMemory)
  26. : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
  27. SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
  28. Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
  29. IncludeTimestamps),
  30. AllowASTWithErrors(AllowASTWithErrors),
  31. ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
  32. this->Buffer->IsComplete = false;
  33. }
  34. PCHGenerator::~PCHGenerator() {
  35. }
  36. void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
  37. // Don't create a PCH if there were fatal failures during module loading.
  38. if (PP.getModuleLoader().HadFatalFailure)
  39. return;
  40. bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
  41. if (hasErrors && !AllowASTWithErrors)
  42. return;
  43. Module *Module = nullptr;
  44. if (PP.getLangOpts().isCompilingModule()) {
  45. Module = PP.getHeaderSearchInfo().lookupModule(
  46. PP.getLangOpts().CurrentModule, SourceLocation(),
  47. /*AllowSearch*/ false);
  48. if (!Module) {
  49. assert(hasErrors && "emitting module but current module doesn't exist");
  50. return;
  51. }
  52. }
  53. // Errors that do not prevent the PCH from being written should not cause the
  54. // overall compilation to fail either.
  55. if (AllowASTWithErrors)
  56. PP.getDiagnostics().getClient()->clear();
  57. // Emit the PCH file to the Buffer.
  58. assert(SemaPtr && "No Sema?");
  59. Buffer->Signature =
  60. Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
  61. // For serialization we are lenient if the errors were
  62. // only warn-as-error kind.
  63. PP.getDiagnostics().hasUncompilableErrorOccurred(),
  64. ShouldCacheASTInMemory);
  65. Buffer->IsComplete = true;
  66. }
  67. ASTMutationListener *PCHGenerator::GetASTMutationListener() {
  68. return &Writer;
  69. }
  70. ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
  71. return &Writer;
  72. }