[PATCH] D133088: [Clang] Fix wrong diagnostic for scope identifier with internal linkage

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 31 21:32:30 PDT 2022


junaire updated this revision to Diff 457171.
junaire added a comment.

Simpify code a little bit.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133088/new/

https://reviews.llvm.org/D133088

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/test/Sema/error-decl-block-internal-linkage-no-init.c


Index: clang/test/Sema/error-decl-block-internal-linkage-no-init.c
===================================================================
--- /dev/null
+++ clang/test/Sema/error-decl-block-internal-linkage-no-init.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify %s
+static int x;
+
+void f(void)
+{
+    extern int x = 1; // expected-error {{declaration of block scope identifier with internal linkage shall have no initializer}}
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -12771,9 +12771,14 @@
     return;
   }
 
+  // C99 6.7.8p5. C++ has no such restriction, but that is a defect.
   if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) {
-    // C99 6.7.8p5. C++ has no such restriction, but that is a defect.
-    Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
+    unsigned DiagKind = diag::err_block_extern_cant_init;
+    // C2x 6.7.10p6.
+    if (VDecl->getFormalLinkage() == InternalLinkage)
+      DiagKind = diag::err_block_internal_linkage_no_init;
+
+    Diag(VDecl->getLocation(), DiagKind);
     VDecl->setInvalidDecl();
     return;
   }
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5896,6 +5896,9 @@
             "'loader_uninitialized' attribute">;
 def err_block_extern_cant_init : Error<
   "'extern' variable cannot have an initializer">;
+def err_block_internal_linkage_no_init : Error<
+  "declaration of block scope identifier with internal linkage shall "
+  "have no initializer">;
 def warn_extern_init : Warning<"'extern' variable has an initializer">,
   InGroup<DiagGroup<"extern-initializer">>;
 def err_variable_object_no_init : Error<
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -116,6 +116,9 @@
 - Correctly diagnose a future keyword if it exist as a keyword in the higher
   language version and specifies in which version it will be a keyword. This
   supports both c and c++ language.
+- Clang will now give a new more accurate diagnostic for declaration of block
+  scope identifiers that have internal linkage that has an initializer.
+  Fixes `Issue 57478: <https://github.com/llvm/llvm-project/issues/57478>`_.
 
 Non-comprehensive list of changes in this release
 -------------------------------------------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133088.457171.patch
Type: text/x-patch
Size: 2665 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220901/6296810b/attachment.bin>


More information about the cfe-commits mailing list