blob: 8be88ce38117cbf1b7543d27d555ea832010653e [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
Eli Friedman22a4efd2009-05-18 22:50:54 +000015#include "clang/Frontend/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;
Eli Friedmana9c310842009-05-18 22:20:00 +000035 llvm::raw_ostream *Out;
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:
Eli Friedmana9c310842009-05-18 22:20:00 +000040 explicit PCHGenerator(const Preprocessor &PP, llvm::raw_ostream *Out);
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
Eli Friedmana9c310842009-05-18 22:20:00 +000046PCHGenerator::PCHGenerator(const Preprocessor &PP, llvm::raw_ostream *OS)
47 : PP(PP), Out(OS), SemaPtr(0), StatCalls(0) {
Douglas Gregor6cc5d192009-04-27 18:38:38 +000048
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
Douglas Gregorc34897d2009-04-09 22:27:44 +000068 // Write the generated bitstream to "Out".
Eli Friedmana9c310842009-05-18 22:20:00 +000069 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregorc34897d2009-04-09 22:27:44 +000070
71 // Make sure it hits disk now.
Eli Friedmana9c310842009-05-18 22:20:00 +000072 Out->flush();
Douglas Gregorc34897d2009-04-09 22:27:44 +000073}
74
Chris Lattner850eabd2009-04-10 18:08:30 +000075ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Eli Friedmana9c310842009-05-18 22:20:00 +000076 llvm::raw_ostream *OS) {
77 return new PCHGenerator(PP, OS);
Douglas Gregorc34897d2009-04-09 22:27:44 +000078}