clang 23.0.0git
GeneratePCH.cpp
Go to the documentation of this file.
1//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the PCHGenerator, which as a SemaConsumer that generates
10// a PCH file.
11//
12//===----------------------------------------------------------------------===//
13
21#include "llvm/Bitstream/BitstreamWriter.h"
22
23using namespace clang;
24
26 Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile,
27 StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
28 const CodeGenOptions &CodeGenOpts,
29 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
30 bool AllowASTWithErrors, bool IncludeTimestamps,
31 bool BuildingImplicitModule, bool GeneratingReducedBMI)
32 : PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33 Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
34 Writer(Stream, this->Buffer->Data, ModCache, CodeGenOpts, Extensions,
35 IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
36 AllowASTWithErrors(AllowASTWithErrors) {
37 this->Buffer->IsComplete = false;
38}
39
42
44 Module *M = nullptr;
45
46 if (PP.getLangOpts().isCompilingModule()) {
47 M = PP.getHeaderSearchInfo().lookupModule(PP.getLangOpts().CurrentModule,
49 /*AllowSearch*/ false);
50 if (!M)
51 assert(PP.getDiagnostics().hasErrorOccurred() &&
52 "emitting module but current module doesn't exist");
53 }
54
55 return M;
56}
57
59 return PP.getDiagnostics();
60}
61
63 if (!PP.getHeaderSearchInfo()
64 .getHeaderSearchOpts()
65 .ModulesSerializeOnlyPreprocessor)
66 Subject = &S;
67}
68
70 // Don't create a PCH if there were fatal failures during module loading.
71 if (PP.getModuleLoader().HadFatalFailure)
72 return;
73
74 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
75 if (hasErrors && !AllowASTWithErrors)
76 return;
77
79
80 // Errors that do not prevent the PCH from being written should not cause the
81 // overall compilation to fail either.
82 if (AllowASTWithErrors)
83 PP.getDiagnostics().getClient()->clear();
84
85 Buffer->Signature = Writer.WriteAST(Subject, OutputFile, Module, isysroot);
86
87 Buffer->IsComplete = true;
88}
89
93
97
98void PCHGenerator::anchor() {}
99
101 ModuleCache &ModCache,
102 StringRef OutputFile,
103 const CodeGenOptions &CodeGenOpts,
104 bool GeneratingReducedBMI,
105 bool AllowASTWithErrors)
106 : PCHGenerator(
107 PP, ModCache, OutputFile, llvm::StringRef(),
108 std::make_shared<PCHBuffer>(), CodeGenOpts,
109 /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
110 AllowASTWithErrors, /*IncludeTimestamps=*/false,
111 /*BuildingImplicitModule=*/false, GeneratingReducedBMI) {}
112
114 Module *M = Ctx.getCurrentNamedModule();
115 assert(M && M->isNamedModuleUnit() &&
116 "CXX20ModulesGenerator should only be used with C++20 Named modules.");
117 return M;
118}
119
122
123 if (!isComplete())
124 return;
125
126 std::error_code EC;
127 auto OS = std::make_unique<llvm::raw_fd_ostream>(getOutputFile(), EC);
128 if (EC) {
129 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
130 << getOutputFile() << EC.message() << "\n";
131 return;
132 }
133
134 *OS << getBufferPtr()->Data;
135 OS->flush();
136}
137
138void CXX20ModulesGenerator::anchor() {}
139
140void ReducedBMIGenerator::anchor() {}
Defines the clang::ASTContext interface.
Defines the clang::Preprocessor interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
CXX20ModulesGenerator(Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, const CodeGenOptions &CodeGenOpts, bool GeneratingReducedBMI, bool AllowASTWithErrors)
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
virtual Module * getEmittingModule(ASTContext &Ctx) override
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
bool hasErrorOccurred() const
Definition Diagnostic.h:881
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:30
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Describes a module or submodule.
Definition Module.h:246
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition Module.h:782
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
PCHBuffer * getBufferPtr()
Definition ASTWriter.h:1020
virtual Module * getEmittingModule(ASTContext &Ctx)
StringRef getOutputFile() const
Definition ASTWriter.h:1021
~PCHGenerator() override
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
PCHGenerator(Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, const CodeGenOptions &CodeGenOpts, ArrayRef< std::shared_ptr< ModuleFileExtension > > Extensions, bool AllowASTWithErrors=false, bool IncludeTimestamps=true, bool BuildingImplicitModule=false, bool GeneratingReducedBMI=false)
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
bool isComplete() const
Definition ASTWriter.h:1019
DiagnosticsEngine & getDiagnostics() const
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
DiagnosticsEngine & getDiagnostics() const
Definition Sema.h:936
Encodes a location in the source.
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
#define false
Definition stdbool.h:26
llvm::SmallVector< char, 0 > Data