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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 08:40:28 PST 2024


================
@@ -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


More information about the cfe-commits mailing list