https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/176684
>From e3d84eb1407b760b1e02e2b6335df9b92886b013 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Mon, 12 Jan 2026 03:11:46 +0800 Subject: [PATCH 1/2] [clang-tidy] Fix crash in modernize-use-std-format when args are in macros --- .../clang-tidy/utils/FormatStringConverter.cpp | 9 +++++---- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++- .../clang-tidy/checkers/modernize/use-std-format.cpp | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp index cbf852ea7afc3..c7c711fd1add4 100644 --- a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp +++ b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp @@ -800,10 +800,11 @@ void FormatStringConverter::applyFixes(DiagnosticBuilder &Diag, } for (const auto &[ArgIndex, Replacement] : ArgFixes) { - const SourceLocation AfterOtherSide = - utils::lexer::findNextTokenSkippingComments(Args[ArgIndex]->getEndLoc(), - SM, LangOpts) - ->getLocation(); + const auto NextToken = utils::lexer::findNextTokenSkippingComments( + Args[ArgIndex]->getEndLoc(), 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 391c3a6b3db79..7c0f361eae586 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -592,7 +592,8 @@ Changes in existing checks - Improved :doc:`modernize-use-std-format <clang-tidy/checks/modernize/use-std-format>` check to correctly match when the format string is converted to a different type by an implicit - constructor call. + constructor call, and fixed a crash when an argument is part of a macro + expansion. - Improved :doc:`modernize-use-std-print <clang-tidy/checks/modernize/use-std-print>` check to correctly match 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)); } >From df32097b723dd309b8ec07c3adcb7159e22aae8d Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Mon, 19 Jan 2026 10:48:48 +0800 Subject: [PATCH 2/2] Fix docu indention --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 9a3b501f89d9c..a25a8f597189a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -110,8 +110,8 @@ Changes in existing checks 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. + <clang-tidy/checks/modernize/use-std-format>` check by fixing a crash + when an argument is part of a macro expansion. - Improved :doc:`performance-move-const-arg <clang-tidy/checks/performance/move-const-arg>` check by avoiding false _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
