[clang] [clang-format] Handle \\""" at end of Java text block (PR #177736)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 23 21:47:08 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: owenca (owenca)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/177736.diff
3 Files Affected:
- (modified) clang/lib/Format/FormatTokenLexer.cpp (+8-3)
- (modified) clang/unittests/Format/FormatTestJava.cpp (+7)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+9)
``````````diff
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));
``````````
</details>
https://github.com/llvm/llvm-project/pull/177736
More information about the cfe-commits
mailing list