[PATCH] D147888: Update declaration message of extern linkage

Krishna Narayanan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 9 11:42:08 PDT 2023


Krishna-13-cyber created this revision.
Krishna-13-cyber added reviewers: tbaeder, cjdb, aaron.ballman.
Herald added a project: All.
Krishna-13-cyber requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This patch aims to improve the diagnostic to much better precision of indicating extern declaration being followed after static declaration rather than just generic non-static declaration.

**Example Testcase:** https://godbolt.org/z/zMTda6MGG


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147888

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/test/SemaCXX/extern_static.cpp


Index: clang/test/SemaCXX/extern_static.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/extern_static.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -Wabstract-vbase-init
+void f(void)
+{
+    static int x; // expected-note {{previous definition is here}}
+    extern int x; // expected-error {{extern declaration of 'x' follows static declaration}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -4637,6 +4637,15 @@
   //   the prior declaration. If no prior declaration is visible, or
   //   if the prior declaration specifies no linkage, then the
   //   identifier has external linkage.
+
+  if (New->hasExternalStorage() &&
+      Old->getCanonicalDecl()->getStorageClass() == SC_Static &&
+      Old->isLocalVarDeclOrParm()) {
+    Diag(New->getLocation(), diag::err_extern_static) << New->getDeclName();
+    Diag(OldLocation, PrevDiag);
+    return New->setInvalidDecl();
+  }
+
   if (New->hasExternalStorage() && Old->hasLinkage())
     /* Okay */;
   else if (New->getCanonicalDecl()->getStorageClass() != SC_Static &&
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5799,6 +5799,8 @@
   InGroup<MicrosoftRedeclareStatic>;
 def err_non_static_static : Error<
   "non-static declaration of %0 follows static declaration">;
+def err_extern_static : Error<
+  "extern declaration of %0 follows static declaration">;
 def err_extern_non_extern : Error<
   "extern declaration of %0 follows non-extern declaration">;
 def err_non_extern_extern : Error<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147888.512026.patch
Type: text/x-patch
Size: 1890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230409/a4e4fc42/attachment-0001.bin>


More information about the cfe-commits mailing list