@@ -425,7 +425,7 @@ namespace {
425
425
426
426
void printRec (Decl *D) { D->dump (OS, Indent + 2 ); }
427
427
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 ); }
429
429
void printRec (TypeRepr *T);
430
430
void printRec (const Pattern *P) {
431
431
PrintPattern (OS, Indent+2 ).visit (const_cast <Pattern *>(P));
@@ -553,7 +553,7 @@ namespace {
553
553
554
554
void printRec (Decl *D) { PrintDecl (OS, Indent + 2 ).visit (D); }
555
555
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 ); }
557
557
void printRec (Pattern *P) { PrintPattern (OS, Indent+2 ).visit (P); }
558
558
void printRec (TypeRepr *T);
559
559
@@ -575,6 +575,13 @@ namespace {
575
575
if (D->isImplicit ())
576
576
PrintWithColorRAII (OS, DeclModifierColor) << " implicit" ;
577
577
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
+
578
585
if (D->TrailingSemiLoc .isValid ())
579
586
PrintWithColorRAII (OS, DeclModifierColor) << " trailing_semi" ;
580
587
}
@@ -984,7 +991,7 @@ namespace {
984
991
PrintWithColorRAII (OS, ParenthesisColor) << ' )' ;
985
992
}
986
993
987
- void printParameterList (const ParameterList *params) {
994
+ void printParameterList (const ParameterList *params, const ASTContext *ctx = nullptr ) {
988
995
OS.indent (Indent);
989
996
PrintWithColorRAII (OS, ParenthesisColor) << ' (' ;
990
997
PrintWithColorRAII (OS, ParameterColor) << " parameter_list" ;
@@ -993,6 +1000,19 @@ namespace {
993
1000
OS << ' \n ' ;
994
1001
printParameter (P);
995
1002
}
1003
+
1004
+ if (!ctx && params->size () != 0 && params->get (0 ))
1005
+ ctx = ¶ms->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
+
996
1016
PrintWithColorRAII (OS, ParenthesisColor) << ' )' ;
997
1017
Indent -= 2 ;
998
1018
}
@@ -1001,7 +1021,7 @@ namespace {
1001
1021
for (auto pl : D->getParameterLists ()) {
1002
1022
OS << ' \n ' ;
1003
1023
Indent += 2 ;
1004
- printParameterList (pl);
1024
+ printParameterList (pl, &D-> getASTContext () );
1005
1025
Indent -= 2 ;
1006
1026
}
1007
1027
if (auto FD = dyn_cast<FuncDecl>(D)) {
@@ -1018,7 +1038,7 @@ namespace {
1018
1038
}
1019
1039
if (auto Body = D->getBody (/* canSynthesize=*/ false )) {
1020
1040
OS << ' \n ' ;
1021
- printRec (Body);
1041
+ printRec (Body, D-> getASTContext () );
1022
1042
}
1023
1043
}
1024
1044
@@ -1065,12 +1085,12 @@ namespace {
1065
1085
printCommon (TLCD, " top_level_code_decl" );
1066
1086
if (TLCD->getBody ()) {
1067
1087
OS << " \n " ;
1068
- printRec (TLCD->getBody ());
1088
+ printRec (TLCD->getBody (), static_cast <Decl *>(TLCD)-> getASTContext () );
1069
1089
}
1070
1090
PrintWithColorRAII (OS, ParenthesisColor) << ' )' ;
1071
1091
}
1072
1092
1073
- void printASTNodes (const ArrayRef<ASTNode> &Elements, StringRef Name) {
1093
+ void printASTNodes (const ArrayRef<ASTNode> &Elements, const ASTContext &Ctx, StringRef Name) {
1074
1094
OS.indent (Indent);
1075
1095
PrintWithColorRAII (OS, ParenthesisColor) << " (" ;
1076
1096
PrintWithColorRAII (OS, ASTNodeColor) << Name;
@@ -1079,7 +1099,7 @@ namespace {
1079
1099
if (auto *SubExpr = Elt.dyn_cast <Expr*>())
1080
1100
printRec (SubExpr);
1081
1101
else if (auto *SubStmt = Elt.dyn_cast <Stmt*>())
1082
- printRec (SubStmt);
1102
+ printRec (SubStmt, Ctx );
1083
1103
else
1084
1104
printRec (Elt.get <Decl*>());
1085
1105
}
@@ -1102,7 +1122,7 @@ namespace {
1102
1122
1103
1123
OS << ' \n ' ;
1104
1124
Indent += 2 ;
1105
- printASTNodes (Clause.Elements , " elements" );
1125
+ printASTNodes (Clause.Elements , ICD-> getASTContext (), " elements" );
1106
1126
Indent -= 2 ;
1107
1127
}
1108
1128
@@ -1349,9 +1369,11 @@ namespace {
1349
1369
class PrintStmt : public StmtVisitor <PrintStmt> {
1350
1370
public:
1351
1371
raw_ostream &OS;
1372
+ const ASTContext *Ctx;
1352
1373
unsigned Indent;
1353
1374
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) {
1355
1377
}
1356
1378
1357
1379
void printRec (Stmt *S) {
@@ -1418,20 +1440,28 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
1418
1440
if (S->isImplicit ())
1419
1441
OS << " implicit" ;
1420
1442
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
+
1421
1452
if (S->TrailingSemiLoc .isValid ())
1422
1453
OS << " trailing_semi" ;
1423
1454
1424
1455
return OS;
1425
1456
}
1426
1457
1427
1458
void visitBraceStmt (BraceStmt *S) {
1428
- printASTNodes (S->getElements (), " brace_stmt" );
1459
+ printCommon (S, " brace_stmt" );
1460
+ printASTNodes (S->getElements ());
1461
+ PrintWithColorRAII (OS, ParenthesisColor) << ' )' ;
1429
1462
}
1430
1463
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) {
1435
1465
for (auto Elt : Elements) {
1436
1466
OS << ' \n ' ;
1437
1467
if (auto *SubExpr = Elt.dyn_cast <Expr*>())
@@ -1441,7 +1471,6 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
1441
1471
else
1442
1472
printRec (Elt.get <Decl*>());
1443
1473
}
1444
- PrintWithColorRAII (OS, ParenthesisColor) << ' )' ;
1445
1474
}
1446
1475
1447
1476
void visitReturnStmt (ReturnStmt *S) {
@@ -1625,8 +1654,8 @@ void Stmt::dump() const {
1625
1654
llvm::errs () << ' \n ' ;
1626
1655
}
1627
1656
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 ));
1630
1659
}
1631
1660
1632
1661
// ===----------------------------------------------------------------------===//
@@ -1671,7 +1700,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
1671
1700
// / FIXME: This should use ExprWalker to print children.
1672
1701
1673
1702
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 ); }
1675
1704
void printRec (const Pattern *P) {
1676
1705
PrintPattern (OS, Indent+2 ).visit (const_cast <Pattern *>(P));
1677
1706
}
@@ -2274,14 +2303,14 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
2274
2303
2275
2304
if (E->getParameters ()) {
2276
2305
OS << ' \n ' ;
2277
- PrintDecl (OS, Indent+2 ).printParameterList (E->getParameters ());
2306
+ PrintDecl (OS, Indent+2 ).printParameterList (E->getParameters (), &E-> getASTContext () );
2278
2307
}
2279
2308
2280
2309
OS << ' \n ' ;
2281
2310
if (E->hasSingleExpressionBody ()) {
2282
2311
printRec (E->getSingleExpressionBody ());
2283
2312
} else {
2284
- printRec (E->getBody ());
2313
+ printRec (E->getBody (), E-> getASTContext () );
2285
2314
}
2286
2315
PrintWithColorRAII (OS, ParenthesisColor) << ' )' ;
2287
2316
}
@@ -2290,7 +2319,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
2290
2319
2291
2320
if (E->getParameters ()) {
2292
2321
OS << ' \n ' ;
2293
- PrintDecl (OS, Indent+2 ).printParameterList (E->getParameters ());
2322
+ PrintDecl (OS, Indent+2 ).printParameterList (E->getParameters (), &E-> getASTContext () );
2294
2323
}
2295
2324
2296
2325
OS << ' \n ' ;
0 commit comments