Skip to content

Commit fce0916

Browse files
authored
[NFC] [C++20] [Modules] Use new class CXX20ModulesGenerator to genera… (#90570)
…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.
1 parent b2b463b commit fce0916

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ class ASTWriter : public ASTDeserializationListener,
890890
/// AST and semantic-analysis consumer that generates a
891891
/// precompiled header from the parsed source code.
892892
class PCHGenerator : public SemaConsumer {
893+
void anchor() override;
894+
893895
Preprocessor &PP;
894896
std::string OutputFile;
895897
std::string isysroot;
@@ -933,17 +935,34 @@ class PCHGenerator : public SemaConsumer {
933935
bool hasEmittedPCH() const { return Buffer->IsComplete; }
934936
};
935937

936-
class ReducedBMIGenerator : public PCHGenerator {
938+
class CXX20ModulesGenerator : public PCHGenerator {
939+
void anchor() override;
940+
937941
protected:
938942
virtual Module *getEmittingModule(ASTContext &Ctx) override;
939943

944+
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
945+
StringRef OutputFile, bool GeneratingReducedBMI);
946+
940947
public:
941-
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
942-
StringRef OutputFile);
948+
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
949+
StringRef OutputFile)
950+
: CXX20ModulesGenerator(PP, ModuleCache, OutputFile,
951+
/*GeneratingReducedBMI=*/false) {}
943952

944953
void HandleTranslationUnit(ASTContext &Ctx) override;
945954
};
946955

956+
class ReducedBMIGenerator : public CXX20ModulesGenerator {
957+
void anchor() override;
958+
959+
public:
960+
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
961+
StringRef OutputFile)
962+
: CXX20ModulesGenerator(PP, ModuleCache, OutputFile,
963+
/*GeneratingReducedBMI=*/true) {}
964+
};
965+
947966
/// If we can elide the definition of \param D in reduced BMI.
948967
///
949968
/// Generally, we can elide the definition of a declaration if it won't affect

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +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-
278-
std::vector<std::unique_ptr<ASTConsumer>> Consumers =
279-
CreateMultiplexConsumer(CI, InFile);
280-
if (Consumers.empty())
281-
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));
282279

283280
if (CI.getFrontendOpts().GenReducedBMI &&
284281
!CI.getFrontendOpts().ModuleOutputPath.empty()) {

clang/lib/Serialization/GeneratePCH.cpp

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

91-
ReducedBMIGenerator::ReducedBMIGenerator(Preprocessor &PP,
92-
InMemoryModuleCache &ModuleCache,
93-
StringRef OutputFile)
91+
void PCHGenerator::anchor() {}
92+
93+
CXX20ModulesGenerator::CXX20ModulesGenerator(Preprocessor &PP,
94+
InMemoryModuleCache &ModuleCache,
95+
StringRef OutputFile,
96+
bool GeneratingReducedBMI)
9497
: PCHGenerator(
9598
PP, ModuleCache, OutputFile, llvm::StringRef(),
9699
std::make_shared<PCHBuffer>(),
97100
/*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
98101
/*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
99102
/*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
100-
/*GeneratingReducedBMI=*/true) {}
103+
GeneratingReducedBMI) {}
101104

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

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-
//
112+
void CXX20ModulesGenerator::HandleTranslationUnit(ASTContext &Ctx) {
113113
// FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
114114
// since this is not about searching header really.
115-
// FIXME2: We'd better to move the class writing full BMI with reduced BMI.
116115
HeaderSearchOptions &HSOpts =
117116
getPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
118117
HSOpts.ModulesSkipDiagnosticOptions = true;
@@ -134,3 +133,7 @@ void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
134133
*OS << getBufferPtr()->Data;
135134
OS->flush();
136135
}
136+
137+
void CXX20ModulesGenerator::anchor() {}
138+
139+
void ReducedBMIGenerator::anchor() {}

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)