Skip to content

Commit 571804b

Browse files
committed
[ASTDumper] print 'trailing_semi' for Expr, Stmt, and Decl if they have valid 'TrailingSemiLoc'
1 parent 6305529 commit 571804b

File tree

2 files changed

+66
-57
lines changed

2 files changed

+66
-57
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ namespace {
409409

410410
if (D->isImplicit())
411411
PrintWithColorRAII(OS, DeclModifierColor) << " implicit";
412+
413+
if (D->TrailingSemiLoc.isValid())
414+
PrintWithColorRAII(OS, DeclModifierColor) << " trailing_semi";
412415
}
413416

414417
void printInherited(ArrayRef<TypeLoc> Inherited) {
@@ -1297,6 +1300,20 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
12971300
}
12981301
}
12991302

1303+
raw_ostream &printCommon(Stmt *S, const char *Name) {
1304+
OS.indent(Indent);
1305+
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1306+
PrintWithColorRAII(OS, StmtColor) << Name;
1307+
1308+
if (S->isImplicit())
1309+
OS << " implicit";
1310+
1311+
if (S->TrailingSemiLoc.isValid())
1312+
OS << " trailing_semi";
1313+
1314+
return OS;
1315+
}
1316+
13001317
void visitBraceStmt(BraceStmt *S) {
13011318
printASTNodes(S->getElements(), "brace_stmt");
13021319
}
@@ -1318,9 +1335,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13181335
}
13191336

13201337
void visitReturnStmt(ReturnStmt *S) {
1321-
OS.indent(Indent);
1322-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1323-
PrintWithColorRAII(OS, StmtColor) << "return_stmt";
1338+
printCommon(S, "return_stmt");
13241339
if (S->hasResult()) {
13251340
OS << '\n';
13261341
printRec(S->getResult());
@@ -1329,19 +1344,15 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13291344
}
13301345

13311346
void visitDeferStmt(DeferStmt *S) {
1332-
OS.indent(Indent);
1333-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1334-
PrintWithColorRAII(OS, StmtColor) << "defer_stmt\n";
1347+
printCommon(S, "defer_stmt") << '\n';
13351348
printRec(S->getTempDecl());
13361349
OS << '\n';
13371350
printRec(S->getCallExpr());
13381351
PrintWithColorRAII(OS, ParenthesisColor) << ')';
13391352
}
13401353

13411354
void visitIfStmt(IfStmt *S) {
1342-
OS.indent(Indent);
1343-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1344-
PrintWithColorRAII(OS, StmtColor) << "if_stmt\n";
1355+
printCommon(S, "if_stmt") << '\n';
13451356
for (auto elt : S->getCond())
13461357
printRec(elt);
13471358
OS << '\n';
@@ -1354,9 +1365,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13541365
}
13551366

13561367
void visitGuardStmt(GuardStmt *S) {
1357-
OS.indent(Indent);
1358-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1359-
PrintWithColorRAII(OS, StmtColor) << "guard_stmt\n";
1368+
printCommon(S, "guard_stmt") << '\n';
13601369
for (auto elt : S->getCond())
13611370
printRec(elt);
13621371
OS << '\n';
@@ -1365,11 +1374,10 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13651374
}
13661375

13671376
void visitIfConfigStmt(IfConfigStmt *S) {
1368-
OS.indent(Indent);
1369-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1370-
PrintWithColorRAII(OS, StmtColor) << "#if_stmt\n";
1377+
printCommon(S, "#if_stmt");
13711378
Indent += 2;
13721379
for (auto &Clause : S->getClauses()) {
1380+
OS << '\n';
13731381
OS.indent(Indent);
13741382
if (Clause.Cond) {
13751383
PrintWithColorRAII(OS, ParenthesisColor) << '(';
@@ -1390,17 +1398,13 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13901398
}
13911399

13921400
void visitDoStmt(DoStmt *S) {
1393-
OS.indent(Indent);
1394-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1395-
PrintWithColorRAII(OS, StmtColor) << "do_stmt\n";
1401+
printCommon(S, "do_stmt") << '\n';
13961402
printRec(S->getBody());
13971403
PrintWithColorRAII(OS, ParenthesisColor) << ')';
13981404
}
13991405

14001406
void visitWhileStmt(WhileStmt *S) {
1401-
OS.indent(Indent);
1402-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1403-
PrintWithColorRAII(OS, StmtColor) << "while_stmt\n";
1407+
printCommon(S, "while_stmt") << '\n';
14041408
for (auto elt : S->getCond())
14051409
printRec(elt);
14061410
OS << '\n';
@@ -1409,18 +1413,14 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14091413
}
14101414

14111415
void visitRepeatWhileStmt(RepeatWhileStmt *S) {
1412-
OS.indent(Indent);
1413-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1414-
PrintWithColorRAII(OS, StmtColor) << "do_while_stmt\n";
1416+
printCommon(S, "repeat_while_stmt") << '\n';
14151417
printRec(S->getBody());
14161418
OS << '\n';
14171419
printRec(S->getCond());
14181420
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14191421
}
14201422
void visitForStmt(ForStmt *S) {
1421-
OS.indent(Indent);
1422-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1423-
PrintWithColorRAII(OS, StmtColor) << "for_stmt\n";
1423+
printCommon(S, "for_stmt") << '\n';
14241424
if (!S->getInitializerVarDecls().empty()) {
14251425
for (auto D : S->getInitializerVarDecls()) {
14261426
printRec(D);
@@ -1449,9 +1449,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14491449
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14501450
}
14511451
void visitForEachStmt(ForEachStmt *S) {
1452-
OS.indent(Indent);
1453-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1454-
OS << "for_each_stmt\n";
1452+
printCommon(S, "for_each_stmt") << '\n';
14551453
printRec(S->getPattern());
14561454
OS << '\n';
14571455
if (S->getWhere()) {
@@ -1477,27 +1475,19 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14771475
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14781476
}
14791477
void visitBreakStmt(BreakStmt *S) {
1480-
OS.indent(Indent);
1481-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1482-
PrintWithColorRAII(OS, StmtColor) << "break_stmt";
1478+
printCommon(S, "break_stmt");
14831479
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14841480
}
14851481
void visitContinueStmt(ContinueStmt *S) {
1486-
OS.indent(Indent);
1487-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1488-
PrintWithColorRAII(OS, StmtColor) << "continue_stmt";
1482+
printCommon(S, "continue_stmt");
14891483
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14901484
}
14911485
void visitFallthroughStmt(FallthroughStmt *S) {
1492-
OS.indent(Indent);
1493-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1494-
PrintWithColorRAII(OS, StmtColor) << "fallthrough_stmt";
1486+
printCommon(S, "fallthrough_stmt");
14951487
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14961488
}
14971489
void visitSwitchStmt(SwitchStmt *S) {
1498-
OS.indent(Indent);
1499-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1500-
PrintWithColorRAII(OS, StmtColor) << "switch_stmt\n";
1490+
printCommon(S, "switch_stmt") << '\n';
15011491
printRec(S->getSubjectExpr());
15021492
for (CaseStmt *C : S->getCases()) {
15031493
OS << '\n';
@@ -1506,9 +1496,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15061496
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15071497
}
15081498
void visitCaseStmt(CaseStmt *S) {
1509-
OS.indent(Indent);
1510-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1511-
PrintWithColorRAII(OS, StmtColor) << "case_stmt";
1499+
printCommon(S, "case_stmt");
15121500
for (const auto &LabelItem : S->getCaseLabelItems()) {
15131501
OS << '\n';
15141502
OS.indent(Indent + 2);
@@ -1529,24 +1517,18 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15291517
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15301518
}
15311519
void visitFailStmt(FailStmt *S) {
1532-
OS.indent(Indent);
1533-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1534-
PrintWithColorRAII(OS, StmtColor) << "fail_stmt";
1520+
printCommon(S, "fail_stmt");
15351521
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15361522
}
15371523

15381524
void visitThrowStmt(ThrowStmt *S) {
1539-
OS.indent(Indent);
1540-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1541-
PrintWithColorRAII(OS, StmtColor) << "throw_stmt\n";
1525+
printCommon(S, "throw_stmt") << '\n';
15421526
printRec(S->getSubExpr());
15431527
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15441528
}
15451529

15461530
void visitDoCatchStmt(DoCatchStmt *S) {
1547-
OS.indent(Indent);
1548-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1549-
PrintWithColorRAII(OS, StmtColor) << "do_catch_stmt\n";
1531+
printCommon(S, "do_catch_stmt") << '\n';
15501532
printRec(S->getBody());
15511533
OS << '\n';
15521534
Indent += 2;
@@ -1560,9 +1542,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15601542
}
15611543
}
15621544
void visitCatchStmt(CatchStmt *clause) {
1563-
OS.indent(Indent);
1564-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1565-
PrintWithColorRAII(OS, StmtColor) << "catch\n";
1545+
printCommon(clause, "catch") << '\n';
15661546
printRec(clause->getErrorPattern());
15671547
if (auto guard = clause->getGuardExpr()) {
15681548
OS << '\n';
@@ -1670,6 +1650,9 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16701650
}
16711651
}
16721652

1653+
if (E->TrailingSemiLoc.isValid())
1654+
OS << " trailing_semi";
1655+
16731656
return OS;
16741657
}
16751658

test/Driver/dump-parse.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,29 @@ func bar() {
2828
// CHECK-AST-NEXT: (declref_expr type='{{[^']+}}' {{.*}} decl=main.(file).foo
2929
foo foo foo
3030
}
31+
32+
// CHECK-LABEL: (enum_decl trailing_semi "TrailingSemi"
33+
enum TrailingSemi {
34+
35+
// CHECK-LABEL: (enum_case_decl trailing_semi
36+
// CHECK-NOT: (enum_element_decl{{.*}}trailing_semi
37+
// CHECK: (enum_element_decl "A")
38+
// CHECK: (enum_element_decl "B")
39+
case A,B;
40+
41+
// CHECK-LABEL: (subscript_decl trailing_semi
42+
// CHECK-NOT: (func_decl trailing_semi 'anonname={{.*}}' getter_for=subscript(_:)
43+
// CHECK: (func_decl 'anonname={{.*}}' getter_for=subscript(_:)
44+
subscript(x: Int) -> Int {
45+
// CHECK-LABEL: (pattern_binding_decl trailing_semi
46+
// CHECK-NOT: (var_decl trailing_semi "y"
47+
// CHECK: (var_decl "y"
48+
var y = 1;
49+
50+
// CHECK-LABEL: (sequence_expr {{.*}} trailing_semi
51+
y += 1;
52+
53+
// CHECK-LABEL: (return_stmt trailing_semi
54+
return y;
55+
};
56+
};

0 commit comments

Comments
 (0)