Skip to content

Commit c58dcfc

Browse files
committed
Include source ranges for statements in the AST dump.
1 parent 46f95d6 commit c58dcfc

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
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 * = 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: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ namespace {
424424

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

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

@@ -1037,7 +1037,7 @@ namespace {
10371037
}
10381038
if (auto Body = D->getBody(/*canSynthesize=*/false)) {
10391039
OS << '\n';
1040-
printRec(Body);
1040+
printRec(Body, D->getASTContext());
10411041
}
10421042
}
10431043

@@ -1084,12 +1084,12 @@ namespace {
10841084
printCommon(TLCD, "top_level_code_decl");
10851085
if (TLCD->getBody()) {
10861086
OS << "\n";
1087-
printRec(TLCD->getBody());
1087+
printRec(TLCD->getBody(), static_cast<Decl *>(TLCD)->getASTContext());
10881088
}
10891089
PrintWithColorRAII(OS, ParenthesisColor) << ')';
10901090
}
10911091

1092-
void printASTNodes(const ArrayRef<ASTNode> &Elements, StringRef Name) {
1092+
void printASTNodes(const ArrayRef<ASTNode> &Elements, const ASTContext &Ctx, StringRef Name) {
10931093
OS.indent(Indent);
10941094
PrintWithColorRAII(OS, ParenthesisColor) << "(";
10951095
PrintWithColorRAII(OS, ASTNodeColor) << Name;
@@ -1098,7 +1098,7 @@ namespace {
10981098
if (auto *SubExpr = Elt.dyn_cast<Expr*>())
10991099
printRec(SubExpr);
11001100
else if (auto *SubStmt = Elt.dyn_cast<Stmt*>())
1101-
printRec(SubStmt);
1101+
printRec(SubStmt, Ctx);
11021102
else
11031103
printRec(Elt.get<Decl*>());
11041104
}
@@ -1121,7 +1121,7 @@ namespace {
11211121

11221122
OS << '\n';
11231123
Indent += 2;
1124-
printASTNodes(Clause.Elements, "elements");
1124+
printASTNodes(Clause.Elements, ICD->getASTContext(), "elements");
11251125
Indent -= 2;
11261126
}
11271127

@@ -1368,9 +1368,11 @@ namespace {
13681368
class PrintStmt : public StmtVisitor<PrintStmt> {
13691369
public:
13701370
raw_ostream &OS;
1371+
const ASTContext *Ctx;
13711372
unsigned Indent;
13721373

1373-
PrintStmt(raw_ostream &os, unsigned indent) : OS(os), Indent(indent) {
1374+
PrintStmt(raw_ostream &os, const ASTContext *ctx, unsigned indent)
1375+
: OS(os), Ctx(ctx), Indent(indent) {
13741376
}
13751377

13761378
void printRec(Stmt *S) {
@@ -1437,20 +1439,28 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14371439
if (S->isImplicit())
14381440
OS << " implicit";
14391441

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

14431454
return OS;
14441455
}
14451456

14461457
void visitBraceStmt(BraceStmt *S) {
1447-
printASTNodes(S->getElements(), "brace_stmt");
1458+
printCommon(S, "brace_stmt");
1459+
printASTNodes(S->getElements());
1460+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14481461
}
14491462

1450-
void printASTNodes(const ArrayRef<ASTNode> &Elements, StringRef Name) {
1451-
OS.indent(Indent);
1452-
PrintWithColorRAII(OS, ParenthesisColor) << "(";
1453-
PrintWithColorRAII(OS, ASTNodeColor) << Name;
1463+
void printASTNodes(const ArrayRef<ASTNode> &Elements) {
14541464
for (auto Elt : Elements) {
14551465
OS << '\n';
14561466
if (auto *SubExpr = Elt.dyn_cast<Expr*>())
@@ -1460,7 +1470,6 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14601470
else
14611471
printRec(Elt.get<Decl*>());
14621472
}
1463-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14641473
}
14651474

14661475
void visitReturnStmt(ReturnStmt *S) {
@@ -1644,8 +1653,8 @@ void Stmt::dump() const {
16441653
llvm::errs() << '\n';
16451654
}
16461655

1647-
void Stmt::print(raw_ostream &OS, unsigned Indent) const {
1648-
PrintStmt(OS, Indent).visit(const_cast<Stmt*>(this));
1656+
void Stmt::print(raw_ostream &OS, const ASTContext *Ctx, unsigned Indent) const {
1657+
PrintStmt(OS, Ctx, Indent).visit(const_cast<Stmt*>(this));
16491658
}
16501659

16511660
//===----------------------------------------------------------------------===//
@@ -1690,7 +1699,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16901699
/// FIXME: This should use ExprWalker to print children.
16911700

16921701
void printRec(Decl *D) { D->dump(OS, Indent + 2); }
1693-
void printRec(Stmt *S) { S->print(OS, Indent + 2); }
1702+
void printRec(Stmt *S, const ASTContext& Ctx) { S->print(OS, &Ctx, Indent + 2); }
16941703
void printRec(const Pattern *P) {
16951704
PrintPattern(OS, Indent+2).visit(const_cast<Pattern *>(P));
16961705
}
@@ -2300,7 +2309,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
23002309
if (E->hasSingleExpressionBody()) {
23012310
printRec(E->getSingleExpressionBody());
23022311
} else {
2303-
printRec(E->getBody());
2312+
printRec(E->getBody(), E->getASTContext());
23042313
}
23052314
PrintWithColorRAII(OS, ParenthesisColor) << ')';
23062315
}

test/Driver/dump-parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ enum TrailingSemi {
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
};

0 commit comments

Comments
 (0)