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; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 35 | const char *isysroot; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 36 | llvm::raw_ostream *Out; |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 37 | Sema *SemaPtr; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 38 | MemorizeStatCalls *StatCalls; // owned by the FileManager |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 39 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 40 | public: |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 41 | explicit PCHGenerator(const Preprocessor &PP, |
| 42 | const char *isysroot, |
| 43 | llvm::raw_ostream *Out); |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 44 | virtual void InitializeSema(Sema &S) { SemaPtr = &S; } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 45 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 46 | }; |
| 47 | } |
| 48 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 49 | PCHGenerator::PCHGenerator(const Preprocessor &PP, |
| 50 | const char *isysroot, |
| 51 | llvm::raw_ostream *OS) |
| 52 | : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0) { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 53 | |
| 54 | // Install a stat() listener to keep track of all of the stat() |
| 55 | // calls. |
| 56 | StatCalls = new MemorizeStatCalls; |
| 57 | PP.getFileManager().setStatCache(StatCalls); |
| 58 | } |
| 59 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 60 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 61 | if (PP.getDiagnostics().hasErrorOccurred()) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 62 | return; |
| 63 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 64 | // Write the PCH contents into a buffer |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 65 | std::vector<unsigned char> Buffer; |
| 66 | BitstreamWriter Stream(Buffer); |
| 67 | PCHWriter Writer(Stream); |
| 68 | |
| 69 | // Emit the PCH file |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 70 | assert(SemaPtr && "No Sema?"); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 71 | Writer.WritePCH(*SemaPtr, StatCalls, isysroot); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 73 | // Write the generated bitstream to "Out". |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 74 | Out->write((char *)&Buffer.front(), Buffer.size()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 75 | |
| 76 | // Make sure it hits disk now. |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 77 | Out->flush(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 80 | ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP, |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame^] | 81 | llvm::raw_ostream *OS, |
| 82 | const char *isysroot) { |
| 83 | return new PCHGenerator(PP, isysroot, OS); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 84 | } |