[clang-tools-extra] [clang-tidy] Ignore internal-linkage functions in concurrency-mt-unsafe (PR #249248)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 08:13:26 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Rudransh Shekhar (rudy-07)

<details>
<summary>Changes</summary>

`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.

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


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/concurrency/MtUnsafeCheck.cpp (+3-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+5) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/concurrency/mt-unsafe-any.cpp (+18) 


``````````diff
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();
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/249248


More information about the cfe-commits mailing list