Skip to content

Commit 2eeeff8

Browse files
authored
[C++20] [Modules] Embed all source files for C++20 Modules (#102444)
Close #72383 The implementation rationale is, I don't want to pass `-fmodules-embed-all-files` all the time since we can't test it in lit tests (we're using `clang_cc1`). So I tried to set it in FrontendActions for modules.
1 parent e5b55e6 commit 2eeeff8

9 files changed

+42
-19
lines changed

clang/include/clang/CodeGen/CodeGenAction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CodeGenAction : public ASTFrontendAction {
5757
bool loadLinkModules(CompilerInstance &CI);
5858

5959
protected:
60-
bool BeginSourceFileAction(CompilerInstance &CI) override;
60+
bool BeginInvocation(CompilerInstance &CI) override;
6161

6262
/// Create a new code generation action. If the optional \p _VMContext
6363
/// parameter is supplied, the action uses it without taking ownership,

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
152152
CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
153153
};
154154

155+
bool BeginInvocationForModules(CompilerInstance &CI);
156+
155157
/// Generates full BMI (which contains full information to generate the object
156158
/// files) for C++20 Named Modules.
157159
class GenerateModuleInterfaceAction : public GenerateModuleAction {
158160
protected:
159-
bool BeginSourceFileAction(CompilerInstance &CI) override;
161+
bool BeginInvocation(CompilerInstance &CI) override;
160162

161163
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
162164
StringRef InFile) override;

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ class InputFile {
8888

8989
InputFile(FileEntryRef File, bool isOverridden = false,
9090
bool isOutOfDate = false) {
91-
assert(!(isOverridden && isOutOfDate) &&
92-
"an overridden cannot be out-of-date");
9391
unsigned intVal = 0;
94-
if (isOverridden)
95-
intVal = Overridden;
96-
else if (isOutOfDate)
92+
// Make isOutOfDate with higher priority than isOverridden.
93+
// It is possible if the recorded hash value mismatches.
94+
if (isOutOfDate)
9795
intVal = OutOfDate;
96+
else if (isOverridden)
97+
intVal = Overridden;
9898
Val.setPointerAndInt(&File.getMapEntry(), intVal);
9999
}
100100

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,10 @@ CodeGenerator *CodeGenAction::getCodeGenerator() const {
969969
return BEConsumer->getCodeGenerator();
970970
}
971971

972-
bool CodeGenAction::BeginSourceFileAction(CompilerInstance &CI) {
972+
bool CodeGenAction::BeginInvocation(CompilerInstance &CI) {
973973
if (CI.getFrontendOpts().GenReducedBMI)
974-
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
974+
return BeginInvocationForModules(CI);
975+
975976
return true;
976977
}
977978

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,20 @@ GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
262262
/*ForceUseTemporary=*/true);
263263
}
264264

265-
bool GenerateModuleInterfaceAction::BeginSourceFileAction(
266-
CompilerInstance &CI) {
265+
bool clang::BeginInvocationForModules(CompilerInstance &CI) {
266+
// Embed all module files for named modules.
267+
// See https://github.com/llvm/llvm-project/issues/72383 for discussion.
268+
CI.getFrontendOpts().ModulesEmbedAllFiles = true;
267269
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
270+
return true;
271+
}
268272

269-
return GenerateModuleAction::BeginSourceFileAction(CI);
273+
bool GenerateModuleInterfaceAction::BeginInvocation(
274+
CompilerInstance &CI) {
275+
if (!BeginInvocationForModules(CI))
276+
return false;
277+
278+
return GenerateModuleAction::BeginInvocation(CI);
270279
}
271280

272281
std::unique_ptr<ASTConsumer>

clang/test/Modules/no-local-decl-in-reduced-bmi.cppm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
//
77
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -emit-reduced-module-interface -o %t/a.pcm
88
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/a.pcm > %t/a.dump
9-
// RUN: cat %t/a.dump | FileCheck %t/a.cppm
9+
// RUN: cat %t/a.dump | FileCheck %t/a.check
1010
//
1111
// RUN: %clang_cc1 -std=c++20 %t/b.cppm -emit-reduced-module-interface -o %t/b.pcm
1212
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/b.pcm > %t/b.dump
13-
// RUN: cat %t/b.dump | FileCheck %t/b.cppm
13+
// RUN: cat %t/b.dump | FileCheck %t/b.check
1414

1515
//--- a.cppm
1616
export module a;
@@ -19,6 +19,9 @@ export int func() {
1919
return 43;
2020
}
2121

22+
//--- a.check
23+
// Use a standalone check file since now we're going to embed all source files in the BMI
24+
// so we will check the `CHECK-NOT: <DECL_VAR` in the source file otherwise.
2225
// Test that the variable declaration is not recorded completely.
2326
// CHECK-NOT: <DECL_VAR
2427

@@ -29,5 +32,6 @@ export inline int func() {
2932
return v;
3033
}
3134

35+
//--- b.check
3236
// Check that we still records the declaration from inline functions.
3337
// CHECK: <DECL_VAR

clang/test/Modules/reduced-bmi-empty-module-purview-std.cppm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-reduced-module-interface -o %t/A.pcm
99
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/A.pcm > %t/A.dump
10-
// RUN: cat %t/A.dump | FileCheck %t/A.cppm
10+
// RUN: cat %t/A.dump | FileCheck %t/A.check
1111

1212
//--- std.h
1313
namespace std {
@@ -22,6 +22,8 @@ module;
2222
#include "std.h"
2323
export module A;
2424

25+
26+
//--- A.check
2527
// CHECK-NOT: <DECL_NAMESPACE
2628
// CHECK-NOT: <DECL_CONTEXT_LEXICAL
2729
// CHECK-NOT: <DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD

clang/test/Modules/reduced-bmi-empty-module-purview.cppm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-reduced-module-interface -o %t/A.pcm \
1010
// RUN: -fmodule-file=M=%t/M.pcm
1111
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/A.pcm > %t/A.dump
12-
// RUN: cat %t/A.dump | FileCheck %t/A.cppm
12+
// RUN: cat %t/A.dump | FileCheck %t/A.check
1313
//
1414
// RUN: %clang_cc1 -std=c++20 %t/A1.cppm -emit-reduced-module-interface -o %t/A1.pcm \
1515
// RUN: -fmodule-file=M=%t/M.pcm
1616
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/A1.pcm > %t/A1.dump
17-
// RUN: cat %t/A1.dump | FileCheck %t/A1.cppm
17+
// RUN: cat %t/A1.dump | FileCheck %t/A1.check
1818

1919
//--- foo.h
2020
namespace ns {
@@ -82,6 +82,7 @@ module;
8282
export module A;
8383
import M;
8484

85+
//--- A.check
8586
// CHECK-NOT: <DECL_CXX_RECORD
8687
// CHECK-NOT: <DECL_UPDATE_OFFSETS
8788

@@ -91,6 +92,7 @@ import M;
9192
#include "foo.h"
9293
export module A;
9394

95+
//--- A1.check
9496
// CHECK-NOT: <DECL_CXX_RECORD
9597
// CHECK-NOT: <DECL_UPDATE_OFFSETS
9698

clang/test/Modules/unreached-static-entities.cppm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
//
44
// RUN: rm -rf %t
55
// RUN: mkdir -p %t
6+
// RUN: split-file %s %t
67
//
7-
// RUN: %clang_cc1 -std=c++20 %s -emit-reduced-module-interface -o %t/S.pcm
8+
// RUN: %clang_cc1 -std=c++20 %t/S.cppm -emit-reduced-module-interface -o %t/S.pcm
89
// RUN: llvm-bcanalyzer --dump --disable-histogram --show-binary-blobs %t/S.pcm > %t/S.dump
9-
// RUN: cat %t/S.dump | FileCheck %s
10+
// RUN: cat %t/S.dump | FileCheck %t/S.check
1011

12+
//--- S.cppm
1113
export module S;
1214
static int static_func() {
1315
return 43;
@@ -17,6 +19,7 @@ export int func() {
1719
return static_func();
1820
}
1921

22+
//--- S.check
2023
// CHECK: <DECL_FUNCTION
2124
// Checks that we won't see a second function
2225
// CHECK-NOT: <DECL_FUNCTION

0 commit comments

Comments
 (0)