[clang-tools-extra] [clang-tidy] Ignore internal-linkage functions in concurrency-mt-unsafe (PR #249248)
Rudransh Shekhar via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 5 08:12:37 PST 2026
https://github.com/rudy-07 created https://github.com/llvm/llvm-project/pull/249248
`concurrency-mt-unsafe` matches unsafe C/POSIX/glibc functions using `hasAnyName` with `::`-prefixed names. becuz `HasNameMatcher` skips anonymous namespaces , functions defined inside an anonymous namespace with the same name as a listed function (like `namespace { void sleep() {} }`) were flagged as non thread safe. global `static` functions with internal linkage had the same issue.
exclude functions matched by `isInAnonymousNamespace()` or `isStaticStorageClass()` in `MtUnsafeCheck::registerMatchers`, and add regression tests in `mt-unsafe-any.cpp`, and document the fix in `ReleaseNotes.md`.
fixes #247030.
>From 9dd761fa31ad206d726d4b65691e8eaf84a7996d Mon Sep 17 00:00:00 2001
From: rudy-07 <kanhaiffco2007 at gmail.com>
Date: Wed, 2 Dec 2026 01:23:31 +0530
Subject: [PATCH] [clang-tidy] Ignore internal-linkage functions in
concurrency-mt-unsafe
---
.../clang-tidy/concurrency/MtUnsafeCheck.cpp | 4 +++-
clang-tools-extra/docs/ReleaseNotes.md | 5 +++++
.../checkers/concurrency/mt-unsafe-any.cpp | 18 ++++++++++++++++++
3 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/concurrency/MtUnsafeCheck.cpp b/clang-tools-extra/clang-tidy/concurrency/MtUnsafeCheck.cpp
index 1a14f7219d324c..1075b7ac3c9f4e 100644
--- a/clang-tools-extra/clang-tidy/concurrency/MtUnsafeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/concurrency/MtUnsafeCheck.cpp
@@ -296,7 +296,9 @@ void MtUnsafeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
void MtUnsafeCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
- callExpr(callee(functionDecl(hasAnyMtUnsafeNames(FuncSet))))
+ callExpr(callee(functionDecl(hasAnyMtUnsafeNames(FuncSet),
+ unless(anyOf(isInAnonymousNamespace(),
+ isStaticStorageClass())))))
.bind("mt-unsafe"),
this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 2549bb564cdd57..d6519e1e45d866 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -271,6 +271,11 @@ infrastructure are described first, followed by tool-specific sections.
available. This leads to faster analysis, but the trade-off is more false
positives (when has value checks involve correlated variables or ORs).
+- Fixed false positives in {doc}`concurrency-mt-unsafe
+ <clang-tidy/checks/concurrency/mt-unsafe>` when calling functions with
+ internal linkage (such as those in anonymous namespaces or declared `static`)
+ that share a name with an unsafe library function.
+
- Improved {doc}`cppcoreguidelines-missing-std-forward
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by diagnosing
unforwarded `auto&&` parameters in C++20 abbreviated function templates.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/concurrency/mt-unsafe-any.cpp b/clang-tools-extra/test/clang-tidy/checkers/concurrency/mt-unsafe-any.cpp
index 6e3934836aa7bb..734596a8764b32 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/concurrency/mt-unsafe-any.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/concurrency/mt-unsafe-any.cpp
@@ -22,3 +22,21 @@ void foo() {
dirname(nullptr);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function is not thread safe [concurrency-mt-unsafe]
}
+
+namespace {
+void sleep() {}
+void gmtime() {}
+} // namespace
+
+namespace custom {
+void sleep() {}
+} // namespace custom
+
+static void dirname() {}
+
+void test_non_libc_functions() {
+ sleep();
+ gmtime();
+ custom::sleep();
+ dirname();
+}
More information about the cfe-commits
mailing list