blob: a2534db82cdd08b17a3ceb494047f6caa09f72cf [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"
Chris Lattner10e286a2010-11-23 19:19:34 +000022#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include <string>
26
27using namespace clang;
Douglas Gregor2cf26342009-04-09 22:27:44 +000028
Mike Stump1eb44332009-09-09 15:08:12 +000029PCHGenerator::PCHGenerator(const Preprocessor &PP,
Douglas Gregor467dc882011-08-25 22:30:56 +000030 StringRef OutputFile,
Douglas Gregor7143aab2011-09-01 17:04:32 +000031 bool IsModule,
Douglas Gregor832d6202011-07-22 16:35:34 +000032 StringRef isysroot,
Chris Lattner5f9e2722011-07-23 10:55:15 +000033 raw_ostream *OS)
Douglas Gregor7143aab2011-09-01 17:04:32 +000034 : PP(PP), OutputFile(OutputFile), IsModule(IsModule),
35 isysroot(isysroot.str()), Out(OS),
Douglas Gregor9293ba82011-08-25 22:35:51 +000036 SemaPtr(0), StatCalls(0), Stream(Buffer), Writer(Stream) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000037 // Install a stat() listener to keep track of all of the stat()
38 // calls.
Chris Lattner10e286a2010-11-23 19:19:34 +000039 StatCalls = new MemorizeStatCalls();
Douglas Gregor9293ba82011-08-25 22:35:51 +000040 PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false);
Douglas Gregor4947e252011-07-22 06:03:18 +000041}
42
43PCHGenerator::~PCHGenerator() {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000044}
45
Douglas Gregor2cf26342009-04-09 22:27:44 +000046void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000047 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 return;
Douglas Gregor89d99802010-11-30 06:16:57 +000049
Douglas Gregor2cf26342009-04-09 22:27:44 +000050 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000051 assert(SemaPtr && "No Sema?");
Douglas Gregor7143aab2011-09-01 17:04:32 +000052 Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, IsModule, isysroot);
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Douglas Gregor2cf26342009-04-09 22:27:44 +000054 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000055 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000056
57 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000058 Out->flush();
Sebastian Redl30c514c2010-07-14 23:45:08 +000059
60 // Free up some memory, in case the process is kept alive.
61 Buffer.clear();
Douglas Gregor2cf26342009-04-09 22:27:44 +000062}
63
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000064ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor9293ba82011-08-25 22:35:51 +000065 return &Writer;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000066}
67
Sebastian Redl571db7f2010-08-18 23:56:56 +000068ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redlffaab3e2010-07-30 00:29:29 +000069 return &Writer;
70}