Skip to content

Commit

Permalink
[NFC] [C++20] [Modules] Use new class CXX20ModulesGenerator to genera…
Browse files Browse the repository at this point in the history
…te module file for C++20 modules instead of PCHGenerator

Previously we're re-using PCHGenerator to generate the module file for
C++20 modules. But this is slighty more or less odd. This patch tries
to use a new class 'CXX20ModulesGenerator' to generate the module file
for C++20 modules.
  • Loading branch information
ChuanqiXu9 committed Apr 30, 2024
1 parent bd72f7b commit 18268ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
19 changes: 16 additions & 3 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,17 +928,30 @@ class PCHGenerator : public SemaConsumer {
bool hasEmittedPCH() const { return Buffer->IsComplete; }
};

class ReducedBMIGenerator : public PCHGenerator {
class CXX20ModulesGenerator : public PCHGenerator {
protected:
virtual Module *getEmittingModule(ASTContext &Ctx) override;

CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, bool GeneratingReducedBMI);

public:
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile);
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile)
: CXX20ModulesGenerator(PP, ModuleCache, OutputFile,
/*GeneratingReducedBMI=*/false) {}

void HandleTranslationUnit(ASTContext &Ctx) override;
};

class ReducedBMIGenerator : public CXX20ModulesGenerator {
public:
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile)
: CXX20ModulesGenerator(PP, ModuleCache, OutputFile,
/*GeneratingReducedBMI=*/true) {}
};

/// If we can elide the definition of \param D in reduced BMI.
///
/// Generally, we can elide the definition of a declaration if it won't affect
Expand Down
12 changes: 4 additions & 8 deletions clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,10 @@ bool GenerateModuleInterfaceAction::BeginSourceFileAction(
std::unique_ptr<ASTConsumer>
GenerateModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
CI.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
CI.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
CI.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings = true;

std::vector<std::unique_ptr<ASTConsumer>> Consumers =
CreateMultiplexConsumer(CI, InFile);
if (Consumers.empty())
return nullptr;
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Consumers.push_back(std::make_unique<CXX20ModulesGenerator>(
CI.getPreprocessor(), CI.getModuleCache(),
CI.getFrontendOpts().OutputFile));

if (CI.getFrontendOpts().GenReducedBMI &&
!CI.getFrontendOpts().ModuleOutputPath.empty()) {
Expand Down
19 changes: 8 additions & 11 deletions clang/lib/Serialization/GeneratePCH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,28 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
return &Writer;
}

ReducedBMIGenerator::ReducedBMIGenerator(Preprocessor &PP,
InMemoryModuleCache &ModuleCache,
StringRef OutputFile)
CXX20ModulesGenerator::CXX20ModulesGenerator(Preprocessor &PP,
InMemoryModuleCache &ModuleCache,
StringRef OutputFile,
bool GeneratingReducedBMI)
: PCHGenerator(
PP, ModuleCache, OutputFile, llvm::StringRef(),
std::make_shared<PCHBuffer>(),
/*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
/*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
/*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
/*GeneratingReducedBMI=*/true) {}
GeneratingReducedBMI) {}

Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
Module *CXX20ModulesGenerator::getEmittingModule(ASTContext &Ctx) {
Module *M = Ctx.getCurrentNamedModule();
assert(M && M->isNamedModuleUnit() &&
"ReducedBMIGenerator should only be used with C++20 Named modules.");
"CXX20ModulesGenerator should only be used with C++20 Named modules.");
return M;
}

void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
// We need to do this to make sure the size of reduced BMI not to be larger
// than full BMI.
//
void CXX20ModulesGenerator::HandleTranslationUnit(ASTContext &Ctx) {
// FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
// since this is not about searching header really.
// FIXME2: We'd better to move the class writing full BMI with reduced BMI.
HeaderSearchOptions &HSOpts =
getPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
HSOpts.ModulesSkipDiagnosticOptions = true;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Modules/pr67893.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm \
// RUN: -emit-module-interface -o %t/a.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.cppm \
// RUN: -emit-module-interface -fprebuilt-module-path=%t
// RUN: -emit-module-interface -fprebuilt-module-path=%t -o %t/m.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.pcm \
// RUN: -fprebuilt-module-path=%t -S -emit-llvm -o - | FileCheck %t/m.cppm

Expand Down
8 changes: 3 additions & 5 deletions clang/test/Modules/search-partitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/partition3.cpp \
// RUN: -o %t/A-Part3.pcm

// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/moduleA.cpp \
// RUN: %clang_cc1 -std=c++20 %t/moduleA.cpp -fsyntax-only -verify \
// RUN: -fprebuilt-module-path=%t

// Test again with reduced BMI
Expand All @@ -28,9 +28,7 @@
// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/partition3.cpp \
// RUN: -o %t/A-Part3.pcm

// RUN: %clang_cc1 -std=c++20 -fsyntax-only %t/moduleA.cpp -fprebuilt-module-path=%t

// expected-no-diagnostics
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %t/moduleA.cpp -fprebuilt-module-path=%t

//--- partition1.cpp
export module A:Part1;
Expand All @@ -50,7 +48,7 @@ export module A:Part3;
int part3();

//--- moduleA.cpp

// expected-no-diagnostics
export module A;

import :Part1;
Expand Down

0 comments on commit 18268ac

Please sign in to comment.