@@ -93,8 +93,10 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
93
93
// string literals are correctly identified.
94
94
handleCSharpVerbatimAndInterpolatedStrings ();
95
95
}
96
- if (Style.isTableGen ())
96
+ if (Style.isTableGen ()) {
97
97
handleTableGenMultilineString ();
98
+ handleTableGenNumericLikeIdentifier ();
99
+ }
98
100
if (Tokens.back ()->NewlinesBefore > 0 || Tokens.back ()->IsMultiline )
99
101
FirstInLineIndex = Tokens.size () - 1 ;
100
102
} while (Tokens.back ()->isNot (tok::eof));
@@ -804,6 +806,44 @@ void FormatTokenLexer::handleTableGenMultilineString() {
804
806
FirstLineText, MultiLineString->OriginalColumn , Style.TabWidth , Encoding);
805
807
}
806
808
809
+ void FormatTokenLexer::handleTableGenNumericLikeIdentifier () {
810
+ FormatToken *Tok = Tokens.back ();
811
+ // TableGen identifiers can begin with digits. Such tokens are lexed as
812
+ // numeric_constant now.
813
+ if (Tok->isNot (tok::numeric_constant))
814
+ return ;
815
+ StringRef Text = Tok->TokenText ;
816
+ // The following check is based on llvm::TGLexer::LexToken.
817
+ // That lexes the token as a number if any of the following holds:
818
+ // 1. It starts with '+', '-'.
819
+ // 2. All the characters are digits.
820
+ // 3. The first non-digit character is 'b', and the next is '0' or '1'.
821
+ // 4. The first non-digit character is 'x', and the next is a hex digit.
822
+ // Note that in the case 3 and 4, if the next character does not exists in
823
+ // this token, the token is an identifier.
824
+ if (Text.size () < 1 || Text[0 ] == ' +' || Text[0 ] == ' -' )
825
+ return ;
826
+ const auto NonDigitPos = Text.find_if ([](char C) { return !isdigit (C); });
827
+ // All the characters are digits
828
+ if (NonDigitPos == StringRef::npos)
829
+ return ;
830
+ char FirstNonDigit = Text[NonDigitPos];
831
+ if (NonDigitPos < Text.size () - 1 ) {
832
+ char TheNext = Text[NonDigitPos + 1 ];
833
+ // Regarded as a binary number.
834
+ if (FirstNonDigit == ' b' && (TheNext == ' 0' || TheNext == ' 1' ))
835
+ return ;
836
+ // Regarded as hex number.
837
+ if (FirstNonDigit == ' x' && isxdigit (TheNext))
838
+ return ;
839
+ }
840
+ if (isalpha (FirstNonDigit) || FirstNonDigit == ' _' ) {
841
+ // This is actually an identifier in TableGen.
842
+ Tok->Tok .setKind (tok::identifier);
843
+ Tok->Tok .setIdentifierInfo (nullptr );
844
+ }
845
+ }
846
+
807
847
void FormatTokenLexer::handleTemplateStrings () {
808
848
FormatToken *BacktickToken = Tokens.back ();
809
849
0 commit comments