blob: 0d8ec736b6ad184b32e30b9f11c5b82289060e95 [file] [log] [blame]
Douglas Gregor2cf26342009-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 Redl3c7f4132010-08-18 23:57:06 +000011// ASTConsumer that generates a PCH file.
Douglas Gregor2cf26342009-04-09 22:27:44 +000012//
13//===----------------------------------------------------------------------===//
14
Eli Friedman39d7c4d2009-05-18 22:50:54 +000015#include "clang/Frontend/ASTConsumers.h"
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000016#include "clang/Serialization/ASTWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTConsumer.h"
Chris Lattner0b1fb982009-04-10 17:15:23 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000021#include "clang/Basic/FileManager.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "llvm/Support/raw_ostream.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include <string>
25
26using namespace clang;
Douglas Gregor2cf26342009-04-09 22:27:44 +000027
Mike Stump1eb44332009-09-09 15:08:12 +000028PCHGenerator::PCHGenerator(const Preprocessor &PP,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000029 bool Chaining,
Douglas Gregore650c8c2009-07-07 00:12:59 +000030 const char *isysroot,
31 llvm::raw_ostream *OS)
Sebastian Redlffaab3e2010-07-30 00:29:29 +000032 : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0),
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000033 StatCalls(0), Stream(Buffer), Writer(Stream), Chaining(Chaining) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000034
35 // Install a stat() listener to keep track of all of the stat()
36 // calls.
37 StatCalls = new MemorizeStatCalls;
Sebastian Redl1476ed42010-07-16 16:36:56 +000038 // If we have a chain, we want new stat calls only, so install the memorizer
Sebastian Redlc43b54c2010-08-18 23:56:43 +000039 // *after* the already installed ASTReader's stat cache.
Sebastian Redl1476ed42010-07-16 16:36:56 +000040 PP.getFileManager().addStatCache(StatCalls,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000041 /*AtBeginning=*/!Chaining);
Douglas Gregor4fed3f42009-04-27 18:38:38 +000042}
43
Douglas Gregor2cf26342009-04-09 22:27:44 +000044void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000045 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000046 return;
47
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000049 assert(SemaPtr && "No Sema?");
Sebastian Redla4232eb2010-08-18 23:56:21 +000050 Writer.WriteAST(*SemaPtr, StatCalls, isysroot);
Douglas Gregor2cf26342009-04-09 22:27:44 +000051
Douglas Gregor2cf26342009-04-09 22:27:44 +000052 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000053 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
55 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000056 Out->flush();
Sebastian Redl30c514c2010-07-14 23:45:08 +000057
58 // Free up some memory, in case the process is kept alive.
59 Buffer.clear();
Douglas Gregor2cf26342009-04-09 22:27:44 +000060}
61
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000062ASTMutationListener *PCHGenerator::GetASTMutationListener() {
63 if (Chaining)
64 return &Writer;
65 return 0;
66}
67
Sebastian Redl571db7f2010-08-18 23:56:56 +000068ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redlffaab3e2010-07-30 00:29:29 +000069 return &Writer;
70}