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 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 15 | #include "ASTConsumers.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/PCHWriter.h" |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 21 | #include "llvm/System/Path.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/Support/Streams.h" |
| 25 | #include <string> |
| 26 | |
| 27 | using namespace clang; |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | class VISIBILITY_HIDDEN PCHGenerator : public ASTConsumer { |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame^] | 32 | const Preprocessor &PP; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 33 | std::string OutFile; |
| 34 | |
| 35 | public: |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame^] | 36 | explicit PCHGenerator(const Preprocessor &PP, const std::string &OutFile) |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 37 | : PP(PP), OutFile(OutFile) { } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 38 | |
| 39 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 44 | if (PP.getDiagnostics().hasErrorOccurred()) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 45 | return; |
| 46 | |
| 47 | // Write the PCH contents into a buffer |
| 48 | std::vector<unsigned char> Buffer; |
| 49 | BitstreamWriter Stream(Buffer); |
| 50 | PCHWriter Writer(Stream); |
| 51 | |
| 52 | // Emit the PCH file |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 53 | Writer.WritePCH(Ctx, PP); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 54 | |
| 55 | // Open up the PCH file. |
| 56 | std::string ErrMsg; |
| 57 | llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); |
| 58 | |
| 59 | if (!ErrMsg.empty()) { |
| 60 | llvm::errs() << "PCH error: " << ErrMsg << "\n"; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Write the generated bitstream to "Out". |
| 65 | Out.write((char *)&Buffer.front(), Buffer.size()); |
| 66 | |
| 67 | // Make sure it hits disk now. |
| 68 | Out.flush(); |
| 69 | } |
| 70 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame^] | 71 | ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP, |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 72 | const std::string &OutFile) { |
| 73 | return new PCHGenerator(PP, OutFile); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 74 | } |