[clang-tools-extra] dd9885c - [clang-tidy] Add missing #include insertion in macros for modernize-use-std-format (#188247)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 24 16:52:51 PDT 2026
Author: Zeyi Xu
Date: 2026-03-25T07:52:45+08:00
New Revision: dd9885c0f34fdf64e0de2b07b94ab141274a82e2
URL: https://github.com/llvm/llvm-project/commit/dd9885c0f34fdf64e0de2b07b94ab141274a82e2
DIFF: https://github.com/llvm/llvm-project/commit/dd9885c0f34fdf64e0de2b07b94ab141274a82e2.diff
LOG: [clang-tidy] Add missing #include insertion in macros for modernize-use-std-format (#188247)
Added missing ``#include`` insertion when the format function call
appears as an argument to a macro.
Part of #175183
---------
Co-authored-by: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Added:
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-macro.cpp
Modified:
clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
Removed:
################################################################################
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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 49901f8a706c6..f8550e72dcc85 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -319,8 +319,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..f8d76d0ad33ba
--- /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 {
+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