thread_local cannot create tentative definitions
The following program demonstrates the issue:
thread_local int x,x;
int main(){}
Clang incorrectly allows this, it should be rejected which GCC correctly does:
If the declaration of an identifier for an object has file scope and an initializer, or has file scope and storage-class specifier thread_local, the declaration is an external definition for the identifier.
Section 6.9.3 "External object definitions" Paragraph 1 ISO/IEC 9899:2024
This wording has been adjusted by Issue 1009 but that does not matter for this case.
@llvm/issue-subscribers-clang-frontend
Author: Jay Ghiron (Halalaluyafail3)
Section 6.9.3 "External object definitions" Paragraph 1 ISO/IEC 9899:2024
This wording has been adjusted by Issue 1009 but that does not matter for this case.
GB-126 of N3108 also made it clear that this should not be a tentative definition.
CC @AaronBallman
So I was looking at this bug, and when scanning the related code, I figured that adding the check getTLSKind() == TLS_None to this part if (!C.getLangOpts().CPlusPlus && isFileVarDecl() && getTLSKind() == TLS_None) return TentativeDefinition; should stop file-scope thread_local declarations from being treated as TentativeDefinition. Since tentative definitions are allowed to repeat, that's currently why the duplicate thread_local declaration goes unflagged; excluding TLS vars from that classification should let the redefinition get caught properly. I just wanted to ask if this is the correct way to tackle this issue before I create a pr.
@Halalaluyafail3
So I was looking at this bug, and when scanning the related code, I figured that adding the check
getTLSKind() == TLS_Noneto this partif (!C.getLangOpts().CPlusPlus && isFileVarDecl() && getTLSKind() == TLS_None) return TentativeDefinition;should stop file-scope thread_local declarations from being treated asTentativeDefinition. Since tentative definitions are allowed to repeat, that's currently why the duplicatethread_localdeclaration goes unflagged; excluding TLS vars from that classification should let the redefinition get caught properly. I just wanted to ask if this is the correct way to tackle this issue before I create a pr. @Halalaluyafail3
I am not familiar with LLVM internals but this is what defines tentative definitions:
- it declares an object,
- it has file scope,
- it has no initializer,
- it does not have the storage-class specifier extern, and
- it does not have the storage-class specifier thread_local.