| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- GeneratePCH.cpp - AST Consumer for PCH Generation ------*- C++ -*-===// | 
|  | 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 | // | 
|  | 10 | //  This file defines the CreatePCHGenerate function, which creates an | 
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 11 | //  ASTConsumer that generates a PCH file. | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Eli Friedman | 9f30fc3 | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/ASTConsumers.h" | 
| Sebastian Redl | 1914c6f | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 16 | #include "clang/Serialization/ASTWriter.h" | 
| Douglas Gregor | 162dd02 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaConsumer.h" | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" | 
|  | 19 | #include "clang/AST/ASTConsumer.h" | 
| Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" | 
| Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/BitstreamWriter.h" | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 24 | #include <string> | 
|  | 25 |  | 
|  | 26 | using namespace clang; | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 27 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | PCHGenerator::PCHGenerator(const Preprocessor &PP, | 
| Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 29 | bool Chaining, | 
| Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 30 | const char *isysroot, | 
|  | 31 | llvm::raw_ostream *OS) | 
| Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 32 | : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), | 
| Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 33 | StatCalls(0), Stream(Buffer), Writer(Stream), Chaining(Chaining) { | 
| Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | // Install a stat() listener to keep track of all of the stat() | 
|  | 36 | // calls. | 
|  | 37 | StatCalls = new MemorizeStatCalls; | 
| Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 38 | // If we have a chain, we want new stat calls only, so install the memorizer | 
| Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 39 | // *after* the already installed ASTReader's stat cache. | 
| Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 40 | PP.getFileManager().addStatCache(StatCalls, | 
| Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 41 | /*AtBeginning=*/!Chaining); | 
| Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 44 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { | 
| Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 45 | if (PP.getDiagnostics().hasErrorOccurred()) | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 46 | return; | 
|  | 47 |  | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 48 | // Emit the PCH file | 
| Douglas Gregor | 162dd02 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 49 | assert(SemaPtr && "No Sema?"); | 
| Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 50 | Writer.WriteAST(*SemaPtr, StatCalls, isysroot); | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 51 |  | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 52 | // Write the generated bitstream to "Out". | 
| Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 53 | Out->write((char *)&Buffer.front(), Buffer.size()); | 
| Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 54 |  | 
|  | 55 | // Make sure it hits disk now. | 
| Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 56 | Out->flush(); | 
| Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 57 |  | 
|  | 58 | // Free up some memory, in case the process is kept alive. | 
|  | 59 | Buffer.clear(); | 
| 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() { | 
|  | 63 | if (Chaining) | 
|  | 64 | return &Writer; | 
|  | 65 | return 0; | 
|  | 66 | } | 
|  | 67 |  | 
| Sebastian Redl | 3e31c72 | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 68 | ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { | 
| Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 69 | return &Writer; | 
|  | 70 | } |