[clang-tools-extra] [clang-tidy] Add missing #include insertion in macros for modernize-use-std-print (PR #188394)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 26 22:12:35 PDT 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/188394
>From b6e605689025baec3a208f44b4375aba89fde30c Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Wed, 25 Mar 2026 09:42:02 +0800
Subject: [PATCH 1/2] [clang-tidy] Add missing #include insertion in macros for
modernize-use-std-print
---
.../clang-tidy/modernize/UseStdPrintCheck.cpp | 3 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
.../checkers/modernize/use-std-print-macro.cpp | 13 +++++++++++++
3 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
index 0f612471497a7..6d18ebeae6ce4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
@@ -158,7 +158,8 @@ void UseStdPrintCheck::check(const MatchFinder::MatchResult &Result) {
if (MaybeHeaderToInclude)
Diag << IncludeInserter.createIncludeInsertion(
- Result.Context->getSourceManager().getFileID(PrintfCall->getBeginLoc()),
+ Result.SourceManager->getFileID(
+ Result.SourceManager->getExpansionLoc(PrintfCall->getBeginLoc())),
*MaybeHeaderToInclude);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index daf635bf625e7..b2e1dc7e43dae 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -313,6 +313,11 @@ Changes in existing checks
<clang-tidy/checks/modernize/use-std-format>` check by fixing a crash
when an argument is part of a macro expansion.
+- Improved :doc:`modernize-use-std-print
+ <clang-tidy/checks/modernize/use-std-print>` check by adding missing
+ ``#include`` insertion when the format function call appears as an
+ argument to a macro.
+
- Improved :doc:`modernize-use-trailing-return-type
<clang-tidy/checks/modernize/use-trailing-return-type>` check by fixing
spurious ``missing '(' after '__has_feature'`` errors caused by builtin
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
new file mode 100644
index 0000000000000..66364d81c25d8
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
@@ -0,0 +1,13 @@
+// RUN: %check_clang_tidy -std=c++23-or-later %s modernize-use-std-print %t -- \
+// RUN: -- -fexceptions
+
+#include <cstdio>
+// CHECK-FIXES: #include <print>
+
+#define WRAP_MSG(msg) msg
+
+void macro_argument_include(int n) {
+ WRAP_MSG(printf("value %d", n));
+ // CHECK-MESSAGES: [[@LINE-1]]:12: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]
+ // CHECK-FIXES: WRAP_MSG(std::print("value {}", n));
+}
>From c14b82da3c5dfdb8d61a43f0c161ca0d0176217a Mon Sep 17 00:00:00 2001
From: Zeyi Xu <mitchell.xu2 at gmail.com>
Date: Fri, 27 Mar 2026 13:12:27 +0800
Subject: [PATCH 2/2] Update
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
Co-authored-by: Victor Chernyakin <chernyakin.victor.j at outlook.com>
---
.../test/clang-tidy/checkers/modernize/use-std-print-macro.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
index 66364d81c25d8..387f6ccfaf427 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-macro.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++23-or-later %s modernize-use-std-print %t -- \
-// RUN: -- -fexceptions
+// RUN: %check_clang_tidy -std=c++23-or-later %s modernize-use-std-print %t
#include <cstdio>
// CHECK-FIXES: #include <print>
More information about the cfe-commits
mailing list