[NFC] [C++20] [Modules] Refactor ReducedBMIGenerator

Changes:
- Don't lookup the emitting module from HeaderSearch. We will use the
  module from the ASTContext directly.
- Remove some useless arguments. Let's addback in the future if
  required.
This commit is contained in:
Chuanqi Xu 2024-03-13 11:20:38 +08:00
parent a62222f5f0
commit 5d7796e674
3 changed files with 36 additions and 22 deletions

View File

@ -868,6 +868,8 @@ protected:
return SemaPtr->getDiagnostics(); return SemaPtr->getDiagnostics();
} }
virtual Module *getEmittingModule(ASTContext &Ctx);
public: public:
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache, PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, StringRef isysroot, StringRef OutputFile, StringRef isysroot,
@ -887,10 +889,12 @@ public:
}; };
class ReducedBMIGenerator : public PCHGenerator { class ReducedBMIGenerator : public PCHGenerator {
protected:
virtual Module *getEmittingModule(ASTContext &Ctx) override;
public: public:
ReducedBMIGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache, ReducedBMIGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, std::shared_ptr<PCHBuffer> Buffer, StringRef OutputFile);
bool IncludeTimestamps);
void HandleTranslationUnit(ASTContext &Ctx) override; void HandleTranslationUnit(ASTContext &Ctx) override;
}; };

View File

@ -293,11 +293,9 @@ GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
std::unique_ptr<ASTConsumer> std::unique_ptr<ASTConsumer>
GenerateReducedModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI, GenerateReducedModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) { StringRef InFile) {
auto Buffer = std::make_shared<PCHBuffer>(); return std::make_unique<ReducedBMIGenerator>(CI.getPreprocessor(),
return std::make_unique<ReducedBMIGenerator>( CI.getModuleCache(),
CI.getPreprocessor(), CI.getModuleCache(), CI.getFrontendOpts().OutputFile);
CI.getFrontendOpts().OutputFile, Buffer,
/*IncludeTimestamps=*/+CI.getFrontendOpts().IncludeTimestamps);
} }
bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) { bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) {

View File

@ -41,6 +41,21 @@ PCHGenerator::PCHGenerator(
PCHGenerator::~PCHGenerator() { PCHGenerator::~PCHGenerator() {
} }
Module *PCHGenerator::getEmittingModule(ASTContext &) {
Module *M = nullptr;
if (PP.getLangOpts().isCompilingModule()) {
M = PP.getHeaderSearchInfo().lookupModule(PP.getLangOpts().CurrentModule,
SourceLocation(),
/*AllowSearch*/ false);
if (!M)
assert(PP.getDiagnostics().hasErrorOccurred() &&
"emitting module but current module doesn't exist");
}
return M;
}
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
// Don't create a PCH if there were fatal failures during module loading. // Don't create a PCH if there were fatal failures during module loading.
if (PP.getModuleLoader().HadFatalFailure) if (PP.getModuleLoader().HadFatalFailure)
@ -50,16 +65,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
if (hasErrors && !AllowASTWithErrors) if (hasErrors && !AllowASTWithErrors)
return; return;
Module *Module = nullptr; Module *Module = getEmittingModule(Ctx);
if (PP.getLangOpts().isCompilingModule()) {
Module = PP.getHeaderSearchInfo().lookupModule(
PP.getLangOpts().CurrentModule, SourceLocation(),
/*AllowSearch*/ false);
if (!Module) {
assert(hasErrors && "emitting module but current module doesn't exist");
return;
}
}
// Errors that do not prevent the PCH from being written should not cause the // Errors that do not prevent the PCH from being written should not cause the
// overall compilation to fail either. // overall compilation to fail either.
@ -84,16 +90,22 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
ReducedBMIGenerator::ReducedBMIGenerator(const Preprocessor &PP, ReducedBMIGenerator::ReducedBMIGenerator(const Preprocessor &PP,
InMemoryModuleCache &ModuleCache, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, StringRef OutputFile)
std::shared_ptr<PCHBuffer> Buffer,
bool IncludeTimestamps)
: PCHGenerator( : PCHGenerator(
PP, ModuleCache, OutputFile, llvm::StringRef(), Buffer, PP, ModuleCache, OutputFile, llvm::StringRef(),
std::make_shared<PCHBuffer>(),
/*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(), /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
/*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/IncludeTimestamps, /*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
/*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false, /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
/*GeneratingReducedBMI=*/true) {} /*GeneratingReducedBMI=*/true) {}
Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
Module *M = Ctx.getCurrentNamedModule();
assert(M->isNamedModuleUnit() &&
"ReducedBMIGenerator should only be used with C++20 Named modules.");
return M;
}
void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) { void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
PCHGenerator::HandleTranslationUnit(Ctx); PCHGenerator::HandleTranslationUnit(Ctx);