blob: 9bc5ded71571563aa092052ea8dce0a09b7dc29b [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- 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 Lattnerffc05ed2009-04-10 17:15:23 +000015#include "ASTConsumers.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTConsumer.h"
Chris Lattnerffc05ed2009-04-10 17:15:23 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor6cc5d192009-04-27 18:38:38 +000021#include "clang/Basic/FileManager.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#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
29using namespace clang;
30using namespace llvm;
31
32namespace {
Douglas Gregor87887da2009-04-20 15:53:59 +000033 class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer {
Chris Lattner850eabd2009-04-10 18:08:30 +000034 const Preprocessor &PP;
Douglas Gregorc34897d2009-04-09 22:27:44 +000035 std::string OutFile;
Douglas Gregor87887da2009-04-20 15:53:59 +000036 Sema *SemaPtr;
Douglas Gregor6cc5d192009-04-27 18:38:38 +000037 MemorizeStatCalls *StatCalls; // owned by the FileManager
Douglas Gregorc34897d2009-04-09 22:27:44 +000038
39 public:
Douglas Gregor6cc5d192009-04-27 18:38:38 +000040 explicit PCHGenerator(const Preprocessor &PP, const std::string &OutFile);
Douglas Gregor87887da2009-04-20 15:53:59 +000041 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregorc34897d2009-04-09 22:27:44 +000042 virtual void HandleTranslationUnit(ASTContext &Ctx);
43 };
44}
45
Douglas Gregor6cc5d192009-04-27 18:38:38 +000046PCHGenerator::PCHGenerator(const Preprocessor &PP, const std::string &OutFile)
47 : PP(PP), OutFile(OutFile), SemaPtr(0), StatCalls(0) {
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 Gregorc34897d2009-04-09 22:27:44 +000055void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattnerffc05ed2009-04-10 17:15:23 +000056 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregorc34897d2009-04-09 22:27:44 +000057 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 Gregor87887da2009-04-20 15:53:59 +000065 assert(SemaPtr && "No Sema?");
Douglas Gregor6cc5d192009-04-27 18:38:38 +000066 Writer.WritePCH(*SemaPtr, StatCalls);
Douglas Gregorc34897d2009-04-09 22:27:44 +000067
68 // Open up the PCH file.
69 std::string ErrMsg;
70 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
71
72 if (!ErrMsg.empty()) {
73 llvm::errs() << "PCH error: " << ErrMsg << "\n";
74 return;
75 }
76
77 // Write the generated bitstream to "Out".
78 Out.write((char *)&Buffer.front(), Buffer.size());
79
80 // Make sure it hits disk now.
81 Out.flush();
82}
83
Chris Lattner850eabd2009-04-10 18:08:30 +000084ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Chris Lattnerffc05ed2009-04-10 17:15:23 +000085 const std::string &OutFile) {
86 return new PCHGenerator(PP, OutFile);
Douglas Gregorc34897d2009-04-09 22:27:44 +000087}