Index: llvm/trunk/lib/MC/MCParser/AsmLexer.cpp =================================================================== --- llvm/trunk/lib/MC/MCParser/AsmLexer.cpp +++ llvm/trunk/lib/MC/MCParser/AsmLexer.cpp @@ -46,7 +46,7 @@ AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) { SetError(SMLoc::getFromPointer(Loc), Msg); - return AsmToken(AsmToken::Error, StringRef(Loc, 0)); + return AsmToken(AsmToken::Error, StringRef(Loc, CurPtr - Loc)); } int AsmLexer::getNextChar() { Index: llvm/trunk/lib/MC/MCParser/AsmParser.cpp =================================================================== --- llvm/trunk/lib/MC/MCParser/AsmParser.cpp +++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp @@ -624,6 +624,9 @@ } const AsmToken &AsmParser::Lex() { + if (Lexer.getTok().is(AsmToken::Error)) + Error(Lexer.getErrLoc(), Lexer.getErr()); + const AsmToken *tok = &Lexer.Lex(); if (tok->is(AsmToken::Eof)) { @@ -636,8 +639,6 @@ } } - if (tok->is(AsmToken::Error)) - Error(Lexer.getErrLoc(), Lexer.getErr()); return *tok; } @@ -675,6 +676,12 @@ if (!parseStatement(Info, nullptr)) continue; + // If we've failed, but on a Error Token, but did not consume it in + // favor of a better message, emit it now. + if (Lexer.getTok().is(AsmToken::Error)) { + Lex(); + } + // We had an error, validate that one was emitted and recover by skipping to // the next line. assert(HadError && "Parse statement returned an error, but none emitted!"); @@ -748,11 +755,11 @@ /// \brief Throw away the rest of the line for testing purposes. void AsmParser::eatToEndOfStatement() { while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) - Lex(); + Lexer.Lex(); // Eat EOL. if (Lexer.is(AsmToken::EndOfStatement)) - Lex(); + Lexer.Lex(); } StringRef AsmParser::parseStringToEndOfStatement() { @@ -2164,7 +2171,7 @@ if (Lexer.is(AsmToken::Space)) { SpaceEaten = true; - Lex(); // Eat spaces + Lexer.Lex(); // Eat spaces } // Spaces can delimit parameters, but could also be part an expression. @@ -2173,11 +2180,11 @@ if (!IsDarwin) { if (isOperator(Lexer.getKind())) { MA.push_back(getTok()); - Lex(); + Lexer.Lex(); // Whitespace after an operator can be ignored. if (Lexer.is(AsmToken::Space)) - Lex(); + Lexer.Lex(); continue; } @@ -2199,7 +2206,7 @@ // Append the token to the current argument list. MA.push_back(getTok()); - Lex(); + Lexer.Lex(); } if (ParenLevel != 0) @@ -2408,7 +2415,7 @@ SMLoc PrefixLoc = getLexer().getLoc(); // Consume the prefix character, and check for a following identifier. - Lex(); + Lexer.Lex(); // Lexer's Lex guarantees consecutive token. if (Lexer.isNot(AsmToken::Identifier)) return true; @@ -2419,7 +2426,7 @@ // Construct the joined identifier and consume the token. Res = StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); - Lex(); + Lexer.Lex(); // Lexer's Lex guarantees consecutive token return false; } @@ -2686,14 +2693,15 @@ // have to manually parse unary prefixes. bool IsNeg = false; if (getLexer().is(AsmToken::Minus)) { - Lex(); + Lexer.Lex(); IsNeg = true; } else if (getLexer().is(AsmToken::Plus)) - Lex(); + Lexer.Lex(); - if (getLexer().isNot(AsmToken::Integer) && - getLexer().isNot(AsmToken::Real) && - getLexer().isNot(AsmToken::Identifier)) + if (Lexer.is(AsmToken::Error)) + return TokError(Lexer.getErr()); + if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) && + Lexer.isNot(AsmToken::Identifier)) return TokError("unexpected token in directive"); // Convert to an APFloat. @@ -2720,10 +2728,10 @@ getStreamer().EmitIntValue(AsInt.getLimitedValue(), AsInt.getBitWidth() / 8); - if (getLexer().is(AsmToken::EndOfStatement)) + if (Lexer.is(AsmToken::EndOfStatement)) break; - if (getLexer().isNot(AsmToken::Comma)) + if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); Lex(); } @@ -3762,14 +3770,19 @@ Lex(); } - // Eat the end of statement. - Lex(); + // Eat just the end of statement. + Lexer.Lex(); + // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors AsmToken EndToken, StartToken = getTok(); unsigned MacroDepth = 0; - // Lex the macro definition. for (;;) { + // Ignore Lexing errors in macros. + while (Lexer.is(AsmToken::Error)) { + Lexer.Lex(); + } + // Check whether we have reached the end of the file. if (getLexer().is(AsmToken::Eof)) return Error(DirectiveLoc, "no matching '.endmacro' in definition"); @@ -3780,7 +3793,7 @@ getTok().getIdentifier() == ".endmacro") { if (MacroDepth == 0) { // Outermost macro. EndToken = getTok(); - Lex(); + Lexer.Lex(); if (getLexer().isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '" + EndToken.getIdentifier() + "' directive"); Index: llvm/trunk/lib/MC/MCParser/MCAsmLexer.cpp =================================================================== --- llvm/trunk/lib/MC/MCParser/MCAsmLexer.cpp +++ llvm/trunk/lib/MC/MCParser/MCAsmLexer.cpp @@ -13,7 +13,7 @@ using namespace llvm; MCAsmLexer::MCAsmLexer() : TokStart(nullptr), SkipSpace(true) { - CurTok.emplace_back(AsmToken::Error, StringRef()); + CurTok.emplace_back(AsmToken::Space, StringRef()); } MCAsmLexer::~MCAsmLexer() { Index: llvm/trunk/test/MC/AsmParser/floating-literals.s =================================================================== --- llvm/trunk/test/MC/AsmParser/floating-literals.s +++ llvm/trunk/test/MC/AsmParser/floating-literals.s @@ -58,25 +58,19 @@ .float -0x1.0p0 # CHECK-ERROR: invalid hexadecimal floating-point constant: expected at least one exponent digit -# CHECK-ERROR: unexpected token in directive .float 0xa.apa # CHECK-ERROR: invalid hexadecimal floating-point constant: expected at least one exponent digit -# CHECK-ERROR: unexpected token in directive .double -0x1.2p+ # CHECK-ERROR: invalid hexadecimal floating-point constant: expected at least one exponent digit -# CHECK-ERROR: unexpected token in directive .double -0x1.2p # CHECK-ERROR: invalid hexadecimal floating-point constant: expected at least one significand digit -# CHECK-ERROR: unexpected token in directive .float 0xp2 # CHECK-ERROR: invalid hexadecimal floating-point constant: expected at least one significand digit -# CHECK-ERROR: unexpected token in directive .float 0x.p5 # CHECK-ERROR: error: invalid hexadecimal floating-point constant: expected exponent part 'p' -# CHECK-ERROR: unexpected token in directive .float 0x1.2 Index: llvm/trunk/test/MC/AsmParser/macro_parsing.s =================================================================== --- llvm/trunk/test/MC/AsmParser/macro_parsing.s +++ llvm/trunk/test/MC/AsmParser/macro_parsing.s @@ -0,0 +1,16 @@ +# RUN: llvm-mc -triple i386-unknown-unknown %s | FileCheck %s + + .macro DEF num + int $0x\num + .endm + DEF 02 + DEF 08 + DEF 09 + DEF 0A + DEF 10 + +# CHECK: int $2 +# CHECK: int $8 +# CHECK: int $9 +# CHECK: int $10 +# CHECK: int $16