@@ -274,13 +274,13 @@ void FormatTokenLexer::tryMergePreviousTokens() {
274
274
return ;
275
275
}
276
276
}
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 ;
284
284
}
285
285
}
286
286
@@ -778,45 +778,31 @@ void FormatTokenLexer::handleTableGenMultilineString() {
778
778
if (MultiLineString->isNot (TT_TableGenMultiLineString))
779
779
return ;
780
780
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);
820
806
}
821
807
}
822
808
0 commit comments