[clang-tools-extra] 2712d71 - [clang-tidy] Fix assert failure in modernize-use-std-format when args are in macros (#176684)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 20 21:16:55 PST 2026
Author: mitchell
Date: 2026-01-21T13:16:51+08:00
New Revision: 2712d71cc3d8a4d69fd9eef001768f350549ec47
URL: https://github.com/llvm/llvm-project/commit/2712d71cc3d8a4d69fd9eef001768f350549ec47
DIFF: https://github.com/llvm/llvm-project/commit/2712d71cc3d8a4d69fd9eef001768f350549ec47.diff
LOG: [clang-tidy] Fix assert failure in modernize-use-std-format when args are in macros (#176684)
Part of [#175183](https://github.com/llvm/llvm-project/issues/175183)
Added:
Modified:
clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
index cbf852ea7afc3..4feeaa18740d7 100644
--- a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
+++ b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
@@ -800,10 +800,12 @@ void FormatStringConverter::applyFixes(DiagnosticBuilder &Diag,
}
for (const auto &[ArgIndex, Replacement] : ArgFixes) {
- const SourceLocation AfterOtherSide =
+ const std::optional<Token> NextToken =
utils::lexer::findNextTokenSkippingComments(Args[ArgIndex]->getEndLoc(),
- SM, LangOpts)
- ->getLocation();
+ SM, LangOpts);
+ if (!NextToken)
+ continue;
+ const SourceLocation AfterOtherSide = NextToken->getLocation();
Diag << FixItHint::CreateInsertion(Args[ArgIndex]->getBeginLoc(),
Replacement, true)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 52a893ae62494..cfd7d277db29c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -114,6 +114,10 @@ Changes in existing checks
- Added support for analyzing function parameters with the `AnalyzeParameters`
option.
+- 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.
+
- Improved :doc:`modernize-use-using
<clang-tidy/checks/modernize/use-using>` check by avoiding the generation
of invalid code for function types with redundant parentheses.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp
index 6a6cb9857fff1..30a8c54f9f125 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp
@@ -26,6 +26,8 @@ struct iterator {
T &operator*();
};
+enum E { E1 };
+
std::string StrFormat_simple() {
return absl::StrFormat("Hello");
// CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
@@ -177,4 +179,10 @@ void StrFormat_macros() {
#define SURROUND_FORMAT(x) "!" x
auto s15 = absl::StrFormat(SURROUND_FORMAT("Hello %d"), 4443);
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: unable to use 'std::format' instead of 'StrFormat' because format string contains unreplaceable macro 'SURROUND_FORMAT' [modernize-use-std-format]
+
+ // Ensure that we don't crash if the call is within a macro.
+#define WRAP_IN_MACRO(x) x
+ WRAP_IN_MACRO(absl::StrFormat("Hello %d", E1));
+ // CHECK-MESSAGES: [[@LINE-1]]:17: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
+ // CHECK-FIXES: WRAP_IN_MACRO(std::format("Hello {}", E1));
}
More information about the cfe-commits
mailing list