blob: 4f6f5cae426853d24263d6ba85b769c3978682e7 [file] [log] [blame]
Douglas Gregoref84c4b2009-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
Sebastian Redld44cd6a2010-08-18 23:57:06 +000011// ASTConsumer that generates a PCH file.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000012//
13//===----------------------------------------------------------------------===//
14
Eli Friedman9f30fc32009-05-18 22:50:54 +000015#include "clang/Frontend/ASTConsumers.h"
Sebastian Redl1914c6f2010-08-18 23:56:37 +000016#include "clang/Serialization/ASTWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTConsumer.h"
Chris Lattnereeffaef2009-04-10 17:15:23 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregorc5046832009-04-27 18:38:38 +000021#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000022#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include <string>
26
27using namespace clang;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000028
Mike Stump11289f42009-09-09 15:08:12 +000029PCHGenerator::PCHGenerator(const Preprocessor &PP,
Sebastian Redl07a89a82010-07-30 00:29:29 +000030 bool Chaining,
Douglas Gregor0086a5a2009-07-07 00:12:59 +000031 const char *isysroot,
32 llvm::raw_ostream *OS)
Sebastian Redl07a89a82010-07-30 00:29:29 +000033 : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0),
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000034 StatCalls(0), Stream(Buffer), Writer(Stream), Chaining(Chaining) {
Douglas Gregorc5046832009-04-27 18:38:38 +000035
36 // Install a stat() listener to keep track of all of the stat()
37 // calls.
Chris Lattner226efd32010-11-23 19:19:34 +000038 StatCalls = new MemorizeStatCalls();
Sebastian Redl1ea025b2010-07-16 16:36:56 +000039 // If we have a chain, we want new stat calls only, so install the memorizer
Sebastian Redl2c499f62010-08-18 23:56:43 +000040 // *after* the already installed ASTReader's stat cache.
Sebastian Redl1ea025b2010-07-16 16:36:56 +000041 PP.getFileManager().addStatCache(StatCalls,
Sebastian Redl07a89a82010-07-30 00:29:29 +000042 /*AtBeginning=*/!Chaining);
Douglas Gregorc5046832009-04-27 18:38:38 +000043}
44
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattnereeffaef2009-04-10 17:15:23 +000046 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 return;
48
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049 // Emit the PCH file
Douglas Gregor162dd022009-04-20 15:53:59 +000050 assert(SemaPtr && "No Sema?");
Sebastian Redl55c0ad52010-08-18 23:56:21 +000051 Writer.WriteAST(*SemaPtr, StatCalls, isysroot);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053 // Write the generated bitstream to "Out".
Eli Friedman94cf21e2009-05-18 22:20:00 +000054 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055
56 // Make sure it hits disk now.
Eli Friedman94cf21e2009-05-18 22:20:00 +000057 Out->flush();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +000058
59 // Free up some memory, in case the process is kept alive.
60 Buffer.clear();
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061}
62
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000063ASTMutationListener *PCHGenerator::GetASTMutationListener() {
64 if (Chaining)
65 return &Writer;
66 return 0;
67}
68
Sebastian Redl3e31c722010-08-18 23:56:56 +000069ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000070 return &Writer;
71}