Skip to content

Commit e112f15

Browse files
committed
Also ignore invalid text blocks
1 parent 470eca4 commit e112f15

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -698,29 +698,16 @@ void FormatTokenLexer::tryParseJavaTextBlock() {
698698
if (FormatTok->TokenText != "\"\"")
699699
return;
700700

701-
const auto *Str = Lex->getBufferLocation();
701+
const auto *S = Lex->getBufferLocation();
702702
const auto *End = Lex->getBuffer().end();
703703

704-
if (Str == End || *Str != '\"')
704+
if (S == End || *S != '\"')
705705
return;
706706

707-
// Skip the `"""` that begins a text block.
708-
const auto *S = Str + 1;
709-
710-
// From docs.oracle.com/en/java/javase/15/text-blocks/#text-block-syntax:
711-
// A text block begins with three double-quote characters followed by a line
712-
// terminator.
713-
while (S < End && *S != '\n') {
714-
if (!isblank(*S))
715-
return;
716-
++S;
717-
}
707+
++S; // Skip the `"""` that begins a text block.
718708

719709
// Find the `"""` that ends the text block.
720-
for (int Count = 0; Count < 3; ++S) {
721-
if (S == End)
722-
return;
723-
710+
for (int Count = 0; Count < 3 && S < End; ++S) {
724711
switch (*S) {
725712
case '\\':
726713
Count = -1;
@@ -733,7 +720,7 @@ void FormatTokenLexer::tryParseJavaTextBlock() {
733720
}
734721
}
735722

736-
// Skip the text block.
723+
// Ignore the possibly invalid text block.
737724
resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(S)));
738725
}
739726

clang/unittests/Format/FormatTestJava.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,16 @@ TEST_F(FormatTestJava, TextBlock) {
836836
" }\n"
837837
"}");
838838

839-
verifyNoChange("String name = \"\"\"\n"
839+
verifyNoChange("String name = \"\"\"\r\n"
840840
" red\n"
841841
" green\n"
842842
" blue\\\n"
843843
" \"\"\";");
844+
845+
verifyFormat("String name = \"\"\"Pat Q. Smith\"\"\";");
846+
847+
verifyNoChange("String name = \"\"\"\n"
848+
" Pat Q. Smith");
844849
}
845850

846851
} // namespace

0 commit comments

Comments
 (0)