Chandler Carruth | 8bd25b4 | 2011-12-09 01:45:42 +0000 | [diff] [blame] | 1 | //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===// |
Douglas Gregor | 2cf2634 | 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 | 8bd25b4 | 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 | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Sebastian Redl | 7faa2ec | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 15 | #include "clang/Serialization/ASTWriter.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/BitstreamWriter.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | using namespace clang; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 26 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | PCHGenerator::PCHGenerator(const Preprocessor &PP, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 28 | StringRef OutputFile, |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 29 | clang::Module *Module, |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 30 | StringRef isysroot, |
Argyrios Kyrtzidis | 1f01f7c | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 31 | raw_ostream *OS, bool AllowASTWithErrors) |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 32 | : PP(PP), OutputFile(OutputFile), Module(Module), |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 33 | isysroot(isysroot.str()), Out(OS), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 34 | SemaPtr(nullptr), Stream(Buffer), Writer(Stream), |
Argyrios Kyrtzidis | 1f01f7c | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 35 | AllowASTWithErrors(AllowASTWithErrors), |
| 36 | HasEmittedPCH(false) { |
Douglas Gregor | 4947e25 | 2011-07-22 06:03:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | PCHGenerator::~PCHGenerator() { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 42 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { |
Argyrios Kyrtzidis | 1f01f7c | 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 | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 49 | return; |
Douglas Gregor | 89d9980 | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 51 | // Emit the PCH file |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 52 | assert(SemaPtr && "No Sema?"); |
Argyrios Kyrtzidis | 1f01f7c | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 53 | Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 55 | // Write the generated bitstream to "Out". |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 56 | Out->write((char *)&Buffer.front(), Buffer.size()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 57 | |
| 58 | // Make sure it hits disk now. |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 59 | Out->flush(); |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 60 | |
| 61 | // Free up some memory, in case the process is kept alive. |
| 62 | Buffer.clear(); |
Argyrios Kyrtzidis | 1f01f7c | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 63 | |
| 64 | HasEmittedPCH = true; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 67 | ASTMutationListener *PCHGenerator::GetASTMutationListener() { |
Douglas Gregor | 9293ba8 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 68 | return &Writer; |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Sebastian Redl | 571db7f | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 71 | ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 72 | return &Writer; |
| 73 | } |