[llvm] [Support] Do not ignore unterminated open { in formatv (PR #104688)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 18 11:39:49 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Rahul Joshi (jurahul)

<details>
<summary>Changes</summary>

- When an unterminated open { is detected in the format string, instead of asserting and ignoring the error, replace that string with another to indicate the error, and remove the assert as well.
- This will make the error behvaior indentical between assert and release builds and also help catch the error more convienent (as several uses of this function are in TableGen and its if often built in release mode even in debug builds, using

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


1 Files Affected:

- (modified) llvm/lib/Support/FormatVariadic.cpp (+8-5) 


``````````diff
diff --git a/llvm/lib/Support/FormatVariadic.cpp b/llvm/lib/Support/FormatVariadic.cpp
index 3c07a80a00ae6d..f23a948e6c52bd 100644
--- a/llvm/lib/Support/FormatVariadic.cpp
+++ b/llvm/lib/Support/FormatVariadic.cpp
@@ -107,15 +107,18 @@ formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
       StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
       return std::make_pair(ReplacementItem{Middle}, Right);
     }
-    // An unterminated open brace is undefined.  We treat the rest of the string
-    // as a literal replacement, but we assert to indicate that this is
-    // undefined and that we consider it an error.
+    // An unterminated open brace is undefined. Assert to indicate that this is
+    // undefined and that we consider it an error. When asserts are disabled,
+    // built a replacement item with an error message.
     std::size_t BC = Fmt.find_first_of('}');
     if (BC == StringRef::npos) {
       assert(
           false &&
-          "Unterminated brace sequence.  Escape with {{ for a literal brace.");
-      return std::make_pair(ReplacementItem{Fmt}, StringRef());
+          "Unterminated brace sequence. Escape with {{ for a literal brace.");
+      return std::make_pair(
+          ReplacementItem{"Unterminated brace sequence. Escape with {{ for a "
+                          "literal brace."},
+          StringRef());
     }
 
     // Even if there is a closing brace, if there is another open brace before

``````````

</details>


https://github.com/llvm/llvm-project/pull/104688


More information about the llvm-commits mailing list