blob: b5031fdf92a498e613af269b36da6c172098ef59 [file] [log] [blame]
Chandler Carruth8bd25b42011-12-09 01:45:42 +00001//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
Douglas Gregor2cf26342009-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 Carruth8bd25b42011-12-09 01:45:42 +000010// This file defines the PCHGenerator, which as a SemaConsumer that generates
11// a PCH file.
Douglas Gregor2cf26342009-04-09 22:27:44 +000012//
13//===----------------------------------------------------------------------===//
14
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000015#include "clang/Serialization/ASTWriter.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTConsumer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000018#include "clang/Basic/FileManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Sema/SemaConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include <string>
24
25using namespace clang;
Douglas Gregor2cf26342009-04-09 22:27:44 +000026
Mike Stump1eb44332009-09-09 15:08:12 +000027PCHGenerator::PCHGenerator(const Preprocessor &PP,
Douglas Gregor467dc882011-08-25 22:30:56 +000028 StringRef OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +000029 clang::Module *Module,
Douglas Gregor832d6202011-07-22 16:35:34 +000030 StringRef isysroot,
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +000031 raw_ostream *OS, bool AllowASTWithErrors)
Douglas Gregora8cc6ce2011-11-30 04:39:39 +000032 : PP(PP), OutputFile(OutputFile), Module(Module),
Douglas Gregor7143aab2011-09-01 17:04:32 +000033 isysroot(isysroot.str()), Out(OS),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070034 SemaPtr(nullptr), Stream(Buffer), Writer(Stream),
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +000035 AllowASTWithErrors(AllowASTWithErrors),
36 HasEmittedPCH(false) {
Douglas Gregor4947e252011-07-22 06:03:18 +000037}
38
39PCHGenerator::~PCHGenerator() {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000040}
41
Douglas Gregor2cf26342009-04-09 22:27:44 +000042void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +000043 // Don't create a PCH if there were fatal failures during module loading.
44 if (PP.getModuleLoader().HadFatalFailure)
45 return;
46
47 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
48 if (hasErrors && !AllowASTWithErrors)
Douglas Gregor2cf26342009-04-09 22:27:44 +000049 return;
Douglas Gregor89d99802010-11-30 06:16:57 +000050
Douglas Gregor2cf26342009-04-09 22:27:44 +000051 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000052 assert(SemaPtr && "No Sema?");
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +000053 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Douglas Gregor2cf26342009-04-09 22:27:44 +000055 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000056 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000059 Out->flush();
Sebastian Redl30c514c2010-07-14 23:45:08 +000060
61 // Free up some memory, in case the process is kept alive.
62 Buffer.clear();
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +000063
64 HasEmittedPCH = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +000065}
66
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000067ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor9293ba82011-08-25 22:35:51 +000068 return &Writer;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000069}
70
Sebastian Redl571db7f2010-08-18 23:56:56 +000071ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redlffaab3e2010-07-30 00:29:29 +000072 return &Writer;
73}