[clang-tools-extra] 9708947 - [clang-tidy] Skip user headers named like C headers in `modernize-deprecated-headers` (#195507)

via cfe-commits cfe-commits at lists.llvm.org
Sun May 3 21:59:24 PDT 2026


Author: Zeyi Xu
Date: 2026-05-04T12:59:20+08:00
New Revision: 9708947197e490d62bae590ec7ce04a2b5c903d6

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

LOG: [clang-tidy] Skip user headers named like C headers in `modernize-deprecated-headers` (#195507)

Previously `modernize-deprecated-headers` would match on any header with
the same name as standard library headers. This commit fixes the problem
by checking whether the include resolves to a system header.

Closes #45991

---------

Co-authored-by: Victor Chernyakin <chernyakin.victor.j at outlook.com>

Added: 
    clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/deprecated-headers/user/assert.h
    clang-tools-extra/test/clang-tidy/checkers/modernize/deprecated-headers-user.cpp

Modified: 
    clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
    clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
    clang-tools-extra/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
index eff7c2f36d49d..88c411fcc6633 100644
--- a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
@@ -175,6 +175,10 @@ void IncludeModernizePPCallbacks::InclusionDirective(
   if (SM.isInSystemHeader(HashLoc))
     return;
 
+  // Skip headers that happen to use the same name as a standard library header.
+  if (!File || !SrcMgr::isSystem(FileType))
+    return;
+
   // FIXME: Take care of library symbols from the global namespace.
   //
   // Reasonable options for the check:

diff  --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 81df26f979ac3..861f3a7973a5a 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -318,12 +318,13 @@ TEST(DiagnosticsTest, ClangTidy) {
     }
   )cpp");
   auto TU = TestTU::withCode(Test.code());
-  TU.HeaderFilename = "assert.h"; // Suppress "not found" error.
+  TU.AdditionalFiles["system/assert.h"] = ""; // Suppress "not found" error.
   TU.ClangTidyProvider = addTidyChecks("bugprone-sizeof-expression,"
                                        "bugprone-macro-repeated-side-effects,"
                                        "modernize-deprecated-headers,"
                                        "modernize-use-trailing-return-type,"
                                        "misc-no-recursion");
+  TU.ExtraArgs.push_back("-isystem" + testPath("system"));
   TU.ExtraArgs.push_back("-Wno-unsequenced");
   EXPECT_THAT(
       TU.build().getDiagnostics(),

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d4b81d0479e89..9d35533377b34 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -408,6 +408,11 @@ Changes in existing checks
   Because it only sees one file at a time, the check can't be sure
   such entities aren't referenced in any other files of that module.
 
+- Improved :doc:`modernize-deprecated-headers
+  <clang-tidy/checks/modernize/deprecated-headers>` check by avoiding false
+  positives on project headers that use the same name as a standard library
+  header.
+
 - Improved :doc:`modernize-pass-by-value
   <clang-tidy/checks/modernize/pass-by-value>` check by adding `IgnoreMacros`
   option to suppress warnings in macros.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/deprecated-headers/user/assert.h b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/deprecated-headers/user/assert.h
new file mode 100644
index 0000000000000..1038e93a26f11
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/deprecated-headers/user/assert.h
@@ -0,0 +1 @@
+#define USER_ASSERT_H 1

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/deprecated-headers-user.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/deprecated-headers-user.cpp
new file mode 100644
index 0000000000000..ff24ffdd246d3
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/deprecated-headers-user.cpp
@@ -0,0 +1,9 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s modernize-deprecated-headers %t -- -extra-arg-before=-iquote%S/Inputs/deprecated-headers/user -extra-arg-before=-isystem%S/Inputs/deprecated-headers
+
+#include "assert.h"
+
+#include <assert.h>
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: inclusion of deprecated C++ header 'assert.h'; consider using 'cassert' instead [modernize-deprecated-headers]
+// CHECK-FIXES: #include <cassert>
+
+int user_header = USER_ASSERT_H;


        


More information about the cfe-commits mailing list