clang  19.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"
16 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/Bitstream/BitstreamWriter.h"
22 
23 using namespace clang;
24 
26  Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile,
27  StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
28  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
29  bool AllowASTWithErrors, bool IncludeTimestamps,
30  bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
31  bool GeneratingReducedBMI)
32  : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33  SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
34  Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
35  IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
36  AllowASTWithErrors(AllowASTWithErrors),
37  ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
38  this->Buffer->IsComplete = false;
39 }
40 
42 }
43 
45  Module *M = nullptr;
46 
47  if (PP.getLangOpts().isCompilingModule()) {
50  /*AllowSearch*/ false);
51  if (!M)
52  assert(PP.getDiagnostics().hasErrorOccurred() &&
53  "emitting module but current module doesn't exist");
54  }
55 
56  return M;
57 }
58 
60  // Don't create a PCH if there were fatal failures during module loading.
62  return;
63 
64  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
65  if (hasErrors && !AllowASTWithErrors)
66  return;
67 
69 
70  // Errors that do not prevent the PCH from being written should not cause the
71  // overall compilation to fail either.
72  if (AllowASTWithErrors)
73  PP.getDiagnostics().getClient()->clear();
74 
75  // Emit the PCH file to the Buffer.
76  assert(SemaPtr && "No Sema?");
77  Buffer->Signature = Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
78  ShouldCacheASTInMemory);
79 
80  Buffer->IsComplete = true;
81 }
82 
84  return &Writer;
85 }
86 
88  return &Writer;
89 }
90 
91 void PCHGenerator::anchor() {}
92 
94  InMemoryModuleCache &ModuleCache,
95  StringRef OutputFile,
96  bool GeneratingReducedBMI)
97  : PCHGenerator(
98  PP, ModuleCache, OutputFile, llvm::StringRef(),
99  std::make_shared<PCHBuffer>(),
100  /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
101  /*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
102  /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
103  GeneratingReducedBMI) {}
104 
106  Module *M = Ctx.getCurrentNamedModule();
107  assert(M && M->isNamedModuleUnit() &&
108  "CXX20ModulesGenerator should only be used with C++20 Named modules.");
109  return M;
110 }
111 
113  // FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
114  // since this is not about searching header really.
115  HeaderSearchOptions &HSOpts =
117  HSOpts.ModulesSkipDiagnosticOptions = true;
118  HSOpts.ModulesSkipHeaderSearchPaths = true;
119 
121 
122  if (!isComplete())
123  return;
124 
125  std::error_code EC;
126  auto OS = std::make_unique<llvm::raw_fd_ostream>(getOutputFile(), EC);
127  if (EC) {
128  getDiagnostics().Report(diag::err_fe_unable_to_open_output)
129  << getOutputFile() << EC.message() << "\n";
130  return;
131  }
132 
133  *OS << getBufferPtr()->Data;
134  OS->flush();
135 }
136 
137 void CXX20ModulesGenerator::anchor() {}
138 
139 void ReducedBMIGenerator::anchor() {}
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:185
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
Definition: ASTContext.h:1074
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:4831
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile, bool GeneratingReducedBMI)
Definition: GeneratePCH.cpp:93
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
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1553
bool hasErrorOccurred() const
Definition: Diagnostic.h:849
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:578
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
unsigned ModulesSkipHeaderSearchPaths
Whether to entirely skip writing header search paths.
unsigned ModulesSkipDiagnosticOptions
Whether to entirely skip writing diagnostic options.
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized.
Definition: HeaderSearch.h:386
In-memory cache for modules.
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:643
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:537
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Describes a module or submodule.
Definition: Module.h:105
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:620
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
Definition: ASTWriter.h:906
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
Definition: GeneratePCH.cpp:83
Preprocessor & getPreprocessor()
Definition: ASTWriter.h:930
virtual Module * getEmittingModule(ASTContext &Ctx)
Definition: GeneratePCH.cpp:44
StringRef getOutputFile() const
Definition: ASTWriter.h:926
~PCHGenerator() override
Definition: GeneratePCH.cpp:41
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:87
PCHGenerator(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, bool GeneratingReducedBMI=false)
Definition: GeneratePCH.cpp:25
PCHBuffer * getBufferPtr()
Definition: ASTWriter.h:925
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:59
bool isComplete() const
Definition: ASTWriter.h:924
DiagnosticsEngine & getDiagnostics() const
Definition: ASTWriter.h:927
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
DiagnosticsEngine & getDiagnostics() const
const LangOptions & getLangOpts() const
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
Definition: Format.h:5433
#define false
Definition: stdbool.h:26
llvm::SmallVector< char, 0 > Data