Author: Chuanqi Xu Date: 2023-08-17T11:45:50+08:00 New Revision: 574ee1c02ef73b66c5957cf93888234b0471695f
URL: https://github.com/llvm/llvm-project/commit/574ee1c02ef73b66c5957cf93888234b0471695f DIFF: https://github.com/llvm/llvm-project/commit/574ee1c02ef73b66c5957cf93888234b0471695f.diff LOG: [C++20] [Modules] Prevent to accept clang modules Close https://github.com/llvm/llvm-project/issues/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. Added: clang/test/Modules/pr64755.cppm Modified: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaModule.cpp clang/test/Modules/cxx20-export-import.cpp clang/test/Modules/diag-flags.cpp clang/test/Modules/diag-pragma.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 935fad819b82a4..632be32cfddd5a 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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< diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp index cd38cd4cf69d88..9b8af60a93d102 100644 --- a/clang/lib/Sema/SemaModule.cpp +++ b/clang/lib/Sema/SemaModule.cpp @@ -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); } diff --git a/clang/test/Modules/cxx20-export-import.cpp b/clang/test/Modules/cxx20-export-import.cpp index c40069e1df20d1..0b505668e85891 100644 --- a/clang/test/Modules/cxx20-export-import.cpp +++ b/clang/test/Modules/cxx20-export-import.cpp @@ -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}} diff --git a/clang/test/Modules/diag-flags.cpp b/clang/test/Modules/diag-flags.cpp index 4902cc9392cced..0db8b4412673d3 100644 --- a/clang/test/Modules/diag-flags.cpp +++ b/clang/test/Modules/diag-flags.cpp @@ -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 diff erence to diagnostics // emitted within the module when using an explicitly-loaded module. diff --git a/clang/test/Modules/diag-pragma.cpp b/clang/test/Modules/diag-pragma.cpp index f2e0206f7b0887..b1f10365e2356c 100644 --- a/clang/test/Modules/diag-pragma.cpp +++ b/clang/test/Modules/diag-pragma.cpp @@ -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 @@ -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; } diff --git a/clang/test/Modules/pr64755.cppm b/clang/test/Modules/pr64755.cppm new file mode 100644 index 00000000000000..75ef843154610d --- /dev/null +++ b/clang/test/Modules/pr64755.cppm @@ -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}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits