[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-28 Thread via cfe-commits


@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }

elhewaty wrote:

@AaronBallman
> If the prior declaration specifies internal or external linkage, the linkage 
> of the identifier at the later declaration is the same as the linkage 
> specified at the prior declaration.

Then when should the declaration be an issue (https://godbolt.org/z/roE9xh76d)?
[Note: the diagnostic of clang and GCC is different.]
If the external linkage follows the previous linkage, when could the error 
happen?




https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-02-05 Thread Aaron Ballman via cfe-commits


@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }

AaronBallman wrote:

I think http://eel.is/c++draft/dcl.stc#example-1 has a lot of good examples 
that are worth looking at.

We do a lot of this checking already, starting from 
https://github.com/llvm/llvm-project/blob/cb8d83a77c25e529f58eba17bb1ec76069a04e90/clang/lib/Sema/SemaDecl.cpp#L4643
 and the issue might relate to the FIXME at 
https://github.com/llvm/llvm-project/blob/cb8d83a77c25e529f58eba17bb1ec76069a04e90/clang/lib/Sema/SemaDecl.cpp#L4696

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/elhewaty created 
https://github.com/llvm/llvm-project/pull/78064

Fixes: https://github.com/llvm/llvm-project/issues/54215


>From da9e05f5574bae712a14a3b467d5b3944418b421 Mon Sep 17 00:00:00 2001
From: Mohamed Atef 
Date: Sat, 13 Jan 2024 22:19:15 +0200
Subject: [PATCH] [clang] emit an error when the same identifier appears with
 both internal and external linkage in a translation unit

---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 ++
 clang/lib/Sema/SemaDecl.cpp   |  5 +
 clang/test/Sema/private-extern.c  | 21 ---
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..9990bb72a8817c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage() ) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..7831c1fcfe8bc2 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -1,3 +1,4 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 4
 // RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern %s
 // RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern -fmodules %s
 
@@ -5,10 +6,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +84,17 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}
+
+

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: None (elhewaty)


Changes

Fixes: https://github.com/llvm/llvm-project/issues/54215


---
Full diff: https://github.com/llvm/llvm-project/pull/78064.diff


3 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+5) 
- (modified) clang/test/Sema/private-extern.c (+18-3) 


``diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..9990bb72a8817c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage() ) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..7831c1fcfe8bc2 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -1,3 +1,4 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 4
 // RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern %s
 // RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern -fmodules %s
 
@@ -5,10 +6,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +84,17 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}
+
+

``




https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 66786a79d6f622012879e94a92838449bf175a71 
da9e05f5574bae712a14a3b467d5b3944418b421 -- clang/lib/Sema/SemaDecl.cpp 
clang/test/Sema/private-extern.c
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 9990bb72a8..6598a72b44 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,7 +4754,7 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
-  if (Old->getFormalLinkage() != New->getFormalLinkage() ) {
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
 Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
 return New->setInvalidDecl();
   }

``




https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/elhewaty updated 
https://github.com/llvm/llvm-project/pull/78064

>From 42029d55a4f9a08648eb83096d9e71977b3bf1b1 Mon Sep 17 00:00:00 2001
From: Mohamed Atef 
Date: Sat, 13 Jan 2024 22:19:15 +0200
Subject: [PATCH] [clang] emit an error when the same identifier appears with
 both internal and external linkage in a translation unit

---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 ++
 clang/lib/Sema/SemaDecl.cpp   |  5 +
 clang/test/Sema/private-extern.c  | 21 ---
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..6598a72b44c19b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..7831c1fcfe8bc2 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -1,3 +1,4 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 4
 // RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern %s
 // RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern -fmodules %s
 
@@ -5,10 +6,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +84,17 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}
+
+

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

elhewaty wrote:

@AaronBallman @cor3ntin 

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/elhewaty updated 
https://github.com/llvm/llvm-project/pull/78064

>From ac16dd9593a3febdecef7fe79167aff1cbf035a7 Mon Sep 17 00:00:00 2001
From: Mohamed Atef 
Date: Sat, 13 Jan 2024 22:19:15 +0200
Subject: [PATCH] [clang] emit an error when the same identifier appears with
 both internal and external linkage in a translation unit

---
 .../include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 clang/lib/Sema/SemaDecl.cpp|  5 +
 clang/test/Sema/private-extern.c   | 18 +++---
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..6598a72b44c19b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..e501d51eb04130 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -5,10 +5,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +83,15 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/elhewaty updated 
https://github.com/llvm/llvm-project/pull/78064

>From cdd9783e65315e296139478283732a935bfa87cf Mon Sep 17 00:00:00 2001
From: Mohamed Atef 
Date: Sat, 13 Jan 2024 22:19:15 +0200
Subject: [PATCH] [clang] emit an error when the same identifier appears with
 both internal and external linkage in a translation unit

---
 .../include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 clang/lib/Sema/SemaDecl.cpp|  5 +
 clang/test/Sema/private-extern.c   | 18 +++---
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..6598a72b44c19b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..e501d51eb04130 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -5,10 +5,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +83,15 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/cor3ntin edited 
https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

cor3ntin wrote:

This change needs a release note.
Please add an entry to `clang/docs/ReleaseNotes.rst` in the section the most 
adapted to the change (Bug fixes), and referencing any Github issue this change 
fixes. Thanks!

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/elhewaty updated 
https://github.com/llvm/llvm-project/pull/78064

>From 3426a3352beed37f805e0e01dbf9aa13cf30a6da Mon Sep 17 00:00:00 2001
From: Mohamed Atef 
Date: Sat, 13 Jan 2024 22:19:15 +0200
Subject: [PATCH] [clang] emit an error when the same identifier appears with
 both internal and external linkage in a translation unit

---
 clang/docs/ReleaseNotes.rst|  4 
 .../include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 clang/lib/Sema/SemaDecl.cpp|  5 +
 clang/test/Sema/private-extern.c   | 18 +++---
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3cbce1be159437..5c4c8f7456dcbd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -918,6 +918,10 @@ Bug Fixes to C++ Support
   (`#57410 `_) and
   (`#76604 `_)
 
+- Emit an error when the same identifier appears with both internal and
+  external linkage in a translation unit Fixes:
+  (`https://github.com/llvm/llvm-project/issues/54215`_)
+
 Bug Fixes to AST Handling
 ^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..6598a72b44c19b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..e501d51eb04130 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -5,10 +5,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +83,15 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

https://github.com/elhewaty updated 
https://github.com/llvm/llvm-project/pull/78064

>From ced1d271332a530960b8b68e8957448dd462350d Mon Sep 17 00:00:00 2001
From: Mohamed Atef 
Date: Sat, 13 Jan 2024 22:19:15 +0200
Subject: [PATCH] [clang] emit an error when the same identifier appears with
 both internal and external linkage in a translation unit

---
 clang/docs/ReleaseNotes.rst|  4 
 .../include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 clang/lib/Sema/SemaDecl.cpp|  5 +
 clang/test/Sema/private-extern.c   | 18 +++---
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3cbce1be159437..b558bdd2a91a2d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -918,6 +918,10 @@ Bug Fixes to C++ Support
   (`#57410 `_) and
   (`#76604 `_)
 
+- Emit an error when the same identifier appears with both internal and
+  external linkage in a translation unit Fixes:
+  (`#54215 `_)
+
 Bug Fixes to AST Handling
 ^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1a79892e40030a..12a95e38786abc 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5940,6 +5940,8 @@ def err_different_language_linkage : Error<
 def ext_retained_language_linkage : Extension<
   "friend function %0 retaining previous language linkage is an extension">,
   InGroup>;
+def err_multiple_linkage: Error<
+  "the same identifier %0 appears with both internal and external linkage">;
 def err_extern_c_global_conflict : Error<
   "declaration of %1 %select{with C language linkage|in global scope}0 "
   "conflicts with declaration %select{in global scope|with C language 
linkage}0">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e92fd104d78eb5..6598a72b44c19b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }
+
   if (New->isInline() && !Old->getMostRecentDecl()->isInline()) {
 if (VarDecl *Def = Old->getDefinition()) {
   // C++1z [dcl.fcn.spec]p4:
diff --git a/clang/test/Sema/private-extern.c b/clang/test/Sema/private-extern.c
index 7e7fb27416aa8d..e501d51eb04130 100644
--- a/clang/test/Sema/private-extern.c
+++ b/clang/test/Sema/private-extern.c
@@ -5,10 +5,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}
 
-static int g2; 
-__private_extern__ int g2;
+static int g2;
+__private_extern__ int g2; // expected-error{{the same identifier 'g2' appears 
with both internal and external linkage}}
 
 int g3; // expected-note{{previous definition}}
 static int g3; // expected-error{{static declaration of 'g3' follows 
non-static declaration}}
@@ -83,3 +83,15 @@ __private_extern__ int g19;
 int g19 = 0;
 
 __private_extern__ int g20 = 0;
+
+static int g21;
+extern int g21; // expected-error{{the same identifier 'g21' appears with both 
internal and external linkage}}
+
+static int g22;
+
+void f10(void) {
+  int g22;
+  {
+extern int g22; // expected-error{{the same identifier 'g22' appears with 
both internal and external linkage}}
+  }
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

elhewaty wrote:

@ cor3ntin done

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-13 Thread via cfe-commits

cor3ntin wrote:

@elhewaty Thanks. LGTM. I'll wait a few days for @AaronBallman to look it over 
before accepting but it looks like a great fix, thanks

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-14 Thread via cfe-commits

elhewaty wrote:

Hello @cor3ntin, can you please suggest `clang` issues that maybe suitable for 
a beginner, or if you have a TO-DO list I will be happy to work on it.
Thanks

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-15 Thread Mariya Podchishchaeva via cfe-commits

Fznamznon wrote:

Note there is 39 failing clang tests in the CI 
https://buildkite.com/llvm-project/clang-ci/builds/9797#018d04dc-7b2b-47fb-8025-123227f7deab
 . 

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-15 Thread via cfe-commits

elhewaty wrote:

@Fznamznon So should I update the test files manually?

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-16 Thread Mariya Podchishchaeva via cfe-commits

Fznamznon wrote:

> @Fznamznon So should I update the test files manually?

Yes, AFAIK there is no script to update the tests. Make sure the new errors 
seen in the tests are expected outcome of the patch though.

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-16 Thread Aaron Ballman via cfe-commits


@@ -918,6 +918,10 @@ Bug Fixes to C++ Support
   (`#57410 `_) and
   (`#76604 `_)
 
+- Emit an error when the same identifier appears with both internal and
+  external linkage in a translation unit Fixes:

AaronBallman wrote:

```suggestion
  external linkage in a translation unit. Fixes:
```
I think the release note should also be moved to be under `Bug Fixes in This 
Version` because it's not a C++-specific diagnostic.

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-16 Thread Aaron Ballman via cfe-commits


@@ -4754,6 +4754,11 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult 
&Previous) {
 return New->setInvalidDecl();
   }
 
+  if (Old->getFormalLinkage() != New->getFormalLinkage()) {
+Diag(New->getLocation(), diag::err_multiple_linkage) << New->getDeclName();
+return New->setInvalidDecl();
+  }

AaronBallman wrote:

I don't think this logic is correct. See C23 6.2.2 (especially p4): 

> For an identifier declared with the storage-class specifier extern in a scope 
> in which a prior declaration of that identifier is visible, if the prior 
> declaration specifies internal or external linkage, the linkage of the 
> identifier at the later declaration is the same as the linkage specified at 
> the prior declaration. 

So a mismatch between linkages isn't always an issue to be diagnosed: 
https://godbolt.org/z/rx7rGhv7c (notice how the local variable `x` is not an 
issue despite having different linkage from the `extern` declaration: 
https://godbolt.org/z/Yjqo4eY9M) This is possibly why you're getting as many CI 
failures as you're seeing.

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] emit an error when the same identifier appears with both internal and external linkage in a translation unit (PR #78064)

2024-01-16 Thread Aaron Ballman via cfe-commits


@@ -5,10 +5,10 @@ static int g0; // expected-note{{previous definition}}
 int g0; // expected-error{{non-static declaration of 'g0' follows static 
declaration}}
 
 static int g1;
-extern int g1;
+extern int g1; // expected-error{{the same identifier 'g1' appears with both 
internal and external linkage}}

AaronBallman wrote:

This is an example of code that should be fine per 6.2.2.p4

https://github.com/llvm/llvm-project/pull/78064
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits