Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 0d8f244

Browse files
committed
[MC] Move parser helper functions from Asmparser to MCAsmParser
NFC Intended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280092 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2a7897d commit 0d8f244

File tree

4 files changed

+83
-66
lines changed

4 files changed

+83
-66
lines changed

include/llvm/MC/MCParser/MCAsmParser.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ class MCAsmParser {
148148
/// \brief Report an error at the current lexer location.
149149
bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None);
150150

151+
bool parseTokenLoc(SMLoc &Loc);
152+
bool parseToken(AsmToken::TokenKind T, const Twine &Msg);
153+
bool parseOptionalToken(AsmToken::TokenKind T, bool &Present);
154+
155+
bool parseEOL(const Twine &ErrMsg);
156+
157+
bool parseIntToken(int64_t &V, const Twine &ErrMsg);
158+
159+
bool check(bool P, const llvm::Twine &Msg);
160+
bool check(bool P, SMLoc Loc, const llvm::Twine &Msg);
161+
151162
/// \brief Parse an identifier or string (as a quoted identifier) and set \p
152163
/// Res to the identifier contents.
153164
virtual bool parseIdentifier(StringRef &Res) = 0;

include/llvm/MC/MCParser/MCAsmParserExtension.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class MCAsmParserExtension {
7979
}
8080

8181
const AsmToken &Lex() { return getParser().Lex(); }
82-
8382
const AsmToken &getTok() { return getParser().getTok(); }
83+
bool parseToken(AsmToken::TokenKind T, const Twine &Msg) {
84+
return getParser().parseToken(T, Msg);
85+
}
8486

8587
bool HasBracketExpressions() const { return BracketExpressionsSupported; }
8688

lib/MC/MCParser/AsmParser.cpp

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -282,42 +282,6 @@ class AsmParser : public MCAsmParser {
282282

283283
void checkForValidSection() override;
284284

285-
bool getTokenLoc(SMLoc &Loc) {
286-
Loc = getTok().getLoc();
287-
return false;
288-
}
289-
290-
bool parseEOL(const Twine &ErrMsg) {
291-
if (getTok().getKind() == AsmToken::Hash) {
292-
StringRef CommentStr = parseStringToEndOfStatement();
293-
Lexer.Lex();
294-
Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
295-
}
296-
if (getTok().getKind() != AsmToken::EndOfStatement)
297-
return TokError(ErrMsg);
298-
Lex();
299-
return false;
300-
}
301-
302-
/// parseToken - If current token has the specified kind, eat it and
303-
/// return success. Otherwise, emit the specified error and return failure.
304-
bool parseToken(AsmToken::TokenKind T, const Twine &ErrMsg) {
305-
if (T == AsmToken::EndOfStatement)
306-
return parseEOL(ErrMsg);
307-
if (getTok().getKind() != T)
308-
return TokError(ErrMsg);
309-
Lex();
310-
return false;
311-
}
312-
313-
bool parseIntToken(int64_t &V, const Twine &ErrMsg) {
314-
if (getTok().getKind() != AsmToken::Integer)
315-
return TokError(ErrMsg);
316-
V = getTok().getIntVal();
317-
Lex();
318-
return false;
319-
}
320-
321285
/// }
322286

323287
private:
@@ -375,18 +339,6 @@ class AsmParser : public MCAsmParser {
375339
}
376340
static void DiagHandler(const SMDiagnostic &Diag, void *Context);
377341

378-
bool check(bool P, SMLoc Loc, const Twine &Msg) {
379-
if (P)
380-
return Error(Loc, Msg);
381-
return false;
382-
}
383-
384-
bool check(bool P, const Twine &Msg) {
385-
if (P)
386-
return TokError(Msg);
387-
return false;
388-
}
389-
390342
/// \brief Enter the specified file. This returns true on failure.
391343
bool enterIncludeFile(const std::string &Filename);
392344

@@ -2932,13 +2884,13 @@ bool AsmParser::parseDirectiveFill() {
29322884
if (getLexer().isNot(AsmToken::EndOfStatement)) {
29332885

29342886
if (parseToken(AsmToken::Comma, "unexpected token in '.fill' directive") ||
2935-
getTokenLoc(SizeLoc) || parseAbsoluteExpression(FillSize))
2887+
parseTokenLoc(SizeLoc) || parseAbsoluteExpression(FillSize))
29362888
return true;
29372889

29382890
if (getLexer().isNot(AsmToken::EndOfStatement)) {
29392891
if (parseToken(AsmToken::Comma,
29402892
"unexpected token in '.fill' directive") ||
2941-
getTokenLoc(ExprLoc) || parseAbsoluteExpression(FillExpr) ||
2893+
parseTokenLoc(ExprLoc) || parseAbsoluteExpression(FillExpr) ||
29422894
parseToken(AsmToken::EndOfStatement,
29432895
"unexpected token in '.fill' directive"))
29442896
return true;
@@ -3016,7 +2968,7 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
30162968

30172969
if (getTok().isNot(AsmToken::EndOfStatement)) {
30182970
if (parseToken(AsmToken::Comma, "unexpected token in directive") ||
3019-
getTokenLoc(MaxBytesLoc) || parseAbsoluteExpression(MaxBytesToFill))
2971+
parseTokenLoc(MaxBytesLoc) || parseAbsoluteExpression(MaxBytesToFill))
30202972
return true;
30212973
}
30222974
}
@@ -3295,11 +3247,11 @@ bool AsmParser::parseDirectiveCVFile() {
32953247
bool AsmParser::parseDirectiveCVLoc() {
32963248
SMLoc Loc;
32973249
int64_t FunctionId, FileNumber;
3298-
if (getTokenLoc(Loc) ||
3250+
if (parseTokenLoc(Loc) ||
32993251
parseIntToken(FunctionId, "unexpected token in '.cv_loc' directive") ||
33003252
check(FunctionId < 0, Loc,
33013253
"function id less than zero in '.cv_loc' directive") ||
3302-
getTokenLoc(Loc) ||
3254+
parseTokenLoc(Loc) ||
33033255
parseIntToken(FileNumber, "expected integer in '.cv_loc' directive") ||
33043256
check(FileNumber < 1, Loc,
33053257
"file number less than one in '.cv_loc' directive") ||
@@ -3368,12 +3320,12 @@ bool AsmParser::parseDirectiveCVLinetable() {
33683320
"function id less than zero in '.cv_linetable' directive") ||
33693321
parseToken(AsmToken::Comma,
33703322
"unexpected token in '.cv_linetable' directive") ||
3371-
getTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3372-
"expected identifier in directive") ||
3323+
parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3324+
"expected identifier in directive") ||
33733325
parseToken(AsmToken::Comma,
33743326
"unexpected token in '.cv_linetable' directive") ||
3375-
getTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3376-
"expected identifier in directive"))
3327+
parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3328+
"expected identifier in directive"))
33773329
return true;
33783330

33793331
MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
@@ -3395,22 +3347,22 @@ bool AsmParser::parseDirectiveCVInlineLinetable() {
33953347
"expected PrimaryFunctionId in '.cv_inline_linetable' directive") ||
33963348
check(PrimaryFunctionId < 0, Loc,
33973349
"function id less than zero in '.cv_inline_linetable' directive") ||
3398-
getTokenLoc(Loc) ||
3350+
parseTokenLoc(Loc) ||
33993351
parseIntToken(
34003352
SourceFileId,
34013353
"expected SourceField in '.cv_inline_linetable' directive") ||
34023354
check(SourceFileId <= 0, Loc,
34033355
"File id less than zero in '.cv_inline_linetable' directive") ||
3404-
getTokenLoc(Loc) ||
3356+
parseTokenLoc(Loc) ||
34053357
parseIntToken(
34063358
SourceLineNum,
34073359
"expected SourceLineNum in '.cv_inline_linetable' directive") ||
34083360
check(SourceLineNum < 0, Loc,
34093361
"Line number less than zero in '.cv_inline_linetable' directive") ||
3410-
getTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3411-
"expected identifier in directive") ||
3412-
getTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3413-
"expected identifier in directive"))
3362+
parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3363+
"expected identifier in directive") ||
3364+
parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3365+
"expected identifier in directive"))
34143366
return true;
34153367

34163368
SmallVector<unsigned, 8> SecondaryFunctionIds;
@@ -4057,8 +4009,9 @@ bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
40574009
bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
40584010
StringRef Name;
40594011
SMLoc Loc;
4060-
if (getTokenLoc(Loc) || check(parseIdentifier(Name), Loc,
4061-
"expected identifier in '.purgem' directive") ||
4012+
if (parseTokenLoc(Loc) ||
4013+
check(parseIdentifier(Name), Loc,
4014+
"expected identifier in '.purgem' directive") ||
40624015
parseToken(AsmToken::EndOfStatement,
40634016
"unexpected token in '.purgem' directive"))
40644017
return true;

lib/MC/MCParser/MCAsmParser.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,57 @@ const AsmToken &MCAsmParser::getTok() const {
3333
return getLexer().getTok();
3434
}
3535

36+
bool MCAsmParser::parseTokenLoc(SMLoc &Loc) {
37+
Loc = getTok().getLoc();
38+
return false;
39+
}
40+
41+
bool MCAsmParser::parseEOL(const Twine &Msg) {
42+
if (getTok().getKind() == AsmToken::Hash) {
43+
StringRef CommentStr = parseStringToEndOfStatement();
44+
getLexer().Lex();
45+
getLexer().UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
46+
}
47+
if (getTok().getKind() != AsmToken::EndOfStatement)
48+
return Error(getTok().getLoc(), Msg);
49+
Lex();
50+
return false;
51+
}
52+
53+
bool MCAsmParser::parseToken(AsmToken::TokenKind T, const Twine &Msg) {
54+
if (T == AsmToken::EndOfStatement)
55+
return parseEOL(Msg);
56+
if (getTok().getKind() != T)
57+
return Error(getTok().getLoc(), Msg);
58+
Lex();
59+
return false;
60+
}
61+
62+
bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
63+
if (getTok().getKind() != AsmToken::Integer)
64+
return TokError(Msg);
65+
V = getTok().getIntVal();
66+
Lex();
67+
return false;
68+
}
69+
70+
bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T, bool &Present) {
71+
Present = (getTok().getKind() == T);
72+
if (Present)
73+
Lex();
74+
return false;
75+
}
76+
77+
bool MCAsmParser::check(bool P, const Twine &Msg) {
78+
return check(P, getTok().getLoc(), Msg);
79+
}
80+
81+
bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
82+
if (P)
83+
return Error(Loc, Msg);
84+
return false;
85+
}
86+
3687
bool MCAsmParser::TokError(const Twine &Msg, ArrayRef<SMRange> Ranges) {
3788
Error(getLexer().getLoc(), Msg, Ranges);
3889
return true;

0 commit comments

Comments
 (0)