[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 06:38:20 PDT 2026


https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/188247

Added missing ``#include`` insertion when the format function call appears as an argument to a macro.

Part of #175183

>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] [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));
+}



More information about the cfe-commits mailing list