Skip to content

Commit 574ee1c

Browse files
committed
[C++20] [Modules] Prevent to accept clang modules
Close #64755 This wouldn't affect the form @import as the test shows. The two affected test case `diag-flags.cpp` and `diag-pragma.cpp` are old test cases in 2017 and 2018, when we're not so clear about the direction of modules. And the things that these 2 tests tested can be covered by clang modules naturally. So I change the them into clang modules to not block this patch.
1 parent d3402bc commit 574ee1c

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11318,6 +11318,8 @@ def err_module_import_in_implementation : Error<
1131811318
"@import of module '%0' in implementation of '%1'; use #import">;
1131911319

1132011320
// C++ Modules
11321+
def err_module_import_non_interface_nor_parition : Error<
11322+
"import of module '%0' imported non C++20 importable modules">;
1132111323
def err_module_decl_not_at_start : Error<
1132211324
"module declaration must occur at the start of the translation unit">;
1132311325
def note_global_module_introducer_missing : Note<

clang/lib/Sema/SemaModule.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
531531
if (!Mod)
532532
return true;
533533

534+
if (!Mod->isInterfaceOrPartition() && !ModuleName.empty()) {
535+
Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
536+
<< ModuleName;
537+
return true;
538+
}
539+
534540
return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
535541
}
536542

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11

22
// RUN: rm -rf %t
3-
// RUN: %clang_cc1 -std=c++20 -fmodules -fmodules-cache-path=%t -fimplicit-module-maps -I%S/Inputs -verify %s
3+
// RUN: mkdir -p %t
4+
// RUN: split-file %s %t
5+
//
6+
// RUN: %clang_cc1 -std=c++20 %t/dummy.cppm -emit-module-interface -o %t/dummy.pcm
7+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -verify %t/test.cpp
8+
9+
10+
//--- dummy.cppm
11+
export module dummy;
12+
13+
//--- test.cpp
414
export import dummy; // expected-error {{export declaration can only be used within a module purview}}

clang/test/Modules/diag-flags.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// RUN: %clang_cc1 -triple %itanium_abi_triple -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -std=c++20 -DERROR -fmodule-file=%t/werror.pcm -Wno-error
3131
// RUN: %clang_cc1 -triple %itanium_abi_triple -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -std=c++20 -DERROR -fmodule-file=%t/werror.pcm -Wno-padded
3232

33-
import diag_flags;
33+
#include "diag_flags.h"
3434

3535
// Diagnostic flags from the module user make no difference to diagnostics
3636
// emitted within the module when using an explicitly-loaded module.

clang/test/Modules/diag-pragma.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -emit-module -fmodule-name=diag_pragma -x c++ %S/Inputs/module.map -std=c++20 -o %t/explicit.pcm -Werror=string-plus-int
55
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -std=c++20 -DEXPLICIT_FLAG -fmodule-file=%t/explicit.pcm
66

7-
import diag_pragma;
7+
#include "diag_pragma.h"
88

99
int foo(int x) {
1010
// Diagnostics from templates in the module follow the diagnostic state from
@@ -42,7 +42,6 @@ int foo(int x) {
4242

4343
if (x = DIAG_PRAGMA_MACRO) // expected-warning {{using the result of an assignment as a condition without parentheses}} \
4444
// expected-note {{place parentheses}} expected-note {{use '=='}}
45-
// expected-error@-2 {{use of undeclared identifier 'DIAG_PRAGMA_MACRO'}}
4645
return 0;
4746
return 1;
4847
}

clang/test/Modules/pr64755.cppm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I%t -fmodule-name=a0 -x c++ -emit-module %t/module.modulemap -o %t/a0.pcm
6+
// RUN: %clang_cc1 -std=c++20 %t/use.cpp -fmodule-file=%t/a0.pcm -verify -fsyntax-only
7+
// RUN: %clang_cc1 -std=c++20 %t/use.cpp -fmodule-file=a0=%t/a0.pcm -verify -fsyntax-only
8+
// RUN: %clang_cc1 -std=c++20 %t/use.cpp -fprebuilt-module-path=%t -verify -fsyntax-only
9+
10+
//--- module.modulemap
11+
module a0 { header "a0.h" export * }
12+
13+
//--- a0.h
14+
void a0() {}
15+
16+
//--- use.cpp
17+
import a0; // expected-error {{import of module 'a0' imported non C++20 importable modules}}

0 commit comments

Comments
 (0)