[clang-tools-extra] [clang-tidy] Fix `thread_local` false positives in `misc-use-internal-linkage` check (PR #132573)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 22 17:27:36 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Baranov Victor (vbvictor)
<details>
<summary>Changes</summary>
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`. 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](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.
---
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
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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/132573
More information about the cfe-commits
mailing list