llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: mitchell (zeyi2)

<details>
<summary>Changes</summary>

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

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


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp 
(+5-4) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp (+8) 


``````````diff
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 94a11b1acb73a..9a3b501f89d9c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -109,6 +109,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:`performance-move-const-arg
   <clang-tidy/checks/performance/move-const-arg>` check by avoiding false
   positives on trivially copyable types with a non-public copy constructor.
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));
 }

``````````

</details>


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

Reply via email to