Skip to content

Commit d6dcd98

Browse files
authored
[clang-tidy] Fix thread_local false positives in misc-use-internal-linkage check (#132573)
Based on C++ standard (see issue #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 #131679.
1 parent ecdbd26 commit d6dcd98

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
130130
isMain())))
131131
.bind("fn"),
132132
this);
133-
Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this);
133+
Finder->addMatcher(
134+
varDecl(Common, hasGlobalStorage(), unless(hasThreadStorageDuration()))
135+
.bind("var"),
136+
this);
134137
}
135138

136139
static constexpr StringRef Message =

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ Changes in existing checks
159159

160160
- Improved :doc:`misc-use-internal-linkage
161161
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
162-
for function or variable in header file which contains macro expansion.
162+
for function or variable in header file which contains macro expansion and
163+
excluding variables with ``thread_local`` storage class specifier from being
164+
matched.
163165

164166
- Improved :doc:`modernize-use-default-member-init
165167
<clang-tidy/checks/modernize/use-default-member-init>` check by matching

clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern int global_extern;
3131

3232
static int global_static;
3333

34+
thread_local int global_thread_local;
35+
3436
namespace {
3537
static int global_anonymous_ns;
3638
namespace NS {
@@ -41,6 +43,7 @@ static int global_anonymous_ns;
4143
static void f(int para) {
4244
int local;
4345
static int local_static;
46+
thread_local int local_thread_local;
4447
}
4548

4649
struct S {

0 commit comments

Comments
 (0)