[clang-tools-extra] [clang-tidy] Add missing #include insertion in macros for modernize-use-std-format (PR #188247)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 24 07:22:08 PDT 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/188247
>From 55eee6c0fffc612bf59108dcd8fd7c4679457a4e Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Tue, 24 Mar 2026 21:32:30 +0800
Subject: [PATCH 1/3] [clang-tidy] Add missing #include insertion in macros for
modernize-use-std-format
---
.../clang-tidy/modernize/UseStdPrintCheck.cpp | 3 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 8 +++++--
.../modernize/use-std-format-macro.cpp | 21 +++++++++++++++++++
3 files changed, 29 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-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..a22577912c376 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -310,8 +310,12 @@ Changes in existing checks
special member function.
- Improved :doc:`modernize-use-std-format
- <clang-tidy/checks/modernize/use-std-format>` check by fixing a crash
- when an argument is part of a macro expansion.
+ <clang-tidy/checks/modernize/use-std-format>` check:
+
+ - Fixed a crash when an argument is part of a macro expansion.
+
+ - Added 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
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
new file mode 100644
index 0000000000000..020abaca62aef
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
@@ -0,0 +1,21 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-std-format %t -- \
+// RUN: -config="{CheckOptions: { \
+// RUN: modernize-use-std-format.StrFormatLikeFunctions: 'String::Printf', \
+// RUN: modernize-use-std-format.ReplacementFormatFunction: 'fmt::format', \
+// RUN: modernize-use-std-format.FormatHeader: '<fmt/format.h>' \
+// RUN: }}"
+
+#include <string>
+// CHECK-FIXES: #include <fmt/format.h>
+
+namespace String {
+extern std::string Printf(const char *format, ...);
+} // namespace String
+
+#define WRAP_MSG(msg) msg
+
+std::string macro_argument_include(int n) {
+ return WRAP_MSG(String::Printf("value %d", n));
+ // CHECK-MESSAGES: [[@LINE-1]]:19: warning: use 'fmt::format' instead of 'Printf' [modernize-use-std-format]
+ // CHECK-FIXES: return WRAP_MSG(fmt::format("value {}", n));
+}
>From da9c4174d84603a0664b1395f781c3d920d263e3 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Tue, 24 Mar 2026 21:47:30 +0800
Subject: [PATCH 2/3] fixup: commit the right file
---
clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.cpp | 4 ++--
clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp | 3 +--
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.cpp
index 101780ad0b186..9864b7b7627ac 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.cpp
@@ -99,8 +99,8 @@ void UseStdFormatCheck::check(const MatchFinder::MatchResult &Result) {
if (MaybeHeaderToInclude)
Diag << IncludeInserter.createIncludeInsertion(
- Result.Context->getSourceManager().getFileID(
- StrFormatCall->getBeginLoc()),
+ Result.SourceManager->getFileID(Result.SourceManager->getExpansionLoc(
+ StrFormatCall->getBeginLoc())),
*MaybeHeaderToInclude);
}
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
index 6d18ebeae6ce4..0f612471497a7 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
@@ -158,8 +158,7 @@ void UseStdPrintCheck::check(const MatchFinder::MatchResult &Result) {
if (MaybeHeaderToInclude)
Diag << IncludeInserter.createIncludeInsertion(
- Result.SourceManager->getFileID(
- Result.SourceManager->getExpansionLoc(PrintfCall->getBeginLoc())),
+ Result.Context->getSourceManager().getFileID(PrintfCall->getBeginLoc()),
*MaybeHeaderToInclude);
}
>From d541d1fee21f38cb00eaf45d23646e16ff5461f9 Mon Sep 17 00:00:00 2001
From: Zeyi Xu <mitchell.xu2 at gmail.com>
Date: Tue, 24 Mar 2026 22:21:56 +0800
Subject: [PATCH 3/3] Update
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
Co-authored-by: Victor Chernyakin <chernyakin.victor.j at outlook.com>
---
.../test/clang-tidy/checkers/modernize/use-std-format-macro.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
index 020abaca62aef..f8d76d0ad33ba 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
@@ -9,7 +9,7 @@
// CHECK-FIXES: #include <fmt/format.h>
namespace String {
-extern std::string Printf(const char *format, ...);
+std::string Printf(const char *format, ...);
} // namespace String
#define WRAP_MSG(msg) msg
More information about the cfe-commits
mailing list