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/Bitcode/BitstreamWriter.h" 20 21 using namespace clang; 22 23 PCHGenerator::PCHGenerator( 24 const Preprocessor &PP, StringRef OutputFile, StringRef isysroot, 25 std::shared_ptr<PCHBuffer> Buffer, 26 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 27 bool AllowASTWithErrors, bool IncludeTimestamps) 28 : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()), 29 SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data), 30 Writer(Stream, this->Buffer->Data, PP.getPCMCache(), Extensions, 31 IncludeTimestamps), 32 AllowASTWithErrors(AllowASTWithErrors) { 33 this->Buffer->IsComplete = false; 34 } 35 36 PCHGenerator::~PCHGenerator() { 37 } 38 39 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { 40 // Don't create a PCH if there were fatal failures during module loading. 41 if (PP.getModuleLoader().HadFatalFailure) 42 return; 43 44 bool hasErrors = PP.getDiagnostics().hasErrorOccurred(); 45 if (hasErrors && !AllowASTWithErrors) 46 return; 47 48 Module *Module = nullptr; 49 if (PP.getLangOpts().isCompilingModule()) { 50 Module = PP.getHeaderSearchInfo().lookupModule( 51 PP.getLangOpts().CurrentModule, /*AllowSearch*/ false); 52 if (!Module) { 53 assert(hasErrors && "emitting module but current module doesn't exist"); 54 return; 55 } 56 } 57 58 // Emit the PCH file to the Buffer. 59 assert(SemaPtr && "No Sema?"); 60 Buffer->Signature = 61 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, 62 // For serialization we are lenient if the errors were 63 // only warn-as-error kind. 64 PP.getDiagnostics().hasUncompilableErrorOccurred()); 65 66 Buffer->IsComplete = true; 67 } 68 69 ASTMutationListener *PCHGenerator::GetASTMutationListener() { 70 return &Writer; 71 } 72 73 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { 74 return &Writer; 75 } 76