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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chandler Carruth | ae89987 | 2011-12-09 01:45:42 +0000 | [diff] [blame] | 10 | // This file defines the PCHGenerator, which as a SemaConsumer that generates |
| 11 | // a PCH file. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Sebastian Redl | 1914c6f | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 15 | #include "clang/Serialization/ASTWriter.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/BitstreamWriter.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | using namespace clang; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 26 | PCHGenerator::PCHGenerator( |
| 27 | const Preprocessor &PP, StringRef OutputFile, |
| 28 | clang::Module *Module, StringRef isysroot, |
| 29 | std::shared_ptr<PCHBuffer> Buffer, |
| 30 | ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions, |
| 31 | bool AllowASTWithErrors, bool IncludeTimestamps) |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 32 | : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()), |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 33 | SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data), |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 34 | Writer(Stream, Extensions, IncludeTimestamps), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 35 | AllowASTWithErrors(AllowASTWithErrors) { |
| 36 | Buffer->IsComplete = false; |
Douglas Gregor | 77d993d | 2011-07-22 06:03:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 39 | PCHGenerator::~PCHGenerator() { |
| 40 | } |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 42 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { |
Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 43 | // Don't create a PCH if there were fatal failures during module loading. |
| 44 | if (PP.getModuleLoader().HadFatalFailure) |
| 45 | return; |
| 46 | |
| 47 | bool hasErrors = PP.getDiagnostics().hasErrorOccurred(); |
| 48 | if (hasErrors && !AllowASTWithErrors) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 49 | return; |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 50 | |
| 51 | // Emit the PCH file to the Buffer. |
Douglas Gregor | 162dd02 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 52 | assert(SemaPtr && "No Sema?"); |
Adrian Prantl | a5206ce | 2015-09-22 23:26:43 +0000 | [diff] [blame] | 53 | Buffer->Signature = |
Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 54 | Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, |
| 55 | // For serialization we are lenient if the errors were |
| 56 | // only warn-as-error kind. |
| 57 | PP.getDiagnostics().hasUncompilableErrorOccurred()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 58 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 59 | Buffer->IsComplete = true; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 62 | ASTMutationListener *PCHGenerator::GetASTMutationListener() { |
Douglas Gregor | 36db4f9 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 63 | return &Writer; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Sebastian Redl | 3e31c72 | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 66 | ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 67 | return &Writer; |
| 68 | } |