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( |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 24 | const Preprocessor &PP, StringRef OutputFile, StringRef isysroot, |
| 25 | std::shared_ptr<PCHBuffer> Buffer, |
David Blaikie | 61137e1 | 2017-01-05 18:23:18 +0000 | [diff] [blame] | 26 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 27 | bool AllowASTWithErrors, bool IncludeTimestamps) |
| 28 | : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()), |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 29 | SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data), |
| 30 | Writer(Stream, this->Buffer->Data, PP.getPCMCache(), Extensions, |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 31 | IncludeTimestamps), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 32 | AllowASTWithErrors(AllowASTWithErrors) { |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 33 | this->Buffer->IsComplete = false; |
Douglas Gregor | 77d993d | 2011-07-22 06:03:18 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 36 | PCHGenerator::~PCHGenerator() { |
| 37 | } |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 39 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { |
Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 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) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 46 | return; |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 47 | |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 48 | Module *Module = nullptr; |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 49 | if (PP.getLangOpts().isCompilingModule()) { |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 50 | Module = PP.getHeaderSearchInfo().lookupModule( |
| 51 | PP.getLangOpts().CurrentModule, /*AllowSearch*/ false); |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 52 | if (!Module) { |
| 53 | assert(hasErrors && "emitting module but current module doesn't exist"); |
| 54 | return; |
| 55 | } |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 58 | // Emit the PCH file to the Buffer. |
Douglas Gregor | 162dd02 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 59 | assert(SemaPtr && "No Sema?"); |
Adrian Prantl | a5206ce | 2015-09-22 23:26:43 +0000 | [diff] [blame] | 60 | Buffer->Signature = |
Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 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()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 65 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 66 | Buffer->IsComplete = true; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 69 | ASTMutationListener *PCHGenerator::GetASTMutationListener() { |
Douglas Gregor | 36db4f9 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 70 | return &Writer; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Sebastian Redl | 3e31c72 | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 73 | ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 74 | return &Writer; |
| 75 | } |