https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/177736

None

>From a932d000be3837236b6258f5e2a896e8b3c6ca11 Mon Sep 17 00:00:00 2001
From: Owen Pan <[email protected]>
Date: Fri, 23 Jan 2026 21:42:39 -0800
Subject: [PATCH] [clang-format] Handle \\""" at end of Java text block

---
 clang/lib/Format/FormatTokenLexer.cpp         | 11 ++++++++---
 clang/unittests/Format/FormatTestJava.cpp     |  7 +++++++
 clang/unittests/Format/TokenAnnotatorTest.cpp |  9 +++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/FormatTokenLexer.cpp 
b/clang/lib/Format/FormatTokenLexer.cpp
index eb8658396ecde..4a087d9e6dc2b 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -718,14 +718,19 @@ void FormatTokenLexer::tryParseJavaTextBlock() {
   ++S; // Skip the `"""` that begins a text block.
 
   // Find the `"""` that ends the text block.
+  bool Escaped = false;
   for (int Count = 0; Count < 3 && S < End; ++S) {
+    if (Escaped) {
+      Escaped = false;
+      continue;
+    }
     switch (*S) {
-    case '\\':
-      Count = -1;
-      break;
     case '\"':
       ++Count;
       break;
+    case '\\':
+      Escaped = true;
+      [[fallthrough]];
     default:
       Count = 0;
     }
diff --git a/clang/unittests/Format/FormatTestJava.cpp 
b/clang/unittests/Format/FormatTestJava.cpp
index 3cc97e2dc0b2e..29890aa863569 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -846,6 +846,13 @@ TEST_F(FormatTestJava, TextBlock) {
 
   verifyNoChange("String name = \"\"\"\n"
                  "              Pat Q. Smith");
+
+  verifyFormat("String foo = \"\"\"\n"
+               "    bar\n"
+               "    \\\\\"\"\";",
+               "String foo=\"\"\"\n"
+               "    bar\n"
+               "    \\\\\"\"\" ;");
 }
 
 TEST_F(FormatTestJava, BreakAfterRecord) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index a04c6bea4d050..b3e127c3eb6ba 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -4006,6 +4006,15 @@ TEST_F(TokenAnnotatorTest, SwitchExpression) {
   EXPECT_TOKEN(Tokens[20], tok::arrow, TT_CaseLabelArrow);
 }
 
+TEST_F(TokenAnnotatorTest, TextBlock) {
+  auto Tokens = annotate("String foo = \"\"\"\n"
+                         "    bar\n"
+                         "    \\\\\"\"\";",
+                         getLLVMStyle(FormatStyle::LK_Java));
+  ASSERT_EQ(Tokens.size(), 6u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_StringInConcatenation);
+}
+
 TEST_F(TokenAnnotatorTest, JavaRecord) {
   auto Tokens = annotate("public record MyRecord() {}",
                          getLLVMStyle(FormatStyle::LK_Java));

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

Reply via email to