1f4a2713aSLionel Sambuc //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the PCHGenerator, which as a SemaConsumer that generates
11f4a2713aSLionel Sambuc // a PCH file.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/Serialization/ASTWriter.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
18f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
19f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
20f4a2713aSLionel Sambuc #include "clang/Sema/SemaConsumer.h"
21f4a2713aSLionel Sambuc #include "llvm/Bitcode/BitstreamWriter.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
23f4a2713aSLionel Sambuc #include <string>
24f4a2713aSLionel Sambuc
25f4a2713aSLionel Sambuc using namespace clang;
26f4a2713aSLionel Sambuc
PCHGenerator(const Preprocessor & PP,StringRef OutputFile,clang::Module * Module,StringRef isysroot,raw_ostream * OS,bool AllowASTWithErrors)27f4a2713aSLionel Sambuc PCHGenerator::PCHGenerator(const Preprocessor &PP,
28f4a2713aSLionel Sambuc StringRef OutputFile,
29f4a2713aSLionel Sambuc clang::Module *Module,
30f4a2713aSLionel Sambuc StringRef isysroot,
31f4a2713aSLionel Sambuc raw_ostream *OS, bool AllowASTWithErrors)
32f4a2713aSLionel Sambuc : PP(PP), OutputFile(OutputFile), Module(Module),
33f4a2713aSLionel Sambuc isysroot(isysroot.str()), Out(OS),
34*0a6a1f1dSLionel Sambuc SemaPtr(nullptr), Stream(Buffer), Writer(Stream),
35f4a2713aSLionel Sambuc AllowASTWithErrors(AllowASTWithErrors),
36f4a2713aSLionel Sambuc HasEmittedPCH(false) {
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc
~PCHGenerator()39f4a2713aSLionel Sambuc PCHGenerator::~PCHGenerator() {
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc
HandleTranslationUnit(ASTContext & Ctx)42f4a2713aSLionel Sambuc void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
43f4a2713aSLionel Sambuc // Don't create a PCH if there were fatal failures during module loading.
44f4a2713aSLionel Sambuc if (PP.getModuleLoader().HadFatalFailure)
45f4a2713aSLionel Sambuc return;
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
48f4a2713aSLionel Sambuc if (hasErrors && !AllowASTWithErrors)
49f4a2713aSLionel Sambuc return;
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc // Emit the PCH file
52f4a2713aSLionel Sambuc assert(SemaPtr && "No Sema?");
53f4a2713aSLionel Sambuc Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc // Write the generated bitstream to "Out".
56f4a2713aSLionel Sambuc Out->write((char *)&Buffer.front(), Buffer.size());
57f4a2713aSLionel Sambuc
58f4a2713aSLionel Sambuc // Make sure it hits disk now.
59f4a2713aSLionel Sambuc Out->flush();
60f4a2713aSLionel Sambuc
61f4a2713aSLionel Sambuc // Free up some memory, in case the process is kept alive.
62f4a2713aSLionel Sambuc Buffer.clear();
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc HasEmittedPCH = true;
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc
GetASTMutationListener()67f4a2713aSLionel Sambuc ASTMutationListener *PCHGenerator::GetASTMutationListener() {
68f4a2713aSLionel Sambuc return &Writer;
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc
GetASTDeserializationListener()71f4a2713aSLionel Sambuc ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
72f4a2713aSLionel Sambuc return &Writer;
73f4a2713aSLionel Sambuc }
74