Skip to content

Commit 18268ac

Browse files
committed
[NFC] [C++20] [Modules] Use new class CXX20ModulesGenerator to generate 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.
1 parent bd72f7b commit 18268ac

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,17 +928,30 @@ class PCHGenerator : public SemaConsumer {
928928
bool hasEmittedPCH() const { return Buffer->IsComplete; }
929929
};
930930

931-
class ReducedBMIGenerator : public PCHGenerator {
931+
class CXX20ModulesGenerator : public PCHGenerator {
932932
protected:
933933
virtual Module *getEmittingModule(ASTContext &Ctx) override;
934934

935+
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
936+
StringRef OutputFile, bool GeneratingReducedBMI);
937+
935938
public:
936-
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
937-
StringRef OutputFile);
939+
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
940+
StringRef OutputFile)
941+
: CXX20ModulesGenerator(PP, ModuleCache, OutputFile,
942+
/*GeneratingReducedBMI=*/false) {}
938943

939944
void HandleTranslationUnit(ASTContext &Ctx) override;
940945
};
941946

947+
class ReducedBMIGenerator : public CXX20ModulesGenerator {
948+
public:
949+
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
950+
StringRef OutputFile)
951+
: CXX20ModulesGenerator(PP, ModuleCache, OutputFile,
952+
/*GeneratingReducedBMI=*/true) {}
953+
};
954+
942955
/// If we can elide the definition of \param D in reduced BMI.
943956
///
944957
/// Generally, we can elide the definition of a declaration if it won't affect

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,10 @@ bool GenerateModuleInterfaceAction::BeginSourceFileAction(
272272
std::unique_ptr<ASTConsumer>
273273
GenerateModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,
274274
StringRef InFile) {
275-
CI.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
276-
CI.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
277-
CI.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings = true;
278-
279-
std::vector<std::unique_ptr<ASTConsumer>> Consumers =
280-
CreateMultiplexConsumer(CI, InFile);
281-
if (Consumers.empty())
282-
return nullptr;
275+
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
276+
Consumers.push_back(std::make_unique<CXX20ModulesGenerator>(
277+
CI.getPreprocessor(), CI.getModuleCache(),
278+
CI.getFrontendOpts().OutputFile));
283279

284280
if (CI.getFrontendOpts().GenReducedBMI &&
285281
!CI.getFrontendOpts().ModuleOutputPath.empty()) {

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,28 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
8888
return &Writer;
8989
}
9090

91-
ReducedBMIGenerator::ReducedBMIGenerator(Preprocessor &PP,
92-
InMemoryModuleCache &ModuleCache,
93-
StringRef OutputFile)
91+
CXX20ModulesGenerator::CXX20ModulesGenerator(Preprocessor &PP,
92+
InMemoryModuleCache &ModuleCache,
93+
StringRef OutputFile,
94+
bool GeneratingReducedBMI)
9495
: PCHGenerator(
9596
PP, ModuleCache, OutputFile, llvm::StringRef(),
9697
std::make_shared<PCHBuffer>(),
9798
/*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
9899
/*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
99100
/*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
100-
/*GeneratingReducedBMI=*/true) {}
101+
GeneratingReducedBMI) {}
101102

102-
Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
103+
Module *CXX20ModulesGenerator::getEmittingModule(ASTContext &Ctx) {
103104
Module *M = Ctx.getCurrentNamedModule();
104105
assert(M && M->isNamedModuleUnit() &&
105-
"ReducedBMIGenerator should only be used with C++20 Named modules.");
106+
"CXX20ModulesGenerator should only be used with C++20 Named modules.");
106107
return M;
107108
}
108109

109-
void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
110-
// We need to do this to make sure the size of reduced BMI not to be larger
111-
// than full BMI.
112-
//
110+
void CXX20ModulesGenerator::HandleTranslationUnit(ASTContext &Ctx) {
113111
// FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
114112
// since this is not about searching header really.
115-
// FIXME2: We'd better to move the class writing full BMI with reduced BMI.
116113
HeaderSearchOptions &HSOpts =
117114
getPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
118115
HSOpts.ModulesSkipDiagnosticOptions = true;

clang/test/Modules/pr67893.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm \
66
// RUN: -emit-module-interface -o %t/a.pcm
77
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.cppm \
8-
// RUN: -emit-module-interface -fprebuilt-module-path=%t
8+
// RUN: -emit-module-interface -fprebuilt-module-path=%t -o %t/m.pcm
99
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/m.pcm \
1010
// RUN: -fprebuilt-module-path=%t -S -emit-llvm -o - | FileCheck %t/m.cppm
1111

clang/test/Modules/search-partitions.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/partition3.cpp \
1212
// RUN: -o %t/A-Part3.pcm
1313

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

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

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

3533
//--- partition1.cpp
3634
export module A:Part1;
@@ -50,7 +48,7 @@ export module A:Part3;
5048
int part3();
5149

5250
//--- moduleA.cpp
53-
51+
// expected-no-diagnostics
5452
export module A;
5553

5654
import :Part1;

0 commit comments

Comments
 (0)