Skip to content

Commit a675947

Browse files
author
Paul C. Anagnostopoulos
committed
[TableGen] Improve error message for semicolon after braced body.
Add a test for this message. Differential Revision: https://reviews.llvm.org/D94412
1 parent 80f0785 commit a675947

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

llvm/lib/TableGen/TGParser.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,7 +2836,7 @@ bool TGParser::ParseBody(Record *CurRec) {
28362836
return false;
28372837

28382838
if (!consume(tgtok::l_brace))
2839-
return TokError("Expected ';' or '{' to start body");
2839+
return TokError("Expected '{' to start body or ';' for declaration only");
28402840

28412841
// An object body introduces a new scope for local variables.
28422842
TGLocalVarScope *BodyScope = PushLocalScope();
@@ -2849,6 +2849,14 @@ bool TGParser::ParseBody(Record *CurRec) {
28492849

28502850
// Eat the '}'.
28512851
Lex.Lex();
2852+
2853+
// If we have a semicolon, print a gentle error.
2854+
SMLoc SemiLoc = Lex.getLoc();
2855+
if (consume(tgtok::semi)) {
2856+
PrintError(SemiLoc, "A class or def body should not end with a semicolon");
2857+
PrintNote("Semicolon ignored; remove to eliminate this error");
2858+
}
2859+
28522860
return false;
28532861
}
28542862

@@ -3432,6 +3440,13 @@ bool TGParser::ParseMultiClass() {
34323440
}
34333441
Lex.Lex(); // eat the '}'.
34343442

3443+
// If we have a semicolon, print a gentle error.
3444+
SMLoc SemiLoc = Lex.getLoc();
3445+
if (consume(tgtok::semi)) {
3446+
PrintError(SemiLoc, "A multiclass body should not end with a semicolon");
3447+
PrintNote("Semicolon ignored; remove to eliminate this error");
3448+
}
3449+
34353450
PopLocalScope(MulticlassScope);
34363451
}
34373452

@@ -3623,7 +3638,7 @@ bool TGParser::ParseFile() {
36233638
if (Lex.getCode() == tgtok::Eof)
36243639
return false;
36253640

3626-
return TokError("Unexpected input at top level");
3641+
return TokError("Unexpected token at top level");
36273642
}
36283643

36293644
// Check an assertion: Obtain the condition value and be sure it is true.

llvm/test/TableGen/spurious-semi.td

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: llvm-tblgen %s | FileCheck %s
2+
// RUN: not llvm-tblgen -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
3+
4+
// This file tests the error message that is printed when a body is
5+
// terminated with a semicolon in addition to the close brace.
6+
7+
// CHECK: class Class0
8+
// CHECK: def Rec0
9+
10+
class Class0 {
11+
}
12+
13+
def Rec0 {
14+
}
15+
16+
multiclass MC0 {
17+
def R;
18+
}
19+
20+
#ifdef ERROR1
21+
22+
// ERROR1: error: A class or def body should not end with a semicolon
23+
// ERROR1: Semicolon ignored
24+
// ERROR1: error: A class or def body should not end with a semicolon
25+
// ERROR1: Semicolon ignored
26+
// ERROR1: error: A multiclass body should not end with a semicolon
27+
// ERROR1: Semicolon ignored
28+
29+
class Class1 {
30+
};
31+
32+
def Rec1 {
33+
};
34+
35+
multiclass MC1 {
36+
def R;
37+
};
38+
39+
#endif

0 commit comments

Comments
 (0)