[clang-tools-extra] [clang-tidy] Add missing #include insertion in macros for modernize-use-std-format (PR #188247)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 24 06:38:55 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Zeyi Xu (zeyi2)

<details>
<summary>Changes</summary>

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

Part of #<!-- -->175183

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


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp (+2-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+6-2) 
- (added) clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp (+21) 


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

``````````

</details>


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


More information about the cfe-commits mailing list