clang  14.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 
14 #include "clang/AST/ASTContext.h"
15 #include "clang/Lex/HeaderSearch.h"
16 #include "clang/Lex/Preprocessor.h"
19 #include "llvm/Bitstream/BitstreamWriter.h"
20 
21 using namespace clang;
22 
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 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),
33  AllowASTWithErrors(AllowASTWithErrors),
34  ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
35  this->Buffer->IsComplete = false;
36 }
37 
39 }
40 
42  // Don't create a PCH if there were fatal failures during module loading.
44  return;
45 
46  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
47  if (hasErrors && !AllowASTWithErrors)
48  return;
49 
50  Module *Module = nullptr;
51  if (PP.getLangOpts().isCompilingModule()) {
53  PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
54  if (!Module) {
55  assert(hasErrors && "emitting module but current module doesn't exist");
56  return;
57  }
58  }
59 
60  // Errors that do not prevent the PCH from being written should not cause the
61  // overall compilation to fail either.
62  if (AllowASTWithErrors)
63  PP.getDiagnostics().getClient()->clear();
64 
65  // Emit the PCH file to the Buffer.
66  assert(SemaPtr && "No Sema?");
67  Buffer->Signature =
68  Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
69  // For serialization we are lenient if the errors were
70  // only warn-as-error kind.
72  ShouldCacheASTInMemory);
73 
74  Buffer->IsComplete = true;
75 }
76 
78  return &Writer;
79 }
80 
82  return &Writer;
83 }
clang::LangOptions::CurrentModule
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:343
clang::HeaderSearch::lookupModule
Module * lookupModule(StringRef ModuleName, bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
Definition: HeaderSearch.cpp:232
clang::LangOptions::isCompilingModule
bool isCompilingModule() const
Are we compiling a module interface (.cppm or module map)?
Definition: LangOptions.h:392
clang::ModuleLoader::HadFatalFailure
bool HadFatalFailure
Definition: ModuleLoader.h:157
clang::DiagnosticsEngine::hasUncompilableErrorOccurred
bool hasUncompilableErrorOccurred() const
Errors that actually prevent compilation, not those that are upgraded from a warning by -Werror.
Definition: Diagnostic.h:837
ASTWriter.h
clang::Preprocessor::getLangOpts
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:937
Preprocessor.h
clang::Module
Describes a module or submodule.
Definition: Module.h:96
clang::PCHGenerator::PCHGenerator
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 ShouldCacheASTInMemory=false)
Definition: GeneratePCH.cpp:23
clang::DiagnosticsEngine::hasErrorOccurred
bool hasErrorOccurred() const
Definition: Diagnostic.h:833
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:211
clang::ASTMutationListener
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
Definition: ASTMutationListener.h:46
clang::PCHGenerator::~PCHGenerator
~PCHGenerator() override
Definition: GeneratePCH.cpp:38
clang::DiagnosticsEngine::getClient
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:565
ASTContext.h
clang::ASTDeserializationListener
Definition: ASTDeserializationListener.h:30
llvm::ArrayRef
Definition: LLVM.h:34
HeaderSearch.h
clang::PCHGenerator::GetASTMutationListener
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
Definition: GeneratePCH.cpp:77
clang::PCHGenerator::HandleTranslationUnit
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:41
clang::PCHGenerator::GetASTDeserializationListener
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:81
clang::ASTWriter::WriteAST
ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, Module *WritingModule, StringRef isysroot, bool hasErrors=false, bool ShouldCacheASTInMemory=false)
Write a precompiled header for the given semantic analysis.
Definition: ASTWriter.cpp:4283
std
Definition: Format.h:3899
clang::Preprocessor::getModuleLoader
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
Definition: Preprocessor.h:959
clang
Definition: CalledOnceCheck.h:17
clang::Preprocessor::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:934
clang::DiagnosticConsumer::clear
virtual void clear()
Definition: Diagnostic.h:1718
clang::InMemoryModuleCache
In-memory cache for modules.
Definition: InMemoryModuleCache.h:30
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::Preprocessor::getHeaderSearchInfo
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:942
SemaConsumer.h