Douglas Gregor | 2cf2634 | 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 |
| 11 | // ASTConsume that generates a PCH file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/ASTConsumers.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/PCHWriter.h" |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 23 | #include "llvm/System/Path.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include "llvm/Support/Streams.h" |
| 27 | #include <string> |
| 28 | |
| 29 | using namespace clang; |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 33 | class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer { |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 34 | const Preprocessor &PP; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 35 | llvm::raw_ostream *Out; |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 36 | Sema *SemaPtr; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 37 | MemorizeStatCalls *StatCalls; // owned by the FileManager |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 40 | explicit PCHGenerator(const Preprocessor &PP, llvm::raw_ostream *Out); |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 41 | virtual void InitializeSema(Sema &S) { SemaPtr = &S; } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 42 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 43 | }; |
| 44 | } |
| 45 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 46 | PCHGenerator::PCHGenerator(const Preprocessor &PP, llvm::raw_ostream *OS) |
| 47 | : PP(PP), Out(OS), SemaPtr(0), StatCalls(0) { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 48 | |
| 49 | // Install a stat() listener to keep track of all of the stat() |
| 50 | // calls. |
| 51 | StatCalls = new MemorizeStatCalls; |
| 52 | PP.getFileManager().setStatCache(StatCalls); |
| 53 | } |
| 54 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 55 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 56 | if (PP.getDiagnostics().hasErrorOccurred()) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 57 | return; |
| 58 | |
| 59 | // Write the PCH contents into a buffer |
| 60 | std::vector<unsigned char> Buffer; |
| 61 | BitstreamWriter Stream(Buffer); |
| 62 | PCHWriter Writer(Stream); |
| 63 | |
| 64 | // Emit the PCH file |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 65 | assert(SemaPtr && "No Sema?"); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 66 | Writer.WritePCH(*SemaPtr, StatCalls); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 68 | // Write the generated bitstream to "Out". |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 69 | Out->write((char *)&Buffer.front(), Buffer.size()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 70 | |
| 71 | // Make sure it hits disk now. |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 72 | Out->flush(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 75 | ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP, |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 76 | llvm::raw_ostream *OS) { |
| 77 | return new PCHGenerator(PP, OS); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 78 | } |