From 3fed5faf01700f4a1b1f36a153086917e55cb5fe Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 16 Oct 2011 12:10:27 +0000 Subject: [PATCH] Add AsmToken::getEndLoc and use it to add ranges to x86 asm register parsing. :1:12: error: register %rax is only available in 64-bit mode incl %rax ^~~~ llvm-svn: 142137 --- include/llvm/MC/MCParser/MCAsmLexer.h | 1 + lib/MC/MCParser/MCAsmLexer.cpp | 4 ++++ lib/Target/X86/AsmParser/X86AsmParser.cpp | 23 ++++++++++++++--------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/llvm/MC/MCParser/MCAsmLexer.h b/include/llvm/MC/MCParser/MCAsmLexer.h index 9bbb75581c2..ac04483ccf1 100644 --- a/include/llvm/MC/MCParser/MCAsmLexer.h +++ b/include/llvm/MC/MCParser/MCAsmLexer.h @@ -71,6 +71,7 @@ public: bool isNot(TokenKind K) const { return Kind != K; } SMLoc getLoc() const; + SMLoc getEndLoc() const; /// getStringContents - Get the contents of a string token (without quotes). StringRef getStringContents() const { diff --git a/lib/MC/MCParser/MCAsmLexer.cpp b/lib/MC/MCParser/MCAsmLexer.cpp index dceece78ba1..3a3ff147117 100644 --- a/lib/MC/MCParser/MCAsmLexer.cpp +++ b/lib/MC/MCParser/MCAsmLexer.cpp @@ -25,3 +25,7 @@ SMLoc MCAsmLexer::getLoc() const { SMLoc AsmToken::getLoc() const { return SMLoc::getFromPointer(Str.data()); } + +SMLoc AsmToken::getEndLoc() const { + return SMLoc::getFromPointer(Str.data() + Str.size() - 1); +} diff --git a/lib/Target/X86/AsmParser/X86AsmParser.cpp b/lib/Target/X86/AsmParser/X86AsmParser.cpp index 42512a7c50d..6bedd524070 100644 --- a/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ b/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -405,7 +405,8 @@ bool X86ATTAsmParser::ParseRegister(unsigned &RegNo, const AsmToken &Tok = Parser.getTok(); if (Tok.isNot(AsmToken::Identifier)) - return Error(Tok.getLoc(), "invalid register name"); + return Error(StartLoc, "invalid register name", + SMRange(StartLoc, Tok.getEndLoc())); RegNo = MatchRegisterName(Tok.getString()); @@ -423,8 +424,9 @@ bool X86ATTAsmParser::ParseRegister(unsigned &RegNo, X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) || X86II::isX86_64NonExtLowByteReg(RegNo) || X86II::isX86_64ExtendedReg(RegNo)) - return Error(Tok.getLoc(), "register %" - + Tok.getString() + " is only available in 64-bit mode"); + return Error(StartLoc, "register %" + + Tok.getString() + " is only available in 64-bit mode", + SMRange(StartLoc, Tok.getEndLoc())); } // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens. @@ -485,9 +487,10 @@ bool X86ATTAsmParser::ParseRegister(unsigned &RegNo, } if (RegNo == 0) - return Error(Tok.getLoc(), "invalid register name"); + return Error(StartLoc, "invalid register name", + SMRange(StartLoc, Tok.getEndLoc())); - EndLoc = Tok.getLoc(); + EndLoc = Tok.getEndLoc(); Parser.Lex(); // Eat identifier token. return false; } @@ -503,7 +506,8 @@ X86Operand *X86ATTAsmParser::ParseOperand() { SMLoc Start, End; if (ParseRegister(RegNo, Start, End)) return 0; if (RegNo == X86::EIZ || RegNo == X86::RIZ) { - Error(Start, "%eiz and %riz can only be used as index registers"); + Error(Start, "%eiz and %riz can only be used as index registers", + SMRange(Start, End)); return 0; } @@ -587,10 +591,11 @@ X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) { unsigned BaseReg = 0, IndexReg = 0, Scale = 1; if (getLexer().is(AsmToken::Percent)) { - SMLoc L; - if (ParseRegister(BaseReg, L, L)) return 0; + SMLoc StartLoc, EndLoc; + if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0; if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) { - Error(L, "eiz and riz can only be used as index registers"); + Error(StartLoc, "eiz and riz can only be used as index registers", + SMRange(StartLoc, EndLoc)); return 0; } }