blob: 2e0076521f9c0154f04cf4feaf5770ad12547508 [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
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
Richard Smithbd97f352016-08-25 18:26:30 +000016#include "clang/Lex/HeaderSearch.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Sema/SemaConsumer.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000019#include "clang/Serialization/ASTWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021
22using namespace clang;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023
Douglas Gregor6623e1f2015-11-03 18:33:07 +000024PCHGenerator::PCHGenerator(
Richard Smithbd97f352016-08-25 18:26:30 +000025 const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
26 std::shared_ptr<PCHBuffer> Buffer,
David Blaikie61137e12017-01-05 18:23:18 +000027 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
Richard Smithbd97f352016-08-25 18:26:30 +000028 bool AllowASTWithErrors, bool IncludeTimestamps)
29 : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000030 SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
31 Writer(Stream, this->Buffer->Data, PP.getPCMCache(), Extensions,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000032 IncludeTimestamps),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000033 AllowASTWithErrors(AllowASTWithErrors) {
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000034 this->Buffer->IsComplete = false;
Douglas Gregor77d993d2011-07-22 06:03:18 +000035}
36
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000037PCHGenerator::~PCHGenerator() {
38}
Douglas Gregorc5046832009-04-27 18:38:38 +000039
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +000041 // Don't create a PCH if there were fatal failures during module loading.
42 if (PP.getModuleLoader().HadFatalFailure)
43 return;
44
45 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
46 if (hasErrors && !AllowASTWithErrors)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 return;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000048
Richard Smithbd97f352016-08-25 18:26:30 +000049 Module *Module = nullptr;
Richard Smithbbcc9f02016-08-26 00:14:38 +000050 if (PP.getLangOpts().isCompilingModule()) {
Richard Smithbd97f352016-08-25 18:26:30 +000051 Module = PP.getHeaderSearchInfo().lookupModule(
52 PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
Richard Smithbbcc9f02016-08-26 00:14:38 +000053 if (!Module) {
54 assert(hasErrors && "emitting module but current module doesn't exist");
55 return;
56 }
Richard Smithbd97f352016-08-25 18:26:30 +000057 }
58
Adrian Prantlbb165fb2015-06-20 18:53:08 +000059 // Emit the PCH file to the Buffer.
Douglas Gregor162dd022009-04-20 15:53:59 +000060 assert(SemaPtr && "No Sema?");
Adrian Prantla5206ce2015-09-22 23:26:43 +000061 Buffer->Signature =
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +000062 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
63 // For serialization we are lenient if the errors were
64 // only warn-as-error kind.
65 PP.getDiagnostics().hasUncompilableErrorOccurred());
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066
Adrian Prantlbb165fb2015-06-20 18:53:08 +000067 Buffer->IsComplete = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068}
69
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000070ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000071 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000072}
73
Sebastian Redl3e31c722010-08-18 23:56:56 +000074ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000075 return &Writer;
76}