[clang-tools-extra] d6dcd98 - [clang-tidy] Fix `thread_local` false positives in `misc-use-internal-linkage` check (#132573)

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 28 06:34:01 PDT 2025


Author: Baranov Victor
Date: 2025-03-28T21:33:58+08:00
New Revision: d6dcd985c03bec4b77872be00a81d92454fedc32

URL: https://github.com/llvm/llvm-project/commit/d6dcd985c03bec4b77872be00a81d92454fedc32
DIFF: https://github.com/llvm/llvm-project/commit/d6dcd985c03bec4b77872be00a81d92454fedc32.diff

LOG: [clang-tidy] Fix `thread_local` false positives in `misc-use-internal-linkage` check  (#132573)

Based on C++ standard (see issue
https://github.com/llvm/llvm-project/issues/131679) and
[StackOverflow](https://stackoverflow.com/questions/22794382/are-c11-thread-local-variables-automatically-static)
`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](https://github.com/search?type=code&q=%22static+thread_local%22+language%3AC%2B%2B).
Maybe disabling warnings on `thread_local` should be made as a flag?
WDYT?

Closes https://github.com/llvm/llvm-project/issues/131679.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp

Removed: 
    


################################################################################
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 7bbf2190ee262..6cb8d572d3a78 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -159,7 +159,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 {


        


More information about the cfe-commits mailing list