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