Skip to content

Commit cb6c8f0

Browse files
authored
Merge pull request #16473 from bdash/ast-dump-source-ranges
Include source ranges for statements, declarations, and parameter lists in the AST dump
2 parents d224db8 + 4ba6dd3 commit cb6c8f0

15 files changed

+126
-97
lines changed

include/swift/AST/Stmt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class alignas(8) Stmt {
128128
LLVM_ATTRIBUTE_DEPRECATED(
129129
void dump() const LLVM_ATTRIBUTE_USED,
130130
"only for use within the debugger");
131-
void print(raw_ostream &OS, unsigned Indent = 0) const;
131+
void print(raw_ostream &OS, const ASTContext *Ctx = nullptr, unsigned Indent = 0) const;
132132

133133
// Only allow allocation of Exprs using the allocator in ASTContext
134134
// or by doing a placement new.

lib/AST/ASTDumper.cpp

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ namespace {
425425

426426
void printRec(Decl *D) { D->dump(OS, Indent + 2); }
427427
void printRec(Expr *E) { E->print(OS, Indent + 2); }
428-
void printRec(Stmt *S) { S->print(OS, Indent + 2); }
428+
void printRec(Stmt *S, const ASTContext &Ctx) { S->print(OS, &Ctx, Indent + 2); }
429429
void printRec(TypeRepr *T);
430430
void printRec(const Pattern *P) {
431431
PrintPattern(OS, Indent+2).visit(const_cast<Pattern *>(P));
@@ -553,7 +553,7 @@ namespace {
553553

554554
void printRec(Decl *D) { PrintDecl(OS, Indent + 2).visit(D); }
555555
void printRec(Expr *E) { E->print(OS, Indent+2); }
556-
void printRec(Stmt *S) { S->print(OS, Indent+2); }
556+
void printRec(Stmt *S, const ASTContext &Ctx) { S->print(OS, &Ctx, Indent+2); }
557557
void printRec(Pattern *P) { PrintPattern(OS, Indent+2).visit(P); }
558558
void printRec(TypeRepr *T);
559559

@@ -575,6 +575,13 @@ namespace {
575575
if (D->isImplicit())
576576
PrintWithColorRAII(OS, DeclModifierColor) << " implicit";
577577

578+
auto R = D->getSourceRange();
579+
if (R.isValid()) {
580+
PrintWithColorRAII(OS, RangeColor) << " range=";
581+
R.print(PrintWithColorRAII(OS, RangeColor).getOS(),
582+
D->getASTContext().SourceMgr, /*PrintText=*/false);
583+
}
584+
578585
if (D->TrailingSemiLoc.isValid())
579586
PrintWithColorRAII(OS, DeclModifierColor) << " trailing_semi";
580587
}
@@ -984,7 +991,7 @@ namespace {
984991
PrintWithColorRAII(OS, ParenthesisColor) << ')';
985992
}
986993

987-
void printParameterList(const ParameterList *params) {
994+
void printParameterList(const ParameterList *params, const ASTContext *ctx = nullptr) {
988995
OS.indent(Indent);
989996
PrintWithColorRAII(OS, ParenthesisColor) << '(';
990997
PrintWithColorRAII(OS, ParameterColor) << "parameter_list";
@@ -993,6 +1000,19 @@ namespace {
9931000
OS << '\n';
9941001
printParameter(P);
9951002
}
1003+
1004+
if (!ctx && params->size() != 0 && params->get(0))
1005+
ctx = &params->get(0)->getASTContext();
1006+
1007+
if (ctx) {
1008+
auto R = params->getSourceRange();
1009+
if (R.isValid()) {
1010+
PrintWithColorRAII(OS, RangeColor) << " range=";
1011+
R.print(PrintWithColorRAII(OS, RangeColor).getOS(),
1012+
ctx->SourceMgr, /*PrintText=*/false);
1013+
}
1014+
}
1015+
9961016
PrintWithColorRAII(OS, ParenthesisColor) << ')';
9971017
Indent -= 2;
9981018
}
@@ -1001,7 +1021,7 @@ namespace {
10011021
for (auto pl : D->getParameterLists()) {
10021022
OS << '\n';
10031023
Indent += 2;
1004-
printParameterList(pl);
1024+
printParameterList(pl, &D->getASTContext());
10051025
Indent -= 2;
10061026
}
10071027
if (auto FD = dyn_cast<FuncDecl>(D)) {
@@ -1018,7 +1038,7 @@ namespace {
10181038
}
10191039
if (auto Body = D->getBody(/*canSynthesize=*/false)) {
10201040
OS << '\n';
1021-
printRec(Body);
1041+
printRec(Body, D->getASTContext());
10221042
}
10231043
}
10241044

@@ -1065,12 +1085,12 @@ namespace {
10651085
printCommon(TLCD, "top_level_code_decl");
10661086
if (TLCD->getBody()) {
10671087
OS << "\n";
1068-
printRec(TLCD->getBody());
1088+
printRec(TLCD->getBody(), static_cast<Decl *>(TLCD)->getASTContext());
10691089
}
10701090
PrintWithColorRAII(OS, ParenthesisColor) << ')';
10711091
}
10721092

1073-
void printASTNodes(const ArrayRef<ASTNode> &Elements, StringRef Name) {
1093+
void printASTNodes(const ArrayRef<ASTNode> &Elements, const ASTContext &Ctx, StringRef Name) {
10741094
OS.indent(Indent);
10751095
PrintWithColorRAII(OS, ParenthesisColor) << "(";
10761096
PrintWithColorRAII(OS, ASTNodeColor) << Name;
@@ -1079,7 +1099,7 @@ namespace {
10791099
if (auto *SubExpr = Elt.dyn_cast<Expr*>())
10801100
printRec(SubExpr);
10811101
else if (auto *SubStmt = Elt.dyn_cast<Stmt*>())
1082-
printRec(SubStmt);
1102+
printRec(SubStmt, Ctx);
10831103
else
10841104
printRec(Elt.get<Decl*>());
10851105
}
@@ -1102,7 +1122,7 @@ namespace {
11021122

11031123
OS << '\n';
11041124
Indent += 2;
1105-
printASTNodes(Clause.Elements, "elements");
1125+
printASTNodes(Clause.Elements, ICD->getASTContext(), "elements");
11061126
Indent -= 2;
11071127
}
11081128

@@ -1349,9 +1369,11 @@ namespace {
13491369
class PrintStmt : public StmtVisitor<PrintStmt> {
13501370
public:
13511371
raw_ostream &OS;
1372+
const ASTContext *Ctx;
13521373
unsigned Indent;
13531374

1354-
PrintStmt(raw_ostream &os, unsigned indent) : OS(os), Indent(indent) {
1375+
PrintStmt(raw_ostream &os, const ASTContext *ctx, unsigned indent)
1376+
: OS(os), Ctx(ctx), Indent(indent) {
13551377
}
13561378

13571379
void printRec(Stmt *S) {
@@ -1418,20 +1440,28 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14181440
if (S->isImplicit())
14191441
OS << " implicit";
14201442

1443+
if (Ctx) {
1444+
auto R = S->getSourceRange();
1445+
if (R.isValid()) {
1446+
PrintWithColorRAII(OS, RangeColor) << " range=";
1447+
R.print(PrintWithColorRAII(OS, RangeColor).getOS(),
1448+
Ctx->SourceMgr, /*PrintText=*/false);
1449+
}
1450+
}
1451+
14211452
if (S->TrailingSemiLoc.isValid())
14221453
OS << " trailing_semi";
14231454

14241455
return OS;
14251456
}
14261457

14271458
void visitBraceStmt(BraceStmt *S) {
1428-
printASTNodes(S->getElements(), "brace_stmt");
1459+
printCommon(S, "brace_stmt");
1460+
printASTNodes(S->getElements());
1461+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14291462
}
14301463

1431-
void printASTNodes(const ArrayRef<ASTNode> &Elements, StringRef Name) {
1432-
OS.indent(Indent);
1433-
PrintWithColorRAII(OS, ParenthesisColor) << "(";
1434-
PrintWithColorRAII(OS, ASTNodeColor) << Name;
1464+
void printASTNodes(const ArrayRef<ASTNode> &Elements) {
14351465
for (auto Elt : Elements) {
14361466
OS << '\n';
14371467
if (auto *SubExpr = Elt.dyn_cast<Expr*>())
@@ -1441,7 +1471,6 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14411471
else
14421472
printRec(Elt.get<Decl*>());
14431473
}
1444-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14451474
}
14461475

14471476
void visitReturnStmt(ReturnStmt *S) {
@@ -1625,8 +1654,8 @@ void Stmt::dump() const {
16251654
llvm::errs() << '\n';
16261655
}
16271656

1628-
void Stmt::print(raw_ostream &OS, unsigned Indent) const {
1629-
PrintStmt(OS, Indent).visit(const_cast<Stmt*>(this));
1657+
void Stmt::print(raw_ostream &OS, const ASTContext *Ctx, unsigned Indent) const {
1658+
PrintStmt(OS, Ctx, Indent).visit(const_cast<Stmt*>(this));
16301659
}
16311660

16321661
//===----------------------------------------------------------------------===//
@@ -1671,7 +1700,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16711700
/// FIXME: This should use ExprWalker to print children.
16721701

16731702
void printRec(Decl *D) { D->dump(OS, Indent + 2); }
1674-
void printRec(Stmt *S) { S->print(OS, Indent + 2); }
1703+
void printRec(Stmt *S, const ASTContext &Ctx) { S->print(OS, &Ctx, Indent + 2); }
16751704
void printRec(const Pattern *P) {
16761705
PrintPattern(OS, Indent+2).visit(const_cast<Pattern *>(P));
16771706
}
@@ -2274,14 +2303,14 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
22742303

22752304
if (E->getParameters()) {
22762305
OS << '\n';
2277-
PrintDecl(OS, Indent+2).printParameterList(E->getParameters());
2306+
PrintDecl(OS, Indent+2).printParameterList(E->getParameters(), &E->getASTContext());
22782307
}
22792308

22802309
OS << '\n';
22812310
if (E->hasSingleExpressionBody()) {
22822311
printRec(E->getSingleExpressionBody());
22832312
} else {
2284-
printRec(E->getBody());
2313+
printRec(E->getBody(), E->getASTContext());
22852314
}
22862315
PrintWithColorRAII(OS, ParenthesisColor) << ')';
22872316
}
@@ -2290,7 +2319,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
22902319

22912320
if (E->getParameters()) {
22922321
OS << '\n';
2293-
PrintDecl(OS, Indent+2).printParameterList(E->getParameters());
2322+
PrintDecl(OS, Indent+2).printParameterList(E->getParameters(), &E->getASTContext());
22942323
}
22952324

22962325
OS << '\n';

test/Driver/dump-parse.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: not %target-swift-frontend -dump-parse %s 2>&1 | %FileCheck %s
22
// RUN: not %target-swift-frontend -dump-ast %s 2>&1 | %FileCheck %s -check-prefix=CHECK-AST
33

4-
// CHECK-LABEL: (func_decl "foo(_:)"
5-
// CHECK-AST-LABEL: (func_decl "foo(_:)"
4+
// CHECK-LABEL: (func_decl{{.*}}"foo(_:)"
5+
// CHECK-AST-LABEL: (func_decl{{.*}}"foo(_:)"
66
func foo(_ n: Int) -> Int {
77
// CHECK: (brace_stmt
88
// CHECK: (return_stmt
@@ -15,8 +15,8 @@ func foo(_ n: Int) -> Int {
1515
}
1616

1717
// -dump-parse should print an AST even though this code is invalid.
18-
// CHECK-LABEL: (func_decl "bar()"
19-
// CHECK-AST-LABEL: (func_decl "bar()"
18+
// CHECK-LABEL: (func_decl{{.*}}"bar()"
19+
// CHECK-AST-LABEL: (func_decl{{.*}}"bar()"
2020
func bar() {
2121
// CHECK: (brace_stmt
2222
// CHECK-NEXT: (unresolved_decl_ref_expr type='{{[^']+}}' name=foo
@@ -29,28 +29,28 @@ func bar() {
2929
foo foo foo
3030
}
3131

32-
// CHECK-LABEL: (enum_decl trailing_semi "TrailingSemi"
32+
// CHECK-LABEL: (enum_decl{{.*}}trailing_semi "TrailingSemi"
3333
enum TrailingSemi {
3434

35-
// CHECK-LABEL: (enum_case_decl trailing_semi
35+
// CHECK-LABEL: (enum_case_decl{{.*}}trailing_semi
3636
// CHECK-NOT: (enum_element_decl{{.*}}trailing_semi
37-
// CHECK: (enum_element_decl "A")
38-
// CHECK: (enum_element_decl "B")
37+
// CHECK: (enum_element_decl{{.*}}"A")
38+
// CHECK: (enum_element_decl{{.*}}"B")
3939
case A,B;
4040

41-
// CHECK-LABEL: (subscript_decl trailing_semi
42-
// CHECK-NOT: (func_decl trailing_semi 'anonname={{.*}}' getter_for=subscript(_:)
43-
// CHECK: (accessor_decl 'anonname={{.*}}' getter_for=subscript(_:)
41+
// CHECK-LABEL: (subscript_decl{{.*}}trailing_semi
42+
// CHECK-NOT: (func_decl{{.*}}trailing_semi 'anonname={{.*}}' getter_for=subscript(_:)
43+
// CHECK: (accessor_decl{{.*}}'anonname={{.*}}' getter_for=subscript(_:)
4444
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"
45+
// CHECK-LABEL: (pattern_binding_decl{{.*}}trailing_semi
46+
// CHECK-NOT: (var_decl{{.*}}trailing_semi "y"
47+
// CHECK: (var_decl{{.*}}"y"
4848
var y = 1;
4949

5050
// CHECK-LABEL: (sequence_expr {{.*}} trailing_semi
5151
y += 1;
5252

53-
// CHECK-LABEL: (return_stmt trailing_semi
53+
// CHECK-LABEL: (return_stmt{{.*}}trailing_semi
5454
return y;
5555
};
5656
};

test/NameBinding/import-resolution-overload.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension HasFooSub {
5555
var foo: Int { return 0 }
5656
}
5757

58-
// CHECK-LABEL: func_decl "testHasFooSub(_:)"
58+
// CHECK-LABEL: func_decl{{.*}}"testHasFooSub(_:)"
5959
func testHasFooSub(_ hfs: HasFooSub) -> Int {
6060
// CHECK: return_stmt
6161
// CHECK-NOT: func_decl
@@ -67,7 +67,7 @@ extension HasBar {
6767
var bar: Int { return 0 }
6868
}
6969

70-
// CHECK-LABEL: func_decl "testHasBar(_:)"
70+
// CHECK-LABEL: func_decl{{.*}}"testHasBar(_:)"
7171
func testHasBar(_ hb: HasBar) -> Int {
7272
// CHECK: return_stmt
7373
// CHECK-NOT: func_decl

test/Parse/if_expr.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-swift-frontend -dump-ast %s 2>&1 | %FileCheck %s
22

3-
// CHECK: (func_decl "r13756261(_:_:)"
3+
// CHECK: (func_decl{{.*}}"r13756261(_:_:)"
44
func r13756261(_ x: Bool, _ y: Int) -> Int {
55
// CHECK: (if_expr
66
// CHECK: (call_expr
@@ -15,7 +15,7 @@ func r13756261(_ x: Bool, _ y: Int) -> Int {
1515
return (x) ? y : (x) ? y : (x) ? y : y
1616
}
1717

18-
// CHECK: (func_decl "r13756221(_:_:)"
18+
// CHECK: (func_decl{{.*}}"r13756221(_:_:)"
1919
func r13756221(_ x: Bool, _ y: Int) -> Int {
2020
// CHECK: (if_expr
2121
// CHECK: (call_expr
@@ -33,7 +33,7 @@ func r13756221(_ x: Bool, _ y: Int) -> Int {
3333
: y
3434
}
3535

36-
// CHECK: (func_decl "telescoping_if(_:_:)"
36+
// CHECK: (func_decl{{.*}}"telescoping_if(_:_:)"
3737
func telescoping_if(_ x: Bool, _ y: Int) -> Int {
3838
// CHECK: (if_expr
3939
// CHECK: (call_expr
@@ -69,7 +69,7 @@ func +>> (x: Bool, y: Bool) -> Bool {}
6969
func +<< (x: Bool, y: Bool) -> Bool {}
7070
func +== (x: Bool, y: Bool) -> Bool {}
7171

72-
// CHECK: (func_decl "prec_above(_:_:_:)"
72+
// CHECK: (func_decl{{.*}}"prec_above(_:_:_:)"
7373
func prec_above(_ x: Bool, _ y: Bool, _ z: Bool) -> Bool {
7474
// (x +>> y) ? (y +>> z) : ((x +>> y) ? (y +>> z) : (x +>> y))
7575
// CHECK: (if_expr
@@ -82,7 +82,7 @@ func prec_above(_ x: Bool, _ y: Bool, _ z: Bool) -> Bool {
8282
return x +>> y ? y +>> z : x +>> y ? y +>> z : x +>> y
8383
}
8484

85-
// CHECK: (func_decl "prec_below(_:_:_:)"
85+
// CHECK: (func_decl{{.*}}"prec_below(_:_:_:)"
8686
func prec_below(_ x: Bool, _ y: Bool, _ z: Bool) -> Bool {
8787
// The middle arm of the ternary is max-munched, so this is:
8888
// ((x +<< (y ? (y +<< z) : x)) +<< (y ? (y +<< z) : x)) +<< y
@@ -102,7 +102,7 @@ func prec_below(_ x: Bool, _ y: Bool, _ z: Bool) -> Bool {
102102
return x +<< y ? y +<< z : x +<< y ? y +<< z : x +<< y
103103
}
104104

105-
// CHECK: (func_decl "prec_equal(_:_:_:)"
105+
// CHECK: (func_decl{{.*}}"prec_equal(_:_:_:)"
106106
func prec_equal(_ x: Bool, _ y: Bool, _ z: Bool) -> Bool {
107107
// The middle arm of the ternary is max-munched, so this is:
108108
// x +== (y ? (y +== z) : (x +== (y ? (y +== z) : (x +== y))))

0 commit comments

Comments
 (0)