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