[clang] 234aacf - [C++20] [Modules] Diagnose for duplicated definition in the same module (#186959)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 16 23:29:50 PDT 2026
Author: Chuanqi Xu
Date: 2026-03-17T06:29:45Z
New Revision: 234aacf8e895ed9dd8c5e09d980cb8f85dc77123
URL: https://github.com/llvm/llvm-project/commit/234aacf8e895ed9dd8c5e09d980cb8f85dc77123
DIFF: https://github.com/llvm/llvm-project/commit/234aacf8e895ed9dd8c5e09d980cb8f85dc77123.diff
LOG: [C++20] [Modules] Diagnose for duplicated definition in the same module (#186959)
Close https://github.com/llvm/llvm-project/issues/186603
Added:
clang/test/Modules/pr186603.cppm
Modified:
clang/lib/Sema/SemaDecl.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c5e8046c2b5e5..ac38f32882d9b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -21216,9 +21216,14 @@ bool Sema::shouldIgnoreInHostDeviceCheck(FunctionDecl *Callee) {
bool Sema::isRedefinitionAllowedFor(NamedDecl *D, NamedDecl **Suggested,
bool &Visible) {
Visible = hasVisibleDefinition(D, Suggested);
+ // Accoding to [basic.def.odr]p16, it is not allowed to have duplicated definition
+ // for declaratins which is attached to named modules.
+ // We only did this if the current module is named module as we have better
+ // diagnostics for declarations in global module and named modules.
+ if (getCurrentModule() && getCurrentModule()->isNamedModule() &&
+ D->isInNamedModule())
+ return false;
// The redefinition of D in the **current** TU is allowed if D is invisible or
- // D is defined in the global module of other module units. We didn't check if
- // it is in global module as, we'll check the redefinition in named module
- // later with better diagnostic message.
+ // D is defined in the global module of other module units.
return D->isInAnotherModuleUnit() || !Visible;
}
diff --git a/clang/test/Modules/pr186603.cppm b/clang/test/Modules/pr186603.cppm
new file mode 100644
index 0000000000000..199efde757ffc
--- /dev/null
+++ b/clang/test/Modules/pr186603.cppm
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// Test that duplicate inline function definitions in
diff erent partitions
+// of the same named module are diagnosed as ODR violations.
+// See https://github.com/llvm/llvm-project/issues/186603
+//
+// Case 1: Module interface imports partition and redefines inline function
+// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/partition1.cppm -o %t/A-P1.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/module1.cppm \
+// RUN: -fmodule-file=A:P1=%t/A-P1.pcm -o %t/A.pcm -verify
+
+//--- partition1.cppm
+export module A:P1;
+export inline void x() {}
+
+//--- module1.cppm
+export module A;
+import :P1;
+export inline void x() {} // expected-error {{redefinition of 'x'}}
+// expected-note at partition1.cppm:* {{previous definition is here}}
\ No newline at end of file
More information about the cfe-commits
mailing list