summaryrefslogtreecommitdiffstats
path: root/llvm/lib/MC/MCParser/AsmParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/MC/MCParser/AsmParser.cpp')
-rw-r--r--llvm/lib/MC/MCParser/AsmParser.cpp88
1 files changed, 51 insertions, 37 deletions
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 9a331ec4458..5b8c4ee5df1 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -257,6 +257,7 @@ private:
bool parseStatement(ParseStatementInfo &Info,
MCAsmParserSemaCallback *SI);
bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
+ void eatToEndOfLine();
bool parseCppHashLineFilenameComment(SMLoc L);
void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
@@ -627,10 +628,6 @@ const AsmToken &AsmParser::Lex() {
Error(Lexer.getErrLoc(), Lexer.getErr());
const AsmToken *tok = &Lexer.Lex();
- // Drop comments here.
- while (tok->is(AsmToken::Comment)) {
- tok = &Lexer.Lex();
- }
if (tok->is(AsmToken::Eof)) {
// If this is the end of an included file, pop the parent file off the
@@ -638,7 +635,7 @@ const AsmToken &AsmParser::Lex() {
SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
if (ParentIncludeLoc != SMLoc()) {
jumpToLoc(ParentIncludeLoc);
- return Lex();
+ tok = &Lexer.Lex();
}
}
@@ -723,8 +720,8 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
// first referenced for a source location. We need to add something
// to track that. Currently, we just point to the end of the file.
HadError |=
- Error(getTok().getLoc(), "assembler local symbol '" +
- Sym->getName() + "' not defined");
+ Error(getLexer().getLoc(), "assembler local symbol '" +
+ Sym->getName() + "' not defined");
}
}
@@ -769,7 +766,7 @@ StringRef AsmParser::parseStringToEndOfStatement() {
const char *Start = getTok().getLoc().getPointer();
while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
- Lexer.Lex();
+ Lex();
const char *End = getTok().getLoc().getPointer();
return StringRef(Start, End - Start);
@@ -780,7 +777,7 @@ StringRef AsmParser::parseStringToComma() {
while (Lexer.isNot(AsmToken::EndOfStatement) &&
Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
- Lexer.Lex();
+ Lex();
const char *End = getTok().getLoc().getPointer();
return StringRef(Start, End - Start);
@@ -862,7 +859,7 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
if (!MAI.useParensForSymbolVariant()) {
if (FirstTokenKind == AsmToken::String) {
if (Lexer.is(AsmToken::At)) {
- Lex(); // eat @
+ Lexer.Lex(); // eat @
SMLoc AtLoc = getLexer().getLoc();
StringRef VName;
if (parseIdentifier(VName))
@@ -874,14 +871,14 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Split = Identifier.split('@');
}
} else if (Lexer.is(AsmToken::LParen)) {
- Lex(); // eat '('.
+ Lexer.Lex(); // eat (
StringRef VName;
parseIdentifier(VName);
if (Lexer.isNot(AsmToken::RParen)) {
return Error(Lexer.getTok().getLoc(),
"unexpected token in variant, expected ')'");
}
- Lex(); // eat ')'.
+ Lexer.Lex(); // eat )
Split = std::make_pair(Identifier, VName);
}
@@ -1346,24 +1343,21 @@ bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
/// ::= Label* Identifier OperandList* EndOfStatement
bool AsmParser::parseStatement(ParseStatementInfo &Info,
MCAsmParserSemaCallback *SI) {
- // Eat initial spaces and comments
- while (Lexer.is(AsmToken::Space))
- Lex();
if (Lexer.is(AsmToken::EndOfStatement)) {
- // if this is a line comment we can drop it safely
- if (getTok().getString().front() == '\r' ||
- getTok().getString().front() == '\n')
- Out.AddBlankLine();
+ Out.AddBlankLine();
Lex();
return false;
}
- // Statements always start with an identifier.
+
+ // Statements always start with an identifier or are a full line comment.
AsmToken ID = getTok();
SMLoc IDLoc = ID.getLoc();
StringRef IDVal;
int64_t LocalLabelVal = -1;
- if (Lexer.is(AsmToken::HashDirective))
+ // A full line comment is a '#' as the first token.
+ if (Lexer.is(AsmToken::Hash))
return parseCppHashLineFilenameComment(IDLoc);
+
// Allow an integer followed by a ':' as a directional local label.
if (Lexer.is(AsmToken::Integer)) {
LocalLabelVal = getTok().getIntVal();
@@ -1654,8 +1648,7 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
return parseDirectiveIncbin();
case DK_CODE16:
case DK_CODE16GCC:
- return TokError(Twine(IDVal) +
- " not currently supported for this target");
+ return TokError(Twine(IDVal) + " not supported yet");
case DK_REPT:
return parseDirectiveRept(IDLoc, IDVal);
case DK_IRP:
@@ -1875,20 +1868,37 @@ AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
return true;
}
+/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
+/// since they may not be able to be tokenized to get to the end of line token.
+void AsmParser::eatToEndOfLine() {
+ if (!Lexer.is(AsmToken::EndOfStatement))
+ Lexer.LexUntilEndOfLine();
+ // Eat EOL.
+ Lex();
+}
+
/// parseCppHashLineFilenameComment as this:
/// ::= # number "filename"
+/// or just as a full line comment if it doesn't have a number and a string.
bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
Lex(); // Eat the hash token.
- // Lexer only ever emits HashDirective if it fully formed if it's
- // done the checking already so this is an internal error.
- assert(getTok().is(AsmToken::Integer) &&
- "Lexing Cpp line comment: Expected Integer");
+
+ if (getLexer().isNot(AsmToken::Integer)) {
+ // Consume the line since in cases it is not a well-formed line directive,
+ // as if were simply a full line comment.
+ eatToEndOfLine();
+ return false;
+ }
+
int64_t LineNumber = getTok().getIntVal();
Lex();
- assert(getTok().is(AsmToken::String) &&
- "Lexing Cpp line comment: Expected String");
+
+ if (getLexer().isNot(AsmToken::String)) {
+ eatToEndOfLine();
+ return false;
+ }
+
StringRef Filename = getTok().getString();
- Lex();
// Get rid of the enclosing quotes.
Filename = Filename.substr(1, Filename.size() - 2);
@@ -1897,6 +1907,9 @@ bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) {
CppHashInfo.Filename = Filename;
CppHashInfo.LineNumber = LineNumber;
CppHashInfo.Buf = CurBuffer;
+
+ // Ignore any trailing characters, they're just comment.
+ eatToEndOfLine();
return false;
}
@@ -2255,7 +2268,7 @@ bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
break;
if (FAI >= NParameters) {
- assert(M && "expected macro to be defined");
+ assert(M && "expected macro to be defined");
Error(IDLoc,
"parameter named '" + FA.Name + "' does not exist for macro '" +
M->Name + "'");
@@ -2413,7 +2426,7 @@ bool AsmParser::parseIdentifier(StringRef &Res) {
// Construct the joined identifier and consume the token.
Res =
StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
- Lex(); // Parser Lex to maintain invariants.
+ Lexer.Lex(); // Lexer's Lex guarantees consecutive token
return false;
}
@@ -2555,16 +2568,16 @@ bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
if (Lexer.isNot(AsmToken::Comma))
return TokError("expected comma");
- Lex();
+ Lexer.Lex();
if (Lexer.isNot(AsmToken::Identifier))
return TokError("expected relocation name");
SMLoc NameLoc = Lexer.getTok().getLoc();
StringRef Name = Lexer.getTok().getIdentifier();
- Lex();
+ Lexer.Lex();
if (Lexer.is(AsmToken::Comma)) {
- Lex();
+ Lexer.Lex();
SMLoc ExprLoc = Lexer.getLoc();
if (parseExpression(Expr))
return true;
@@ -5237,9 +5250,10 @@ static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
bool parseAssignmentExpression(StringRef Name, bool allow_redef,
MCAsmParser &Parser, MCSymbol *&Sym,
const MCExpr *&Value) {
+ MCAsmLexer &Lexer = Parser.getLexer();
// FIXME: Use better location, we should use proper tokens.
- SMLoc EqualLoc = Parser.getTok().getLoc();
+ SMLoc EqualLoc = Lexer.getLoc();
if (Parser.parseExpression(Value)) {
Parser.TokError("missing expression");
@@ -5251,7 +5265,7 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef,
// a = b
// b = c
- if (Parser.getTok().isNot(AsmToken::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return Parser.TokError("unexpected token in assignment");
// Eat the end of statement marker.
OpenPOWER on IntegriCloud