llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

thread_local cannot create tentative definitions

Open Halalaluyafail3 opened this issue 3 months ago • 5 comments

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.

Halalaluyafail3 avatar Aug 20 '26 14:08 Halalaluyafail3

@llvm/issue-subscribers-clang-frontend

Author: Jay Ghiron (Halalaluyafail3)

The following program demonstrates the issue: ```c 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.

GB-126 of N3108 also made it clear that this should not be a tentative definition.

Halalaluyafail3 avatar Aug 20 '26 16:08 Halalaluyafail3

CC @AaronBallman

shafik avatar Aug 20 '26 17:08 shafik

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

AhmedKamel10 avatar Aug 29 '26 10:08 AhmedKamel10

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

I am not familiar with LLVM internals but this is what defines tentative definitions:

  1. it declares an object,
  2. it has file scope,
  3. it has no initializer,
  4. it does not have the storage-class specifier extern, and
  5. it does not have the storage-class specifier thread_local.

Halalaluyafail3 avatar Aug 29 '26 13:08 Halalaluyafail3