[clang] [C++20] [Modules] Warn for importing implementation partition unit in interface units (PR #108493)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 13 02:10:06 PDT 2024
https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/108493
>From b109ea29ee6d1c290766e141ce75317fbf450767 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Fri, 13 Sep 2024 13:18:43 +0800
Subject: [PATCH 1/2] [C++20] [Modules] Warn for importing implementation
partition unit in interface units
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 +++
clang/lib/Sema/SemaModule.cpp | 7 +++++++
clang/test/CXX/module/module.import/p2.cpp | 3 +--
clang/test/Modules/cxx20-10-3-ex1.cpp | 1 +
4 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index efdc058edca56d..7545a06123789d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -439,6 +439,9 @@ def warn_deprecated_literal_operator_id: Warning<
"is deprecated">, InGroup<DeprecatedLiteralOperator>, DefaultIgnore;
def warn_reserved_module_name : Warning<
"%0 is a reserved name for a module">, InGroup<ReservedModuleIdentifier>;
+def warn_import_implementation_partition_unit_in_interface_unit : Warning<
+ "it is not suggested to import implementation partition unit in interface unit">,
+ InGroup<DiagGroup<"import-implementation-partition-unit-in-interface-unit">>;
def warn_parameter_size: Warning<
"%0 is a large (%1 bytes) pass-by-value argument; "
diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp
index 3b84e7bd4277fd..9a80adbde60d19 100644
--- a/clang/lib/Sema/SemaModule.cpp
+++ b/clang/lib/Sema/SemaModule.cpp
@@ -650,6 +650,13 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
else
VisibleModules.setVisible(Mod, ImportLoc);
+ assert((!Mod->isModulePartitionImplementation() || getCurrentModule()) &&
+ "We can only import a partition unit in a named module.");
+ if (Mod->isModulePartitionImplementation() &&
+ getCurrentModule()->isModuleInterfaceUnit())
+ Diag(ImportLoc,
+ diag::warn_import_implementation_partition_unit_in_interface_unit);
+
checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
// FIXME: we should support importing a submodule within a different submodule
diff --git a/clang/test/CXX/module/module.import/p2.cpp b/clang/test/CXX/module/module.import/p2.cpp
index ef6006811e7763..3ac76856c7cfc3 100644
--- a/clang/test/CXX/module/module.import/p2.cpp
+++ b/clang/test/CXX/module/module.import/p2.cpp
@@ -30,9 +30,8 @@ void test() {
}
//--- UseInPartA.cppm
-// expected-no-diagnostics
export module M:partA;
-import :impl;
+import :impl; // expected-warning {{it is not suggested to import implementation partition unit in interface unit}}
void test() {
A a;
}
diff --git a/clang/test/Modules/cxx20-10-3-ex1.cpp b/clang/test/Modules/cxx20-10-3-ex1.cpp
index 99b88c7e442ffd..dcdc92340366ae 100644
--- a/clang/test/Modules/cxx20-10-3-ex1.cpp
+++ b/clang/test/Modules/cxx20-10-3-ex1.cpp
@@ -37,6 +37,7 @@ module M:PartImpl;
export module M;
// error: exported partition :Part is an implementation unit
export import :PartImpl; // expected-error {{module partition implementations cannot be exported}}
+ // expected-warning at -1 {{it is not suggested to import implementation partition unit in interface unit}}
//--- std10-3-ex1-tu3.cpp
export module M:Part;
>From c3d3f78ce81a7a1b5f589624a9a792b46f3ff811 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Fri, 13 Sep 2024 17:09:30 +0800
Subject: [PATCH 2/2] address comments
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 ++-
clang/lib/Sema/SemaModule.cpp | 3 ++-
clang/test/CXX/module/module.import/p2.cpp | 2 +-
clang/test/Modules/cxx20-10-3-ex1.cpp | 2 +-
4 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 7545a06123789d..10bb91564c2afa 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -440,7 +440,8 @@ def warn_deprecated_literal_operator_id: Warning<
def warn_reserved_module_name : Warning<
"%0 is a reserved name for a module">, InGroup<ReservedModuleIdentifier>;
def warn_import_implementation_partition_unit_in_interface_unit : Warning<
- "it is not suggested to import implementation partition unit in interface unit">,
+ "importing an implementation partition unit in a module interface is not recommended. "
+ "Names in by %0 may not be reachable">,
InGroup<DiagGroup<"import-implementation-partition-unit-in-interface-unit">>;
def warn_parameter_size: Warning<
diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp
index 9a80adbde60d19..4b3a480a2f8f4a 100644
--- a/clang/lib/Sema/SemaModule.cpp
+++ b/clang/lib/Sema/SemaModule.cpp
@@ -655,7 +655,8 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
if (Mod->isModulePartitionImplementation() &&
getCurrentModule()->isModuleInterfaceUnit())
Diag(ImportLoc,
- diag::warn_import_implementation_partition_unit_in_interface_unit);
+ diag::warn_import_implementation_partition_unit_in_interface_unit)
+ << Mod->Name << "\n";
checkModuleImportContext(*this, Mod, ImportLoc, CurContext);
diff --git a/clang/test/CXX/module/module.import/p2.cpp b/clang/test/CXX/module/module.import/p2.cpp
index 3ac76856c7cfc3..6b8e32f746b628 100644
--- a/clang/test/CXX/module/module.import/p2.cpp
+++ b/clang/test/CXX/module/module.import/p2.cpp
@@ -31,7 +31,7 @@ void test() {
//--- UseInPartA.cppm
export module M:partA;
-import :impl; // expected-warning {{it is not suggested to import implementation partition unit in interface unit}}
+import :impl; // expected-warning {{importing an implementation partition unit in a module interface is not recommended.}}
void test() {
A a;
}
diff --git a/clang/test/Modules/cxx20-10-3-ex1.cpp b/clang/test/Modules/cxx20-10-3-ex1.cpp
index dcdc92340366ae..82ecb40df910fa 100644
--- a/clang/test/Modules/cxx20-10-3-ex1.cpp
+++ b/clang/test/Modules/cxx20-10-3-ex1.cpp
@@ -37,7 +37,7 @@ module M:PartImpl;
export module M;
// error: exported partition :Part is an implementation unit
export import :PartImpl; // expected-error {{module partition implementations cannot be exported}}
- // expected-warning at -1 {{it is not suggested to import implementation partition unit in interface unit}}
+ // expected-warning at -1 {{importing an implementation partition unit in a module interface is not recommended.}}
//--- std10-3-ex1-tu3.cpp
export module M:Part;
More information about the cfe-commits
mailing list