Skip to content

Conversation

@vbvictor
Copy link
Contributor

@vbvictor vbvictor commented Mar 23, 2025

Based on C++ standard (see issue #131679) and StackOverflow thread_local variables are implicitly static so we should not suggest adding static on a thread_local variables. I'd appreciate if someone else will confirm this too because reading standard is tricky.

However, many people still use static and thread_local together: github code-search. Maybe disabling warnings on thread_local should be made as a flag? WDYT?

Closes #131679.

@llvmbot
Copy link
Member

llvmbot commented Mar 23, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

Changes

Based on C++ standard (see issue #131679) and StackOverflow thread_local variables are implicitly static. I'd appreciate it if someone else will confirm this too because reading standard is tricky.

However, many people still use static and thread_local together: github code-search. Maybe disabling warnings on thread_local should be made as a flag? WDYT?

Closes #131679.


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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp (+4-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp (+3)
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index a1a20c0782230..e2071b806b125 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -130,7 +130,10 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
                                 isMain())))
           .bind("fn"),
       this);
-  Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this);
+  Finder->addMatcher(
+      varDecl(Common, hasGlobalStorage(), unless(hasThreadStorageDuration()))
+          .bind("var"),
+      this);
 }
 
 static constexpr StringRef Message =
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 72aa05eb4dcd1..816e3ea579817 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -164,7 +164,9 @@ Changes in existing checks
 
 - Improved :doc:`misc-use-internal-linkage
   <clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
-  for function or variable in header file which contains macro expansion.
+  for function or variable in header file which contains macro expansion and
+  excluding variables with ``thread_local`` storage class specifier from being
+  matched.
 
 - Improved :doc:`modernize-use-default-member-init
   <clang-tidy/checks/modernize/use-default-member-init>` check by matching
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
index 901272e40b8f2..3da05c71dd94f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
@@ -31,6 +31,8 @@ extern int global_extern;
 
 static int global_static;
 
+thread_local int global_thread_local;
+
 namespace {
 static int global_anonymous_ns;
 namespace NS {
@@ -41,6 +43,7 @@ static int global_anonymous_ns;
 static void f(int para) {
   int local;
   static int local_static;
+  thread_local int local_thread_local;
 }
 
 struct S {

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vbvictor
Copy link
Contributor Author

@PiotrZSL, could you please merge this PR when you have time, thank you.

@HerrCai0907 HerrCai0907 merged commit d6dcd98 into llvm:main Mar 28, 2025
15 checks passed
@vbvictor vbvictor deleted the misc-use-internal-linkage-issue131679 branch June 22, 2025 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-tidy] misc-use-internal-linkage false positive with thread_local block-level variable

4 participants