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 <mohamedatef1...@gmail.com> 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<DiagGroup<"retained-language-linkage">>; +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