[PATCH] D147888: Update declaration message of extern linkage

Krishna Narayanan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 21 11:33:02 PDT 2023


Krishna-13-cyber updated this revision to Diff 515843.
Krishna-13-cyber added a comment.

I have tried a little modification from my side thinking on a beneficial note. I will make the changes to all the other test files as well if this diagnostic representation goes well after mentor review.

For refactoring and restructuring the whole of the re-declaration would need some time I have been through it and would initiate another patch for that,In the concern of giving or having just one diagnostic for getting all cases of re-declaration would also need multiple conditional or switch statements inside our function block.At present we have the same with conditional statements taking care of each linkage but it has multiple permutations of diagnostic messages which is nice but can be improved.GCC differs only for this case of extern linkage which can be better/precise in clang where as others seem to be more precise in clang than latter as I worked out with good number of test cases regarding .


Repository:
  rG LLVM Github Monorepo

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

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;Please pick exactly one (static or extern)}}
+}
\ 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
@@ -5798,11 +5798,13 @@
   "redeclaring non-static %0 as static is a Microsoft extension">,
   InGroup<MicrosoftRedeclareStatic>;
 def err_non_static_static : Error<
-  "non-static declaration of %0 follows static declaration">;
+  "non-static declaration of %0 follows static declaration;Please pick exactly one (static or non-static)">;
+def err_extern_static : Error<
+  "extern declaration of %0 follows static declaration;Please pick exactly one (static or extern)">;
 def err_extern_non_extern : Error<
-  "extern declaration of %0 follows non-extern declaration">;
+  "extern declaration of %0 follows non-extern declaration;Please pick exactly one (non-extern or extern)">;
 def err_non_extern_extern : Error<
-  "non-extern declaration of %0 follows extern declaration">;
+  "non-extern declaration of %0 follows extern declaration;Please pick exactly one (extern or non-extern)">;
 def err_non_thread_thread : Error<
   "non-thread-local declaration of %0 follows thread-local declaration">;
 def err_thread_non_thread : Error<


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


More information about the cfe-commits mailing list