[clang] [Clang] Diagnose UB and emit error when identifier has both internal and external linkage (PR #192116)

Aditya Medhane via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 21 10:54:21 PDT 2026


https://github.com/flash1729 updated https://github.com/llvm/llvm-project/pull/192116

>From e3e7693878ce36ecff5e652a88340b7828dab820 Mon Sep 17 00:00:00 2001
From: flash1729 <sherlockedaditya at gmail.com>
Date: Wed, 15 Apr 2026 00:42:00 +0530
Subject: [PATCH 1/6] [Clang] Diagnose UB when identifier has both internal and
 external linkage

C11 6.2.2p7 states that if the same identifier appears with both internal
and external linkage within a translation unit, the behavior is undefined.

This occurs when a block-scope extern declaration finds a file-scope static
declaration, but a no-linkage local variable shadows the static, preventing
linkage inheritance per C11 6.2.2p4.

Fixes #54215
---
 clang/docs/ReleaseNotes.rst                   |  6 +++-
 .../clang/Basic/DiagnosticSemaKinds.td        |  3 ++
 clang/lib/Sema/SemaDecl.cpp                   | 18 ++++++++++
 clang/test/Sema/linkage-internal-extern.c     | 36 +++++++++++++++++++
 4 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/linkage-internal-extern.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c311b3f6075d8..492f95a08c8f3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1,4 +1,4 @@
-.. If you want to modify sections/contents permanently, you should modify both
+a.. If you want to modify sections/contents permanently, you should modify both
    ReleaseNotes.rst and ReleaseNotesTemplate.txt.
 
 ===========================================
@@ -418,6 +418,10 @@ Improvements to Clang's diagnostics
   code can automatically be made portable to other host platforms that don't
   support backslashes.
 
+- Clang now diagnoses the undefined behavior described in C11 6.2.2p7 where
+  the same identifier appears with both internal and external linkage within a
+  translation unit. (#GH54215)
+
 - No longer emitting a ``-Wpre-c2y-compat`` or extension diagnostic about use
   of octal literals with a ``0o`` prefix, and no longer emitting a
   ``-Wdeprecated-octal-literals`` diagnostic for use of octal literals without
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index cc82bb6a51e52..0a5ae9ec3d54a 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6534,6 +6534,9 @@ def err_inline_declaration_block_scope : Error<
   "inline declaration of %0 not allowed in block scope">;
 def err_static_non_static : Error<
   "static declaration of %0 follows non-static declaration">;
+def err_internal_extern_mismatch : Error<
+  "variable %0 declared with both internal and external linkage "
+  "in the same translation unit; behavior is undefined">;
 def err_different_language_linkage : Error<
   "declaration of %0 has a different language linkage">;
 def ext_retained_language_linkage : Extension<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6afdd73328eba..75c313e9b2347 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4802,6 +4802,24 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
       return New->setInvalidDecl();
     }
   }
+
+  // C11 6.2.2p7:
+  //   If, within a translation unit, the same identifier appears with both
+  //   internal and external linkage, the behavior is undefined.
+  //
+  // This can occur when an extern declaration in block scope finds a file-scope
+  // static declaration, but a no-linkage local variable shadowed the static,
+  // preventing linkage inheritance per C11 6.2.2p4.
+  if (New->isLocalVarDecl() && New->hasExternalStorage() &&
+      Old->isFileVarDecl() && Old->hasLinkage() &&
+      Previous.isShadowed() &&
+      Old->getFormalLinkage() == Linkage::Internal) {
+    Diag(New->getLocation(), diag::err_internal_extern_mismatch)
+        << New->getDeclName();
+    Diag(OldLocation, PrevDiag);
+    return New->setInvalidDecl();
+  }
+
   // C99 6.2.2p4:
   //   For an identifier declared with the storage-class specifier
   //   extern in a scope in which a prior declaration of that
diff --git a/clang/test/Sema/linkage-internal-extern.c b/clang/test/Sema/linkage-internal-extern.c
new file mode 100644
index 0000000000000..42591c5b21f0b
--- /dev/null
+++ b/clang/test/Sema/linkage-internal-extern.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s
+
+// C11 6.2.2p7: same identifier with both internal and external linkage is UB.
+
+// Conflicting linkage (UB)
+
+static int x; // expected-note {{previous}}
+void test_basic_shadow(void) {
+    int x;
+    { extern int x; } // expected-error {{declared with both internal and external linkage}}
+}
+
+static int y; // expected-note {{previous}}
+void test_deep_nesting(void) {
+    int y;
+    { int y; { { extern int y; } } } // expected-error {{declared with both internal and external linkage}}
+}
+
+static int p; // expected-note {{previous}}
+void test_param_shadow(int p) {
+    { extern int p; } // expected-error {{declared with both internal and external linkage}}
+}
+
+// Valid cases
+
+static int a;
+void test_no_shadow(void) {
+    extern int a;
+}
+
+void test_no_file_scope(void) {
+    for (static int b = 0;;) {
+        extern int b;
+        break;
+    }
+}

>From 9f2afd197bb2a607e84797fbcf4f829321c0791c Mon Sep 17 00:00:00 2001
From: flash1729 <sherlockedaditya at gmail.com>
Date: Wed, 15 Apr 2026 01:07:53 +0530
Subject: [PATCH 2/6] fixup! [Clang] Diagnose UB when identifier has both
 internal and external linkage

---
 clang/lib/Sema/SemaDecl.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 75c313e9b2347..4d8892a906812 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4811,8 +4811,7 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
   // static declaration, but a no-linkage local variable shadowed the static,
   // preventing linkage inheritance per C11 6.2.2p4.
   if (New->isLocalVarDecl() && New->hasExternalStorage() &&
-      Old->isFileVarDecl() && Old->hasLinkage() &&
-      Previous.isShadowed() &&
+      Old->isFileVarDecl() && Old->hasLinkage() && Previous.isShadowed() &&
       Old->getFormalLinkage() == Linkage::Internal) {
     Diag(New->getLocation(), diag::err_internal_extern_mismatch)
         << New->getDeclName();

>From a53221eef0e9da040facf81d47618e1678db8c39 Mon Sep 17 00:00:00 2001
From: flash1729 <sherlockedaditya at gmail.com>
Date: Wed, 15 Apr 2026 01:39:21 +0530
Subject: [PATCH 3/6] fixup! [Clang] Diagnose UB when identifier has both
 internal and external linkage

---
 clang/lib/Sema/SemaDecl.cpp | 6 +++---
 clang/test/C/C2y/n3410.c    | 8 +++-----
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 4d8892a906812..f047a9ed354f5 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4810,9 +4810,9 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
   // This can occur when an extern declaration in block scope finds a file-scope
   // static declaration, but a no-linkage local variable shadowed the static,
   // preventing linkage inheritance per C11 6.2.2p4.
-  if (New->isLocalVarDecl() && New->hasExternalStorage() &&
-      Old->isFileVarDecl() && Old->hasLinkage() && Previous.isShadowed() &&
-      Old->getFormalLinkage() == Linkage::Internal) {
+  if (!getLangOpts().CPlusPlus && New->isLocalVarDecl() &&
+      New->hasExternalStorage() && Old->isFileVarDecl() && Old->hasLinkage() &&
+      Previous.isShadowed() && Old->getFormalLinkage() == Linkage::Internal) {
     Diag(New->getLocation(), diag::err_internal_extern_mismatch)
         << New->getDeclName();
     Diag(OldLocation, PrevDiag);
diff --git a/clang/test/C/C2y/n3410.c b/clang/test/C/C2y/n3410.c
index e1cb41f375b82..c7162f6d324d8 100644
--- a/clang/test/C/C2y/n3410.c
+++ b/clang/test/C/C2y/n3410.c
@@ -24,19 +24,17 @@ void func2() {
   extern int b; // Ok
 }
 
-static int c, d;
+static int c, d; // expected-note {{previous definition is here}} expected-note {{previous definition is here}}
 void func3() {
   int c; // no linkage, different object from the one declared above.
   for (int d;;) {
     // This 'c' is the same as the one declared at file scope, but because of
     // the local scope 'c', the file scope 'c' is not visible.
-    // FIXME: This should be diagnosed under N3410.
-    extern int c;
+    extern int c; // expected-error {{declared with both internal and external linkage}}
     // This 'd' is the same as the one declared at file scope as well, but
     // because of the 'd' declared within the for loop, the file scope 'd' is
     // also not visible, same as with 'c'.
-    // FIXME: This should be diagnosed under N3410.
-    extern int d;
+    extern int d; // expected-error {{declared with both internal and external linkage}}
   }
   for (static int e;;) {
     extern int e; // Ok for the same reason as 'b' above.

>From a1ae92795dd636deb2fe7515e899675b370d9486 Mon Sep 17 00:00:00 2001
From: flash1729 <sherlockedaditya at gmail.com>
Date: Sun, 19 Apr 2026 13:30:18 +0530
Subject: [PATCH 4/6] fixup! [Clang] Diagnose UB when identifier has both
 internal and external linkage

---
 clang/docs/ReleaseNotes.rst                   |  9 ++++++---
 .../clang/Basic/DiagnosticSemaKinds.td        |  2 +-
 clang/lib/Sema/SemaDecl.cpp                   | 10 ++++++----
 clang/test/C/C2y/n3410.c                      |  2 +-
 clang/test/Sema/linkage-internal-extern.cpp   | 19 +++++++++++++++++++
 clang/www/c_status.html                       |  2 +-
 6 files changed, 34 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/Sema/linkage-internal-extern.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 492f95a08c8f3..f0cc47ed8c597 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -155,6 +155,12 @@ C Language Changes
 C2y Feature Support
 ^^^^^^^^^^^^^^^^^^^
 
+- Clang now diagnoses the use of the same identifier with both internal and
+  external linkage within a translation unit, as made ill-formed by
+  `N3410 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3410.pdf>`_.
+  This is also diagnosed in older C language modes as the behavior was
+  undefined prior to C2y. (#GH54215)
+
 C23 Feature Support
 ^^^^^^^^^^^^^^^^^^^
 - Clang now allows C23 ``constexpr`` struct member access through the dot operator in constant expressions. (#GH178349)
@@ -418,9 +424,6 @@ Improvements to Clang's diagnostics
   code can automatically be made portable to other host platforms that don't
   support backslashes.
 
-- Clang now diagnoses the undefined behavior described in C11 6.2.2p7 where
-  the same identifier appears with both internal and external linkage within a
-  translation unit. (#GH54215)
 
 - No longer emitting a ``-Wpre-c2y-compat`` or extension diagnostic about use
   of octal literals with a ``0o`` prefix, and no longer emitting a
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a5ae9ec3d54a..b744cd5df1664 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6536,7 +6536,7 @@ def err_static_non_static : Error<
   "static declaration of %0 follows non-static declaration">;
 def err_internal_extern_mismatch : Error<
   "variable %0 declared with both internal and external linkage "
-  "in the same translation unit; behavior is undefined">;
+  "in the same translation unit%select{; behavior is undefined|}1">;
 def err_different_language_linkage : Error<
   "declaration of %0 has a different language linkage">;
 def ext_retained_language_linkage : Extension<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f047a9ed354f5..5e42a4686e26f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4803,18 +4803,20 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
     }
   }
 
-  // C11 6.2.2p7:
+  // C2y 6.2.2p7 (N3410):
   //   If, within a translation unit, the same identifier appears with both
-  //   internal and external linkage, the behavior is undefined.
+  //   internal and external linkage, the program is ill-formed.
+  //
+  // In C11 through C23, this was undefined behavior (C11 6.2.2p7).
   //
   // This can occur when an extern declaration in block scope finds a file-scope
   // static declaration, but a no-linkage local variable shadowed the static,
-  // preventing linkage inheritance per C11 6.2.2p4.
+  // preventing linkage inheritance per C2y 6.2.2p4.
   if (!getLangOpts().CPlusPlus && New->isLocalVarDecl() &&
       New->hasExternalStorage() && Old->isFileVarDecl() && Old->hasLinkage() &&
       Previous.isShadowed() && Old->getFormalLinkage() == Linkage::Internal) {
     Diag(New->getLocation(), diag::err_internal_extern_mismatch)
-        << New->getDeclName();
+        << New->getDeclName() << getLangOpts().C2y;
     Diag(OldLocation, PrevDiag);
     return New->setInvalidDecl();
   }
diff --git a/clang/test/C/C2y/n3410.c b/clang/test/C/C2y/n3410.c
index c7162f6d324d8..27baf9b44458a 100644
--- a/clang/test/C/C2y/n3410.c
+++ b/clang/test/C/C2y/n3410.c
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic -Wno-unused %s
 
-/* WG14 N3410: No
+/* WG14 N3410: Yes
  * Slay Some Earthly Demons XI
  *
  * It is now ill-formed for the same identifier within a TU to have both
diff --git a/clang/test/Sema/linkage-internal-extern.cpp b/clang/test/Sema/linkage-internal-extern.cpp
new file mode 100644
index 0000000000000..ab084767305fd
--- /dev/null
+++ b/clang/test/Sema/linkage-internal-extern.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++17 %s
+
+// expected-no-diagnostics
+
+// CWG 426 / [basic.link]p6: the same identifier with both internal and
+// external linkage should be ill-formed in C++, but Clang does not yet
+// diagnose this due to ABI break concerns. This test documents current
+// behavior.
+
+static int x;
+void test_shadow(void) {
+    int x;
+    {
+        // FIXME: Per CWG 426, this should be ill-formed because the
+        // file-scope 'x' has internal linkage but this 'extern' gets
+        // external linkage due to the local shadow.
+        extern int x;
+    }
+}
diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index 5270033471167..8d8ca9f35686e 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -283,7 +283,7 @@ <h2 id="c2y">C2y implementation status</h2>
     <tr>
       <td>Slay Some Earthly Demons XI</td>
       <td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3410.pdf">N3410</a></td>
-      <td class="none" align="center">No</td>
+      <td class="full" align="center">Clang 21</td>
 	</tr>
     <tr>
       <td>Slay Some Earthly Demons XII</td>

>From 9e48cf2306f9dfa63d98ae07e04a3f9a2077a7e6 Mon Sep 17 00:00:00 2001
From: flash1729 <sherlockedaditya at gmail.com>
Date: Sun, 19 Apr 2026 13:45:51 +0530
Subject: [PATCH 5/6] fixup! [Clang] Diagnose UB when identifier has both
 internal and external linkage

---
 clang/www/c_status.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index 8d8ca9f35686e..639400e3fd386 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -283,7 +283,7 @@ <h2 id="c2y">C2y implementation status</h2>
     <tr>
       <td>Slay Some Earthly Demons XI</td>
       <td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3410.pdf">N3410</a></td>
-      <td class="full" align="center">Clang 21</td>
+      <td class="full" align="center">Yes</td>
 	</tr>
     <tr>
       <td>Slay Some Earthly Demons XII</td>

>From ad332e71dfea6c2935497a0397e12100f3468360 Mon Sep 17 00:00:00 2001
From: flash1729 <sherlockedaditya at gmail.com>
Date: Tue, 21 Apr 2026 22:21:42 +0530
Subject: [PATCH 6/6] fixup! [Clang] Diagnose UB when identifier has both
 internal and external linkage

---
 clang/lib/Sema/SemaDecl.cpp | 6 +++---
 clang/test/C/C2y/n3410.c    | 2 +-
 clang/www/c_status.html     | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5e42a4686e26f..83f536cef913d 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4803,9 +4803,9 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
     }
   }
 
-  // C2y 6.2.2p7 (N3410):
-  //   If, within a translation unit, the same identifier appears with both
-  //   internal and external linkage, the program is ill-formed.
+  // C2y 6.7.1p7:
+  //   Within a translation unit, the same identifier shall not appear with
+  //   both internal and external linkage.
   //
   // In C11 through C23, this was undefined behavior (C11 6.2.2p7).
   //
diff --git a/clang/test/C/C2y/n3410.c b/clang/test/C/C2y/n3410.c
index 27baf9b44458a..6e65666887fe5 100644
--- a/clang/test/C/C2y/n3410.c
+++ b/clang/test/C/C2y/n3410.c
@@ -24,7 +24,7 @@ void func2() {
   extern int b; // Ok
 }
 
-static int c, d; // expected-note {{previous definition is here}} expected-note {{previous definition is here}}
+static int c, d; // expected-note 2 {{previous definition is here}}
 void func3() {
   int c; // no linkage, different object from the one declared above.
   for (int d;;) {
diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index 639400e3fd386..4b3695831de58 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -283,7 +283,7 @@ <h2 id="c2y">C2y implementation status</h2>
     <tr>
       <td>Slay Some Earthly Demons XI</td>
       <td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3410.pdf">N3410</a></td>
-      <td class="full" align="center">Yes</td>
+      <td class="unreleased" align="center">Clang 23</td>
 	</tr>
     <tr>
       <td>Slay Some Earthly Demons XII</td>



More information about the cfe-commits mailing list