[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:11:57 PDT 2022
junaire created this revision.
junaire added a reviewer: aaron.ballman.
Herald added a project: All.
junaire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
If the declaration of an identifier has block scope, and the identifier has
external or internal linkage, the declaration shall have no initializer for
the identifier.
Clang now gives a correct diagnostic for this case.
Fixes https://github.com/llvm/llvm-project/issues/57478
Signed-off-by: Jun Zhang <jun at junz.org>
Repository:
rG LLVM Github Monorepo
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,17 @@
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);
+
+ // C2x 6.7.10p6.
+ unsigned DiagKind = diag::err_block_extern_cant_init;
+ bool IsInBlock =
+ getCurScope()->getFlags() & (Scope::FnScope | Scope::BlockScope);
+ if (IsInBlock && 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.457168.patch
Type: text/x-patch
Size: 2777 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220901/ffcc0f76/attachment-0001.bin>
More information about the cfe-commits
mailing list