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<


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<
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to