]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Serialization/GeneratePCH.cpp
Vendor import of clang trunk r300422:
[FreeBSD/FreeBSD.git] / lib / Serialization / GeneratePCH.cpp
1 //===--- GeneratePCH.cpp - Sema 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 PCHGenerator, which as a SemaConsumer that generates
11 //  a PCH file.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/AST/ASTContext.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Sema/SemaConsumer.h"
19 #include "clang/Serialization/ASTWriter.h"
20 #include "llvm/Bitcode/BitstreamWriter.h"
21
22 using namespace clang;
23
24 PCHGenerator::PCHGenerator(
25     const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
26     std::shared_ptr<PCHBuffer> Buffer,
27     ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
28     bool AllowASTWithErrors, bool IncludeTimestamps)
29     : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
30       SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
31       Writer(Stream, this->Buffer->Data, PP.getPCMCache(), Extensions,
32              IncludeTimestamps),
33       AllowASTWithErrors(AllowASTWithErrors) {
34   this->Buffer->IsComplete = false;
35 }
36
37 PCHGenerator::~PCHGenerator() {
38 }
39
40 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
41   // 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)
47     return;
48
49   Module *Module = nullptr;
50   if (PP.getLangOpts().isCompilingModule()) {
51     Module = PP.getHeaderSearchInfo().lookupModule(
52         PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
53     if (!Module) {
54       assert(hasErrors && "emitting module but current module doesn't exist");
55       return;
56     }
57   }
58
59   // Emit the PCH file to the Buffer.
60   assert(SemaPtr && "No Sema?");
61   Buffer->Signature =
62       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());
66
67   Buffer->IsComplete = true;
68 }
69
70 ASTMutationListener *PCHGenerator::GetASTMutationListener() {
71   return &Writer;
72 }
73
74 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
75   return &Writer;
76 }