Skip to content

Commit 682fe17

Browse files
committed
[MC/AsmLexer] Add '?' (Question) token
'?' is a valid token in our downstream target. There seem to be no way to do target-specific lexing, so just add make AsmParser recognize it. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D154202
1 parent 5e28d30 commit 682fe17

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

llvm/include/llvm/MC/MCAsmMacro.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AsmToken {
4646
Slash, // '/'
4747
BackSlash, // '\'
4848
LParen, RParen, LBrac, RBrac, LCurly, RCurly,
49-
Star, Dot, Comma, Dollar, Equal, EqualEqual,
49+
Question, Star, Dot, Comma, Dollar, Equal, EqualEqual,
5050

5151
Pipe, PipePipe, Caret,
5252
Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,

llvm/lib/MC/MCParser/AsmLexer.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,11 @@ AsmToken AsmLexer::LexToken() {
776776
IsAtStartOfStatement = false;
777777
switch (CurChar) {
778778
default:
779-
// Handle identifier: [a-zA-Z_.?][a-zA-Z0-9_$.@#?]*
780-
if (isalpha(CurChar) || CurChar == '_' || CurChar == '.' ||
781-
(MAI.doesAllowQuestionAtStartOfIdentifier() && CurChar == '?'))
779+
// Handle identifier: [a-zA-Z_.$@#?][a-zA-Z0-9_.$@#?]*
780+
// Whether or not the lexer accepts '$', '@', '#' and '?' at the start of
781+
// an identifier is target-dependent. These characters are handled in the
782+
// respective switch cases.
783+
if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
782784
return LexIdentifier();
783785

784786
// Unknown character, emit an error.
@@ -830,11 +832,18 @@ AsmToken AsmLexer::LexToken() {
830832
return LexIdentifier();
831833
return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
832834
}
833-
case '@': {
835+
case '@':
834836
if (MAI.doesAllowAtAtStartOfIdentifier())
835837
return LexIdentifier();
836838
return AsmToken(AsmToken::At, StringRef(TokStart, 1));
837-
}
839+
case '#':
840+
if (MAI.doesAllowHashAtStartOfIdentifier())
841+
return LexIdentifier();
842+
return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
843+
case '?':
844+
if (MAI.doesAllowQuestionAtStartOfIdentifier())
845+
return LexIdentifier();
846+
return AsmToken(AsmToken::Question, StringRef(TokStart, 1));
838847
case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
839848
case '=':
840849
if (*CurPtr == '=') {
@@ -914,11 +923,6 @@ AsmToken AsmLexer::LexToken() {
914923
case '/':
915924
IsAtStartOfStatement = OldIsAtStartOfStatement;
916925
return LexSlash();
917-
case '#': {
918-
if (MAI.doesAllowHashAtStartOfIdentifier())
919-
return LexIdentifier();
920-
return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
921-
}
922926
case '\'': return LexSingleQuote();
923927
case '"': return LexQuote();
924928
case '0': case '1': case '2': case '3': case '4':

llvm/lib/MC/MCParser/MCAsmLexer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void AsmToken::dump(raw_ostream &OS) const {
8888
case AsmToken::Pipe: OS << "Pipe"; break;
8989
case AsmToken::PipePipe: OS << "PipePipe"; break;
9090
case AsmToken::Plus: OS << "Plus"; break;
91+
case AsmToken::Question: OS << "Question"; break;
9192
case AsmToken::RBrac: OS << "RBrac"; break;
9293
case AsmToken::RCurly: OS << "RCurly"; break;
9394
case AsmToken::RParen: OS << "RParen"; break;

llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ TEST_F(SystemZAsmLexerLinux, CheckDefaultQuestionAtStartOfIdentifier) {
358358
Parser->getLexer().Lex();
359359

360360
SmallVector<AsmToken::TokenKind> ExpectedTokens(
361-
{AsmToken::Error, AsmToken::Identifier, AsmToken::EndOfStatement,
361+
{AsmToken::Question, AsmToken::Identifier, AsmToken::EndOfStatement,
362362
AsmToken::Eof});
363363
lexAndCheckTokens(AsmStr, ExpectedTokens);
364364
}

0 commit comments

Comments
 (0)