Skip to content

Commit bba0a09

Browse files
author
hnakamura5
committed
Fixed the reviewed points.
1 parent d076735 commit bba0a09

File tree

4 files changed

+45
-47
lines changed

4 files changed

+45
-47
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ struct FormatToken {
434434
setType(T);
435435
}
436436
bool isTypeFinalized() const { return TypeIsFinalized; }
437+
void setTypeIsFinalized() { TypeIsFinalized = true; }
437438

438439
/// Used to set an operator precedence explicitly.
439440
prec::Level ForcedPrecedence = prec::Unknown;

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,13 @@ void FormatTokenLexer::tryMergePreviousTokens() {
274274
return;
275275
}
276276
}
277-
if (Style.isTableGen()) {
278-
if (tryMergeTokens({tok::l_square, tok::l_brace},
279-
TT_TableGenMultiLineString)) {
280-
// Multi line string starts with [{
281-
Tokens.back()->Tok.setKind(tok::string_literal);
282-
return;
283-
}
277+
// TableGen's Multi line string starts with [{
278+
if (Style.isTableGen() && tryMergeTokens({tok::l_square, tok::l_brace},
279+
TT_TableGenMultiLineString)) {
280+
// This must never be annotated as other types.
281+
Tokens.back()->setTypeIsFinalized();
282+
Tokens.back()->Tok.setKind(tok::string_literal);
283+
return;
284284
}
285285
}
286286

@@ -778,45 +778,31 @@ void FormatTokenLexer::handleTableGenMultilineString() {
778778
if (MultiLineString->isNot(TT_TableGenMultiLineString))
779779
return;
780780

781-
bool PrevIsRBrace = false;
782-
const char *FirstBreak = nullptr;
783-
const char *LastBreak = nullptr;
784-
const char *Begin = MultiLineString->TokenText.begin();
785-
// Skip until }], the closer of multi line string found.
786-
for (const char *Current = Begin, *End = Lex->getBuffer().end();
787-
Current != End; ++Current) {
788-
if (PrevIsRBrace && *Current == ']') {
789-
// }] is the end of multi line string.
790-
if (!FirstBreak)
791-
FirstBreak = Current;
792-
MultiLineString->TokenText = StringRef(Begin, Current - Begin + 1);
793-
// ColumnWidth is only the width of the first line.
794-
MultiLineString->ColumnWidth = encoding::columnWidthWithTabs(
795-
StringRef(Begin, FirstBreak - Begin + 1),
796-
MultiLineString->OriginalColumn, Style.TabWidth, Encoding);
797-
if (LastBreak) {
798-
// Set LastLineColumnWidth if multi line string has multiple lines.
799-
MultiLineString->LastLineColumnWidth = encoding::columnWidthWithTabs(
800-
StringRef(LastBreak + 1, Current - LastBreak),
801-
MultiLineString->OriginalColumn, Style.TabWidth, Encoding);
802-
}
803-
resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Current + 1)));
804-
return;
805-
}
806-
PrevIsRBrace = false;
807-
if (*Current == '\n') {
808-
MultiLineString->IsMultiline = true;
809-
// Assure LastBreak is not equal to FirstBreak.
810-
if (!FirstBreak)
811-
FirstBreak = Current;
812-
LastBreak = Current;
813-
continue;
814-
}
815-
if (*Current == '}') {
816-
// Memorize '}'. If next character is ']', they are the closer.
817-
PrevIsRBrace = true;
818-
continue;
819-
}
781+
auto OpenOffset = Lex->getCurrentBufferOffset() - 2 /* "[{" */;
782+
// "}]" is the end of multi line string.
783+
auto CloseOffset = Lex->getBuffer().find("}]", OpenOffset);
784+
if (CloseOffset == StringRef::npos)
785+
return;
786+
auto Text = Lex->getBuffer().substr(OpenOffset, CloseOffset + 2);
787+
MultiLineString->TokenText = Text;
788+
resetLexer(SourceMgr.getFileOffset(
789+
Lex->getSourceLocation(Lex->getBufferLocation() - 2 + Text.size())));
790+
// Set ColumnWidth and LastLineColumnWidth.
791+
auto FirstLineText = Text;
792+
auto FirstBreak = Text.find('\n');
793+
if (FirstBreak != StringRef::npos) {
794+
MultiLineString->IsMultiline = true;
795+
FirstLineText = Text.substr(0, FirstBreak + 1);
796+
}
797+
// ColumnWidth holds only the width of the first line.
798+
MultiLineString->ColumnWidth = encoding::columnWidthWithTabs(
799+
FirstLineText, MultiLineString->OriginalColumn, Style.TabWidth, Encoding);
800+
auto LastBreak = Text.rfind('\n');
801+
if (LastBreak != StringRef::npos) {
802+
// Set LastLineColumnWidth if it has multiple lines.
803+
MultiLineString->LastLineColumnWidth = encoding::columnWidthWithTabs(
804+
Text.substr(LastBreak + 1, Text.size()),
805+
MultiLineString->OriginalColumn, Style.TabWidth, Encoding);
820806
}
821807
}
822808

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ class AnnotatingParser {
17101710
TT_UnionLBrace, TT_RequiresClause,
17111711
TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
17121712
TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
1713-
TT_BracedListLBrace, TT_TableGenMultiLineString)) {
1713+
TT_BracedListLBrace)) {
17141714
CurrentToken->setType(TT_Unknown);
17151715
}
17161716
CurrentToken->Role.reset();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,17 @@ TEST_F(TokenAnnotatorTest, UnderstandTableGenTokens) {
21982198
Tokens = Annotate("[{ code is multiline string }]");
21992199
ASSERT_EQ(Tokens.size(), 2u) << Tokens;
22002200
EXPECT_TOKEN(Tokens[0], tok::string_literal, TT_TableGenMultiLineString);
2201+
EXPECT_FALSE(Tokens[0]->IsMultiline);
2202+
// Case with multiple lines.
2203+
Tokens = Annotate("[{ It can break\n"
2204+
" across lines and the line breaks\n"
2205+
" are retained in \n"
2206+
" the string. }]");
2207+
ASSERT_EQ(Tokens.size(), 2u) << Tokens;
2208+
EXPECT_TOKEN(Tokens[0], tok::string_literal, TT_TableGenMultiLineString);
2209+
EXPECT_EQ(Tokens[0]->ColumnWidth, sizeof("[{ It can break\n") - 1);
2210+
EXPECT_TRUE(Tokens[0]->IsMultiline);
2211+
EXPECT_EQ(Tokens[0]->LastLineColumnWidth, sizeof(" the string. }]") - 1);
22012212
}
22022213

22032214
TEST_F(TokenAnnotatorTest, UnderstandConstructors) {

0 commit comments

Comments
 (0)