Skip to content

Commit 1b1dc50

Browse files
committed
[MCParser] Improve parseIntToken error message
Add a default argument, which is more readable than existing call sites and encourages new call sites to omit the argument. Omit " in ... directive" since this the error message includes the line.
1 parent 5a11912 commit 1b1dc50

File tree

5 files changed

+45
-64
lines changed

5 files changed

+45
-64
lines changed

llvm/include/llvm/MC/MCParser/MCAsmParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class MCAsmParser {
270270

271271
bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
272272

273-
bool parseIntToken(int64_t &V, const Twine &ErrMsg);
273+
bool parseIntToken(int64_t &V, const Twine &ErrMsg = "expected integer");
274274

275275
bool check(bool P, const Twine &Msg);
276276
bool check(bool P, SMLoc Loc, const Twine &Msg);

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,13 +3616,7 @@ bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
36163616
/// parseDirectiveLine
36173617
/// ::= .line [number]
36183618
bool AsmParser::parseDirectiveLine() {
3619-
int64_t LineNumber;
3620-
if (getLexer().is(AsmToken::Integer)) {
3621-
if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
3622-
return true;
3623-
(void)LineNumber;
3624-
// FIXME: Do something with the .line.
3625-
}
3619+
parseOptionalToken(AsmToken::Integer);
36263620
return parseEOL();
36273621
}
36283622

@@ -3636,7 +3630,7 @@ bool AsmParser::parseDirectiveLine() {
36363630
bool AsmParser::parseDirectiveLoc() {
36373631
int64_t FileNumber = 0, LineNumber = 0;
36383632
SMLoc Loc = getTok().getLoc();
3639-
if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
3633+
if (parseIntToken(FileNumber) ||
36403634
check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
36413635
"file number less than one in '.loc' directive") ||
36423636
check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
@@ -3753,8 +3747,7 @@ bool AsmParser::parseDirectiveCVFile() {
37533747
std::string Checksum;
37543748
int64_t ChecksumKind = 0;
37553749

3756-
if (parseIntToken(FileNumber,
3757-
"expected file number in '.cv_file' directive") ||
3750+
if (parseIntToken(FileNumber, "expected file number") ||
37583751
check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
37593752
check(getTok().isNot(AsmToken::String),
37603753
"unexpected token in '.cv_file' directive") ||
@@ -3787,19 +3780,18 @@ bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
37873780
StringRef DirectiveName) {
37883781
SMLoc Loc;
37893782
return parseTokenLoc(Loc) ||
3790-
parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
3791-
"' directive") ||
3783+
parseIntToken(FunctionId, "expected function id") ||
37923784
check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
37933785
"expected function id within range [0, UINT_MAX)");
37943786
}
37953787

37963788
bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
37973789
SMLoc Loc;
37983790
return parseTokenLoc(Loc) ||
3799-
parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
3800-
"' directive") ||
3801-
check(FileNumber < 1, Loc, "file number less than one in '" +
3802-
DirectiveName + "' directive") ||
3791+
parseIntToken(FileNumber, "expected file number") ||
3792+
check(FileNumber < 1, Loc,
3793+
"file number less than one in '" + DirectiveName +
3794+
"' directive") ||
38033795
check(!getCVContext().isValidFileNumber(FileNumber), Loc,
38043796
"unassigned file number in '" + DirectiveName + "' directive");
38053797
}
@@ -3978,21 +3970,15 @@ bool AsmParser::parseDirectiveCVInlineLinetable() {
39783970
SMLoc Loc = getTok().getLoc();
39793971
if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
39803972
parseTokenLoc(Loc) ||
3981-
parseIntToken(
3982-
SourceFileId,
3983-
"expected SourceField in '.cv_inline_linetable' directive") ||
3984-
check(SourceFileId <= 0, Loc,
3985-
"File id less than zero in '.cv_inline_linetable' directive") ||
3973+
parseIntToken(SourceFileId, "expected SourceField") ||
3974+
check(SourceFileId <= 0, Loc, "File id less than zero") ||
3975+
parseTokenLoc(Loc) ||
3976+
parseIntToken(SourceLineNum, "expected SourceLineNum") ||
3977+
check(SourceLineNum < 0, Loc, "Line number less than zero") ||
3978+
parseTokenLoc(Loc) ||
3979+
check(parseIdentifier(FnStartName), Loc, "expected identifier") ||
39863980
parseTokenLoc(Loc) ||
3987-
parseIntToken(
3988-
SourceLineNum,
3989-
"expected SourceLineNum in '.cv_inline_linetable' directive") ||
3990-
check(SourceLineNum < 0, Loc,
3991-
"Line number less than zero in '.cv_inline_linetable' directive") ||
3992-
parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3993-
"expected identifier in directive") ||
3994-
parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3995-
"expected identifier in directive"))
3981+
check(parseIdentifier(FnEndName), Loc, "expected identifier"))
39963982
return true;
39973983

39983984
if (parseEOL())
@@ -4154,7 +4140,7 @@ bool AsmParser::parseDirectiveCVFileChecksums() {
41544140
/// ::= .cv_filechecksumoffset fileno
41554141
bool AsmParser::parseDirectiveCVFileChecksumOffset() {
41564142
int64_t FileNo;
4157-
if (parseIntToken(FileNo, "expected identifier in directive"))
4143+
if (parseIntToken(FileNo))
41584144
return true;
41594145
if (parseEOL())
41604146
return true;
@@ -5896,24 +5882,16 @@ bool AsmParser::parseDirectivePseudoProbe() {
58965882
int64_t Type;
58975883
int64_t Attr;
58985884
int64_t Discriminator = 0;
5899-
5900-
if (parseIntToken(Guid, "unexpected token in '.pseudoprobe' directive"))
5885+
if (parseIntToken(Guid))
59015886
return true;
5902-
5903-
if (parseIntToken(Index, "unexpected token in '.pseudoprobe' directive"))
5887+
if (parseIntToken(Index))
59045888
return true;
5905-
5906-
if (parseIntToken(Type, "unexpected token in '.pseudoprobe' directive"))
5889+
if (parseIntToken(Type))
59075890
return true;
5908-
5909-
if (parseIntToken(Attr, "unexpected token in '.pseudoprobe' directive"))
5891+
if (parseIntToken(Attr))
5892+
return true;
5893+
if (hasDiscriminator(Attr) && parseIntToken(Discriminator))
59105894
return true;
5911-
5912-
if (hasDiscriminator(Attr)) {
5913-
if (parseIntToken(Discriminator,
5914-
"unexpected token in '.pseudoprobe' directive"))
5915-
return true;
5916-
}
59175895

59185896
// Parse inline stack like @ GUID:11:12 @ GUID:1:11 @ GUID:3:21
59195897
MCPseudoProbeInlineStack InlineStack;
@@ -5924,9 +5902,8 @@ bool AsmParser::parseDirectivePseudoProbe() {
59245902

59255903
int64_t CallerGuid = 0;
59265904
if (getLexer().is(AsmToken::Integer)) {
5927-
if (parseIntToken(CallerGuid,
5928-
"unexpected token in '.pseudoprobe' directive"))
5929-
return true;
5905+
CallerGuid = getTok().getIntVal();
5906+
Lex();
59305907
}
59315908

59325909
// eat colon
@@ -5935,9 +5912,8 @@ bool AsmParser::parseDirectivePseudoProbe() {
59355912

59365913
int64_t CallerProbeId = 0;
59375914
if (getLexer().is(AsmToken::Integer)) {
5938-
if (parseIntToken(CallerProbeId,
5939-
"unexpected token in '.pseudoprobe' directive"))
5940-
return true;
5915+
CallerProbeId = getTok().getIntVal();
5916+
Lex();
59415917
}
59425918

59435919
InlineSite Site(CallerGuid, CallerProbeId);
@@ -5947,7 +5923,7 @@ bool AsmParser::parseDirectivePseudoProbe() {
59475923
// Parse function entry name
59485924
StringRef FnName;
59495925
if (parseIdentifier(FnName))
5950-
return Error(getLexer().getLoc(), "unexpected token in '.pseudoprobe' directive");
5926+
return Error(getLexer().getLoc(), "expected identifier");
59515927
MCSymbol *FnSym = getContext().lookupSymbol(FnName);
59525928

59535929
if (parseEOL())

llvm/lib/MC/MCParser/MCAsmParserExtension.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ bool MCAsmParserExtension::parseDirectiveCGProfile(StringRef, SMLoc) {
4444
Lex();
4545

4646
int64_t Count;
47-
if (getParser().parseIntToken(
48-
Count, "expected integer count in '.cg_profile' directive"))
47+
if (getParser().parseIntToken(Count))
4948
return true;
5049

5150
if (getLexer().isNot(AsmToken::EndOfStatement))

llvm/test/MC/AsmParser/directive_loc.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RUN: llvm-mc -triple i386-unknown-unknown %s | FileCheck %s
22
# RUN: llvm-mc -triple i386-unknown-unknown %s -filetype=null
3+
# RUN: not llvm-mc -triple i386 --defsym ERR=1 %s 2>&1 | FileCheck %s --check-prefix=ERR
34

45
.file 1 "hello"
56
# CHECK: .file 1 "hello"
@@ -13,3 +14,8 @@
1314
.loc 1 2 0 isa 3
1415
# CHECK: 1 2 0 isa 3
1516
.loc 1 0
17+
18+
.ifdef ERR
19+
# ERR: [[#@LINE+1]]:6: error: expected integer
20+
.loc a
21+
.endif

llvm/test/MC/COFF/cv-errors.s

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@
33
.text
44
foo:
55
.cv_file a
6-
# CHECK: error: expected file number in '.cv_file' directive
6+
# CHECK: error: expected file number
77
# CHECK-NOT: error:
88
.cv_file 0 "t.cpp"
99
# CHECK: error: file number less than one
1010
# CHECK-NOT: error:
1111
.cv_func_id x
12-
# CHECK: error: expected function id in '.cv_func_id' directive
12+
# CHECK: error: expected function id
1313
# CHECK-NOT: error:
1414
.cv_func_id -1
15-
# CHECK: error: expected function id in '.cv_func_id' directive
15+
# CHECK: error: expected function id
1616
# CHECK-NOT: error:
1717
.cv_func_id 0xFFFFFFFFFFFFFFFF
1818
# CHECK: error: expected function id within range [0, UINT_MAX)
1919
# CHECK-NOT: error:
2020
.cv_inline_site_id x
21-
# CHECK: error: expected function id in '.cv_inline_site_id' directive
21+
# CHECK: error: expected function id
2222
# CHECK-NOT: error:
2323

2424
.cv_file 1 "t.cpp"
2525
.cv_func_id 0
2626

2727
.cv_inline_site_id 0 0 0 0 0 0
28-
# CHECK: error: expected 'within' identifier in '.cv_inline_site_id' directive
28+
# CHECK: error: expected 'within' identifier
2929
# CHECK-NOT: error:
3030

3131
.cv_inline_site_id 0 within a
32-
# CHECK: error: expected function id in '.cv_inline_site_id' directive
32+
# CHECK: error: expected function id
3333
# CHECK-NOT: error:
3434

3535
.cv_inline_site_id 0 within 0 x
36-
# CHECK: error: expected 'inlined_at' identifier in '.cv_inline_site_id' directive
36+
# CHECK: error: expected 'inlined_at' identifier
3737
# CHECK-NOT: error:
3838

3939
.cv_inline_site_id 0 within 0 inlined_at 0 0 0
40-
# CHECK: error: file number less than one in '.cv_inline_site_id' directive
40+
# CHECK: error: file number less than one
4141
# CHECK-NOT: error:
4242

4343
.cv_inline_site_id 0 within 0 inlined_at 10 0 0
44-
# CHECK: error: unassigned file number in '.cv_inline_site_id' directive
44+
# CHECK: error: unassigned file number
4545
# CHECK-NOT: error:
4646

4747
.cv_inline_site_id 0 within 0 inlined_at 1 1 1

0 commit comments

Comments
 (0)