clang 18.0.0git
Go to the documentation of this file.
1//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9// This file defines the PCHGenerator, which as a SemaConsumer that generates
10// a PCH file.
19#include "llvm/Bitstream/BitstreamWriter.h"
21using namespace clang;
24 const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
25 StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
26 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
27 bool AllowASTWithErrors, bool IncludeTimestamps,
28 bool BuildingImplicitModule, bool ShouldCacheASTInMemory)
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, ModuleCache, Extensions,
32 IncludeTimestamps, BuildingImplicitModule),
33 AllowASTWithErrors(AllowASTWithErrors),
34 ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
35 this->Buffer->IsComplete = false;
42 // Don't create a PCH if there were fatal failures during module loading.
44 return;
46 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
47 if (hasErrors && !AllowASTWithErrors)
48 return;
50 Module *Module = nullptr;
51 if (PP.getLangOpts().isCompilingModule()) {
54 /*AllowSearch*/ false);
55 if (!Module) {
56 assert(hasErrors && "emitting module but current module doesn't exist");
57 return;
58 }
59 }
61 // Errors that do not prevent the PCH from being written should not cause the
62 // overall compilation to fail either.
63 if (AllowASTWithErrors)
66 // Emit the PCH file to the Buffer.
67 assert(SemaPtr && "No Sema?");
68 Buffer->Signature = Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
69 ShouldCacheASTInMemory);
71 Buffer->IsComplete = true;
75 return &Writer;
79 return &Writer;
Defines the clang::ASTContext interface.
Defines the clang::Preprocessor interface.
const char * Data
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile, Module *WritingModule, StringRef isysroot, bool ShouldCacheASTInMemory=false)
Write a precompiled header for the given semantic analysis.
Definition: ASTWriter.cpp:4619
bool hasErrorOccurred() const
Definition: Diagnostic.h:843
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:572
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
In-memory cache for modules.
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:534
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:448
Describes a module or submodule.
Definition: Module.h:105
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
Definition: GeneratePCH.cpp:74
~PCHGenerator() override
Definition: GeneratePCH.cpp:38
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:78
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:41
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, ArrayRef< std::shared_ptr< ModuleFileExtension > > Extensions, bool AllowASTWithErrors=false, bool IncludeTimestamps=true, bool BuildingImplicitModule=false, bool ShouldCacheASTInMemory=false)
Definition: GeneratePCH.cpp:23
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
HeaderSearch & getHeaderSearchInfo() const
const LangOptions & getLangOpts() const
DiagnosticsEngine & getDiagnostics() const
Encodes a location in the source.
Definition: Format.h:5226