blob: 870d65489584a29cab6cb9e02a8158f194ccc6cc [file] [log] [blame]
Chandler Carruthae899872011-12-09 01:45:42 +00001//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
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//
Chandler Carruthae899872011-12-09 01:45:42 +000010// This file defines the PCHGenerator, which as a SemaConsumer that generates
11// a PCH file.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000012//
13//===----------------------------------------------------------------------===//
14
Sebastian Redl1914c6f2010-08-18 23:56:37 +000015#include "clang/Serialization/ASTWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000016#include "clang/Sema/SemaConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/ASTConsumer.h"
Chris Lattnereeffaef2009-04-10 17:15:23 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregorc5046832009-04-27 18:38:38 +000020#include "clang/Basic/FileManager.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include <string>
24
25using namespace clang;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026
Mike Stump11289f42009-09-09 15:08:12 +000027PCHGenerator::PCHGenerator(const Preprocessor &PP,
Douglas Gregor69f74f82011-08-25 22:30:56 +000028 StringRef OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +000029 clang::Module *Module,
Douglas Gregorc567ba22011-07-22 16:35:34 +000030 StringRef isysroot,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031 raw_ostream *OS)
Douglas Gregorf7a700fd2011-11-30 04:39:39 +000032 : PP(PP), OutputFile(OutputFile), Module(Module),
Douglas Gregor4a69c2e2011-09-01 17:04:32 +000033 isysroot(isysroot.str()), Out(OS),
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +000034 SemaPtr(0), Stream(Buffer), Writer(Stream) {
Douglas Gregor77d993d2011-07-22 06:03:18 +000035}
36
37PCHGenerator::~PCHGenerator() {
Douglas Gregorc5046832009-04-27 18:38:38 +000038}
39
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattnereeffaef2009-04-10 17:15:23 +000041 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042 return;
Douglas Gregorf88e35b2010-11-30 06:16:57 +000043
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044 // Emit the PCH file
Douglas Gregor162dd022009-04-20 15:53:59 +000045 assert(SemaPtr && "No Sema?");
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +000046 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048 // Write the generated bitstream to "Out".
Eli Friedman94cf21e2009-05-18 22:20:00 +000049 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
51 // Make sure it hits disk now.
Eli Friedman94cf21e2009-05-18 22:20:00 +000052 Out->flush();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +000053
54 // Free up some memory, in case the process is kept alive.
55 Buffer.clear();
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056}
57
Douglas Gregorcb28f9d2012-10-09 23:05:51 +000058PPMutationListener *PCHGenerator::GetPPMutationListener() {
59 return &Writer;
60}
61
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000062ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000063 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000064}
65
Sebastian Redl3e31c722010-08-18 23:56:56 +000066ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000067 return &Writer;
68}