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

Part of [#175183](https://github.com/llvm/llvm-project/issues/175183)

>From e3d84eb1407b760b1e02e2b6335df9b92886b013 Mon Sep 17 00:00:00 2001
From: mtx <[email protected]>
Date: Mon, 12 Jan 2026 03:11:46 +0800
Subject: [PATCH] [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));
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to