| Chandler Carruth | ae89987 | 2011-12-09 01:45:42 +0000 | [diff] [blame] | 1 | //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===// | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
| Chandler Carruth | ae89987 | 2011-12-09 01:45:42 +0000 | [diff] [blame] | 9 | //  This file defines the PCHGenerator, which as a SemaConsumer that generates | 
|  | 10 | //  a PCH file. | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" | 
| Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 15 | #include "clang/Lex/HeaderSearch.h" | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" | 
|  | 17 | #include "clang/Sema/SemaConsumer.h" | 
| Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 18 | #include "clang/Serialization/ASTWriter.h" | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/BitstreamWriter.h" | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 |  | 
|  | 21 | using namespace clang; | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 |  | 
| Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 23 | PCHGenerator::PCHGenerator( | 
| Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 24 | const Preprocessor &PP, InMemoryModuleCache &ModuleCache, | 
|  | 25 | StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer, | 
| David Blaikie | 61137e1 | 2017-01-05 18:23:18 +0000 | [diff] [blame] | 26 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, | 
| Duncan P. N. Exon Smith | 70d759b | 2019-03-12 18:38:04 +0000 | [diff] [blame] | 27 | bool AllowASTWithErrors, bool IncludeTimestamps, | 
|  | 28 | bool ShouldCacheASTInMemory) | 
| Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 29 | : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()), | 
| Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 30 | SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data), | 
| Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 31 | Writer(Stream, this->Buffer->Data, ModuleCache, Extensions, | 
| Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 32 | IncludeTimestamps), | 
| Duncan P. N. Exon Smith | 70d759b | 2019-03-12 18:38:04 +0000 | [diff] [blame] | 33 | AllowASTWithErrors(AllowASTWithErrors), | 
|  | 34 | ShouldCacheASTInMemory(ShouldCacheASTInMemory) { | 
| Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 35 | this->Buffer->IsComplete = false; | 
| Douglas Gregor | 77d993d | 2011-07-22 06:03:18 +0000 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
| Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 38 | PCHGenerator::~PCHGenerator() { | 
|  | 39 | } | 
| Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 40 |  | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 41 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { | 
| Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 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) | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 48 | return; | 
| Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 49 |  | 
| Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 50 | Module *Module = nullptr; | 
| Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 51 | if (PP.getLangOpts().isCompilingModule()) { | 
| Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 52 | Module = PP.getHeaderSearchInfo().lookupModule( | 
|  | 53 | PP.getLangOpts().CurrentModule, /*AllowSearch*/ false); | 
| Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 54 | if (!Module) { | 
|  | 55 | assert(hasErrors && "emitting module but current module doesn't exist"); | 
|  | 56 | return; | 
|  | 57 | } | 
| Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
| Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 60 | // Emit the PCH file to the Buffer. | 
| Douglas Gregor | 162dd02 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 61 | assert(SemaPtr && "No Sema?"); | 
| Adrian Prantl | a5206ce | 2015-09-22 23:26:43 +0000 | [diff] [blame] | 62 | Buffer->Signature = | 
| Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 63 | Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, | 
|  | 64 | // For serialization we are lenient if the errors were | 
|  | 65 | // only warn-as-error kind. | 
| Duncan P. N. Exon Smith | 70d759b | 2019-03-12 18:38:04 +0000 | [diff] [blame] | 66 | PP.getDiagnostics().hasUncompilableErrorOccurred(), | 
|  | 67 | ShouldCacheASTInMemory); | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 68 |  | 
| Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 69 | Buffer->IsComplete = true; | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 72 | ASTMutationListener *PCHGenerator::GetASTMutationListener() { | 
| Douglas Gregor | 36db4f9 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 73 | return &Writer; | 
| Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Sebastian Redl | 3e31c72 | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 76 | ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { | 
| Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 77 | return &Writer; | 
|  | 78 | } |