@@ -654,6 +654,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
654654
655655 void printAttributes (const Decl *D);
656656 void printTypedPattern (const TypedPattern *TP);
657+ void printBraceStmt (const BraceStmt *stmt, bool newlineIfEmpty = true );
657658
658659public:
659660 void printPattern (const Pattern *pattern);
@@ -695,6 +696,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
695696 void printGenericDeclGenericParams (GenericContext *decl);
696697 void printGenericDeclGenericRequirements (GenericContext *decl);
697698 void printInherited (const Decl *decl);
699+ void printBodyIfNecessary (const AbstractFunctionDecl *decl);
698700
699701 void printEnumElement (EnumElementDecl *elt);
700702
@@ -1480,6 +1482,29 @@ bool PrintAST::shouldPrint(const Decl *D, bool Notify) {
14801482 return Result;
14811483}
14821484
1485+ void PrintAST::printBraceStmt (const BraceStmt *stmt, bool newlineIfEmpty) {
1486+ Printer << " {" ;
1487+ if (printASTNodes (stmt->getElements ()) || newlineIfEmpty) {
1488+ Printer.printNewline ();
1489+ indent ();
1490+ }
1491+ Printer << " }" ;
1492+ }
1493+
1494+ void PrintAST::printBodyIfNecessary (const AbstractFunctionDecl *decl) {
1495+ if (auto BodyFunc = Options.FunctionBody ) {
1496+ BodyFunc (decl, Printer);
1497+ indent ();
1498+ return ;
1499+ }
1500+
1501+ if (!Options.FunctionDefinitions || !decl->getBody ())
1502+ return ;
1503+
1504+ Printer << " " ;
1505+ printBraceStmt (decl->getBody (), /* newlineIfEmpty*/ !isa<AccessorDecl>(decl));
1506+ }
1507+
14831508static bool isAccessorAssumedNonMutating (AccessorKind kind) {
14841509 switch (kind) {
14851510 case AccessorKind::Get:
@@ -2540,19 +2565,7 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
25402565 });
25412566 }
25422567
2543- if (auto BodyFunc = Options.FunctionBody ) {
2544- BodyFunc (decl, Printer);
2545- indent ();
2546- return ;
2547- }
2548-
2549- Printer << " {" ;
2550- if (Options.FunctionDefinitions && decl->getBody ())
2551- if (printASTNodes (decl->getBody ()->getElements ())) {
2552- Printer.printNewline ();
2553- indent ();
2554- }
2555- Printer << " }" ;
2568+ printBodyIfNecessary (decl);
25562569}
25572570
25582571void PrintAST::visitFuncDecl (FuncDecl *decl) {
@@ -2624,13 +2637,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26242637 printGenericDeclGenericRequirements (decl);
26252638 }
26262639
2627- if (auto BodyFunc = Options.FunctionBody ) {
2628- BodyFunc (decl, Printer);
2629- indent ();
2630- } else if (Options.FunctionDefinitions && decl->getBody ()) {
2631- Printer << " " ;
2632- visit (decl->getBody ());
2633- }
2640+ printBodyIfNecessary (decl);
26342641}
26352642
26362643void PrintAST::printEnumElement (EnumElementDecl *elt) {
@@ -2799,13 +2806,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
27992806
28002807 printGenericDeclGenericRequirements (decl);
28012808
2802- if (auto BodyFunc = Options.FunctionBody ) {
2803- BodyFunc (decl, Printer);
2804- indent ();
2805- } else if (Options.FunctionDefinitions && decl->getBody ()) {
2806- Printer << " " ;
2807- visit (decl->getBody ());
2808- }
2809+ printBodyIfNecessary (decl);
28092810}
28102811
28112812void PrintAST::visitDestructorDecl (DestructorDecl *decl) {
@@ -2817,12 +2818,7 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
28172818 Printer << " deinit" ;
28182819 });
28192820
2820- if (!Options.FunctionDefinitions || !decl->getBody ()) {
2821- return ;
2822- }
2823-
2824- Printer << " " ;
2825- visit (decl->getBody ());
2821+ printBodyIfNecessary (decl);
28262822}
28272823
28282824void PrintAST::visitInfixOperatorDecl (InfixOperatorDecl *decl) {
@@ -2932,11 +2928,7 @@ void PrintAST::visitMissingMemberDecl(MissingMemberDecl *decl) {
29322928}
29332929
29342930void PrintAST::visitBraceStmt (BraceStmt *stmt) {
2935- Printer << " {" ;
2936- printASTNodes (stmt->getElements ());
2937- Printer.printNewline ();
2938- indent ();
2939- Printer << " }" ;
2931+ printBraceStmt (stmt);
29402932}
29412933
29422934void PrintAST::visitReturnStmt (ReturnStmt *stmt) {
0 commit comments