Skip to content

Commit

Permalink
[C++20] [Modules] Prevent to accept clang modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChuanqiXu9 committed Aug 17, 2023
1 parent d3402bc commit 574ee1c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -11318,6 +11318,8 @@ def err_module_import_in_implementation : Error<
"@import of module '%0' in implementation of '%1'; use #import">;

// C++ Modules
def err_module_import_non_interface_nor_parition : Error<
"import of module '%0' imported non C++20 importable modules">;
def err_module_decl_not_at_start : Error<
"module declaration must occur at the start of the translation unit">;
def note_global_module_introducer_missing : Note<
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
if (!Mod)
return true;

if (!Mod->isInterfaceOrPartition() && !ModuleName.empty()) {
Diag(ImportLoc, diag::err_module_import_non_interface_nor_parition)
<< ModuleName;
return true;
}

return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
}

Expand Down
12 changes: 11 additions & 1 deletion clang/test/Modules/cxx20-export-import.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

// RUN: rm -rf %t
// RUN: %clang_cc1 -std=c++20 -fmodules -fmodules-cache-path=%t -fimplicit-module-maps -I%S/Inputs -verify %s
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/dummy.cppm -emit-module-interface -o %t/dummy.pcm
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -verify %t/test.cpp


//--- dummy.cppm
export module dummy;

//--- test.cpp
export import dummy; // expected-error {{export declaration can only be used within a module purview}}
2 changes: 1 addition & 1 deletion clang/test/Modules/diag-flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// 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
// 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

import diag_flags;
#include "diag_flags.h"

// Diagnostic flags from the module user make no difference to diagnostics
// emitted within the module when using an explicitly-loaded module.
Expand Down
3 changes: 1 addition & 2 deletions clang/test/Modules/diag-pragma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
// 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

import diag_pragma;
#include "diag_pragma.h"

int foo(int x) {
// Diagnostics from templates in the module follow the diagnostic state from
Expand Down Expand Up @@ -42,7 +42,6 @@ int foo(int x) {

if (x = DIAG_PRAGMA_MACRO) // expected-warning {{using the result of an assignment as a condition without parentheses}} \
// expected-note {{place parentheses}} expected-note {{use '=='}}
// expected-error@-2 {{use of undeclared identifier 'DIAG_PRAGMA_MACRO'}}
return 0;
return 1;
}
17 changes: 17 additions & 0 deletions clang/test/Modules/pr64755.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// 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
// RUN: %clang_cc1 -std=c++20 %t/use.cpp -fmodule-file=%t/a0.pcm -verify -fsyntax-only
// RUN: %clang_cc1 -std=c++20 %t/use.cpp -fmodule-file=a0=%t/a0.pcm -verify -fsyntax-only
// RUN: %clang_cc1 -std=c++20 %t/use.cpp -fprebuilt-module-path=%t -verify -fsyntax-only

//--- module.modulemap
module a0 { header "a0.h" export * }

//--- a0.h
void a0() {}

//--- use.cpp
import a0; // expected-error {{import of module 'a0' imported non C++20 importable modules}}

0 comments on commit 574ee1c

Please sign in to comment.