Skip to content

Commit 0afadf1

Browse files
committed
[SourceKit] Adjust newlines between decls
Previously, Clang modules didn't have empty lines between top-level decls. This was inconsistent with Swift module. (cherry picked from commit 7b3738d)
1 parent 825a182 commit 0afadf1

File tree

32 files changed

+3416
-3244
lines changed

32 files changed

+3416
-3244
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ struct PrintOptions {
447447
bool PrintInSILBody = false;
448448

449449
/// Whether to use an empty line to separate two members in a single decl.
450-
bool EmptyLineBetweenMembers = false;
450+
bool EmptyLineBetweenDecls = false;
451451

452452
/// Whether to print empty members of a declaration on a single line, e.g.:
453453
/// ```
@@ -692,7 +692,7 @@ struct PrintOptions {
692692
result.SkipUnderscoredStdlibProtocols = true;
693693
result.SkipUnsafeCXXMethods = true;
694694
result.SkipDeinit = true;
695-
result.EmptyLineBetweenMembers = true;
695+
result.EmptyLineBetweenDecls = true;
696696
result.CascadeDocComment = true;
697697
result.ShouldQualifyNestedDeclarations =
698698
QualifyNestedDeclarations::Always;
@@ -755,7 +755,7 @@ struct PrintOptions {
755755
static PrintOptions printSwiftFileInterface(bool printFullConvention) {
756756
PrintOptions result = printInterface(printFullConvention);
757757
result.AccessFilter = AccessLevel::Internal;
758-
result.EmptyLineBetweenMembers = true;
758+
result.EmptyLineBetweenDecls = true;
759759
return result;
760760
}
761761

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
705705
StringRef RawText =
706706
RC->getRawText(ClangContext.getSourceManager()).rtrim("\n\r");
707707
trimLeadingWhitespaceFromLines(RawText, WhitespaceToTrim, Lines);
708+
indent();
708709
bool FirstLine = true;
709710
for (auto Line : Lines) {
710711
if (FirstLine)
@@ -1134,20 +1135,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
11341135
Printer.setSynthesizedTarget(Options.TransformContext->getDecl());
11351136
}
11361137

1137-
// We want to print a newline before doc comments. Swift code already
1138-
// handles this, but we need to insert it for clang doc comments when not
1139-
// printing other clang comments. Do it now so the printDeclPre callback
1140-
// happens after the newline.
1141-
if (Options.PrintDocumentationComments && D->hasClangNode()) {
1142-
auto clangNode = D->getClangNode();
1143-
auto clangDecl = clangNode.getAsDecl();
1144-
if (clangDecl &&
1145-
clangDecl->getASTContext().getRawCommentForAnyRedecl(clangDecl)) {
1146-
Printer.printNewline();
1147-
indent();
1148-
}
1149-
}
1150-
11511138
Printer.callPrintDeclPre(D, Options.BracketOptions);
11521139

11531140
if (Options.PrintCompatibilityFeatureChecks) {
@@ -2724,7 +2711,7 @@ void PrintAST::printMembers(ArrayRef<Decl *> members, bool needComma,
27242711
if (!member->shouldPrintInContext(Options))
27252712
continue;
27262713

2727-
if (Options.EmptyLineBetweenMembers)
2714+
if (Options.EmptyLineBetweenDecls)
27282715
Printer.printNewline();
27292716
indent();
27302717
visit(member);

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ static bool printModuleInterfaceDecl(Decl *D,
167167
};
168168
}
169169
}
170-
if (!LeadingComment.empty() && Options.shouldPrint(D))
171-
Printer << LeadingComment << "\n";
170+
if (!LeadingComment.empty() && Options.shouldPrint(D)) {
171+
Printer << LeadingComment;
172+
Printer.printNewline();
173+
}
172174
if (D->print(Printer, Options)) {
173175
if (Options.BracketOptions.shouldCloseNominal(D))
174-
Printer << "\n";
176+
Printer.printNewline();
175177
Options.BracketOptions = BracketOptions();
176178
if (auto NTD = dyn_cast<NominalTypeDecl>(D)) {
177179
std::queue<NominalTypeDecl *> SubDecls{{NTD}};
@@ -194,11 +196,13 @@ static bool printModuleInterfaceDecl(Decl *D,
194196
}
195197
if (extensionHasClangNode(Ext))
196198
continue; // will be printed in its source location, see above.
197-
Printer << "\n";
198-
if (!LeadingComment.empty())
199-
Printer << LeadingComment << "\n";
199+
Printer.printNewline();
200+
if (!LeadingComment.empty()) {
201+
Printer << LeadingComment;
202+
Printer.printNewline();
203+
}
200204
Ext->print(Printer, Options);
201-
Printer << "\n";
205+
Printer.printNewline();
202206
}
203207
for (auto Sub : Ext->getMembers())
204208
if (auto N = dyn_cast<NominalTypeDecl>(Sub))
@@ -226,7 +230,7 @@ static bool printModuleInterfaceDecl(Decl *D,
226230
if (ET.IsSynthesized)
227231
Options.clearSynthesizedExtension();
228232
if (Options.BracketOptions.shouldCloseExtension(ET.Ext))
229-
Printer << "\n";
233+
Printer.printNewline();
230234
}
231235
});
232236
}
@@ -250,9 +254,11 @@ static bool printModuleInterfaceDecl(Decl *D,
250254
ET.Ext, !Opened, Decls.back().Ext == ET.Ext, true
251255
};
252256
if (Options.BracketOptions.shouldOpenExtension(ET.Ext)) {
253-
Printer << "\n";
254-
if (Options.shouldPrint(ET.Ext) && !LeadingComment.empty())
255-
Printer << LeadingComment << "\n";
257+
Printer.printNewline();
258+
if (Options.shouldPrint(ET.Ext) && !LeadingComment.empty()) {
259+
Printer << LeadingComment;
260+
Printer.printNewline();
261+
}
256262
}
257263
if (ET.IsSynthesized) {
258264
if (ET.EnablingExt)
@@ -265,7 +271,7 @@ static bool printModuleInterfaceDecl(Decl *D,
265271
if (ET.IsSynthesized)
266272
Options.clearSynthesizedExtension();
267273
if (Options.BracketOptions.shouldCloseExtension(ET.Ext)) {
268-
Printer << "\n";
274+
Printer.printNewline();
269275
}
270276
}
271277
});
@@ -411,18 +417,22 @@ static void printCrossImportOverlays(ModuleDecl *Declaring, ASTContext &Ctx,
411417
BystanderList += Bystanders[I].str();
412418
}
413419

414-
Printer << "\n// MARK: - " << BystanderList << " Additions\n\n";
420+
Printer.printNewline();
421+
Printer << "// MARK: - " << BystanderList << " Additions";
422+
Printer.printNewline();
423+
Printer.printNewline();
415424
for (auto *Import : DeclLists.first)
416425
PrintDecl(Import);
417-
Printer << "\n";
426+
if (!DeclLists.first.empty())
427+
Printer.printNewline();
418428

419429
std::string PerDeclComment = "// Available when " + BystanderList;
420430
PerDeclComment += Bystanders.size() == 1 ? " is" : " are";
421431
PerDeclComment += " imported with " + Declaring->getNameStr().str();
422432

423433
for (auto *D : DeclLists.second) {
424-
if (PrintDecl(D, PerDeclComment))
425-
Printer << "\n";
434+
if (PrintDecl(D, PerDeclComment) && Options.EmptyLineBetweenDecls)
435+
Printer.printNewline();
426436
}
427437
}
428438
}
@@ -668,7 +678,7 @@ void swift::ide::printModuleInterface(
668678
if (!TargetMod->isStdlibModule()) {
669679
for (auto *D : ImportDecls)
670680
PrintDecl(D);
671-
Printer << "\n";
681+
Printer.printNewline();
672682
}
673683

674684
{
@@ -686,14 +696,15 @@ void swift::ide::printModuleInterface(
686696

687697
for (auto CM : ClangModules) {
688698
for (auto DeclAndLoc : ClangDecls[CM.first])
689-
PrintDecl(DeclAndLoc.first);
699+
if (PrintDecl(DeclAndLoc.first) && Options.EmptyLineBetweenDecls)
700+
Printer.printNewline();
690701
}
691702
}
692703

693704
if (!(TraversalOptions & ModuleTraversal::SkipOverlay) || !TargetClangMod) {
694705
for (auto *D : SwiftDecls) {
695-
if (PrintDecl(D))
696-
Printer << "\n";
706+
if (PrintDecl(D) && Options.EmptyLineBetweenDecls)
707+
Printer.printNewline();
697708
}
698709

699710
// If we're printing the entire target module (not specific sub-groups),
@@ -704,6 +715,8 @@ void swift::ide::printModuleInterface(
704715
AdjustedOptions, PrintSynthesizedExtensions);
705716
}
706717
}
718+
// Flush pending newlines.
719+
Printer.forceNewlines();
707720
}
708721

709722
static SourceLoc getDeclStartPosition(SourceFile &File) {
@@ -813,8 +826,9 @@ void swift::ide::printHeaderInterface(
813826
continue;
814827
}
815828
if (D->print(Printer, AdjustedOptions))
816-
Printer << "\n";
829+
Printer.printNewline();
817830
}
831+
Printer.forceNewlines();
818832
}
819833

820834
void swift::ide::printSymbolicSwiftClangModuleInterface(
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
import SwiftOnoneSupport
22

33
func %%% (lhs: Int, rhs: Int) -> Int
4-
54
postfix func =-> (lhs: Int) -> Int
6-
75
postfix func => (lhs: Int) -> Int
8-
96
struct BarGenericSwiftStruct1<T> {
107
init(t: T)
118
func bar1InstanceFunc()
129
}
13-
1410
struct BarGenericSwiftStruct2<T, U> where T : BarProtocol {
1511
init(t: T, u: U)
1612
func bar2InstanceFunc()
1713
}
18-
1914
protocol BarProtocol {
2015
func instanceFunc()
2116
}
22-
2317
/// FooSwiftStruct Aaa.
2418
/**
2519
* Bbb.
@@ -36,31 +30,21 @@ struct FooSwiftStruct {
3630
func fooInstanceFunc()
3731
init()
3832
}
39-
4033
/// rdar://18457785
4134
enum MyQuickLookObject {
4235
/// A rectangle.
4336
///
4437
/// Uses explicit coordinates to avoid coupling a particular Cocoa type.
4538
case Rectangle(Float64, Float64, Float64, Float64)
4639
}
47-
4840
var globalVar: Int
49-
5041
func hiddenImport()
51-
5242
func overlayedFoo()
53-
5443
func visibleImport()
55-
5644
precedencegroup High {
5745
associativity: left
5846
higherThan: BitwiseShiftPrecedence
5947
}
60-
6148
infix operator %%% : High
62-
6349
postfix operator =>
64-
6550
postfix operator =->
66-

test/IDE/Inputs/print_clang_header/header-to-print.h.command-line-include.printed.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class BaseInHead {
88
class func doIt(_ arg: Int32)
99
func doIt(_ arg: Int32)
1010
}
11-
1211
/// Awesome name.
1312
class SameName {
1413
}

test/IDE/Inputs/print_clang_header/header-to-print.h.module.printed.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class BaseInHead {
55
class func doIt(_ arg: Int32)
66
func doIt(_ arg: Int32)
77
}
8-
98
/// Awesome name.
109
class SameName {
1110
}

test/IDE/Inputs/print_clang_header/header-to-print.h.printed.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class BaseInHead {
88
class func doIt(_ arg: Int32)
99
func doIt(_ arg: Int32)
1010
}
11-
1211
/// Awesome name.
1312
class SameName {
1413
}

test/IDE/print_module_comments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -emit-module %S/Inputs/foo_swift_module.swift -emit-module-path %t/foo_swift_module.swiftmodule -emit-module-doc-path %t/foo_swift_module.swiftdoc
55
//
66
// RUN: %target-swift-ide-test -print-module -source-filename %s -I %t -module-to-print=foo_swift_module > %t.printed.txt
7-
// RUN: diff %t.printed.txt %S/Inputs/foo_swift_module.printed.comments.txt
7+
// RUN: diff -u %S/Inputs/foo_swift_module.printed.comments.txt %t.printed.txt
88

test/Interop/Cxx/symbolic-imports/indexing-emit-objcxx-symbolic-module-interface.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import ObjCxxModule
5555
// CHECK-EMPTY:
5656
// CHECK-NEXT: public static func freeCxxFunction(_ x: Int32, _ y: Int32) -> Int32
5757
// CHECK-NEXT: }
58+
// CHECK-EMPTY:
5859
// CHECK-NEXT: open class ObjCClass : NSObject {
5960
// CHECK-EMPTY:
6061
// CHECK-NEXT: open func myTestMethod()

test/Interop/Cxx/symbolic-imports/indexing-emit-symbolic-module-interface-used-decls.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ public func useConcreteTemplate() {
4141
// CHECK-NEXT: public func methodFunc(_ x: Any)
4242
// CHECK-NEXT:}
4343
// CHECK-NEXT:}
44+
// CHECK-EMPTY:
4445
// CHECK-NEXT: public typealias TemplateRecordInt = ns.TemplateRecord

test/Interop/Cxx/symbolic-imports/indexing-emit-symbolic-module-interface.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,7 @@ import CxxModule
158158
// CHECK-EMPTY:
159159
// CHECK-NEXT: static func freeFunction(_ x: Int32, _ y: Int32) -> Int32
160160
// CHECK-NEXT: }
161+
// CHECK-EMPTY:
161162
// CHECK-NEXT: typealias MyType = ns.TemplateRecord
163+
// CHECK-EMPTY:
162164
// CHECK-NEXT: typealias MyType2 = TransitiveStruct

test/SourceKit/CursorInfo/cursor_generated_interface.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ASwiftType {
4141
}
4242

4343
// LibA is a mixed framework with no source info and a submodule
44-
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=11:36 -print-raw-response | %FileCheck %s --check-prefix=CHECKA
44+
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=12:36 -print-raw-response | %FileCheck %s --check-prefix=CHECKA
4545
// CHECKA: key.name: "ASwiftType"
4646
// CHECKA: key.modulename: "LibA"
4747
// CHECKA: key.decl_lang: source.lang.swift
@@ -60,7 +60,7 @@ framework module LibA {
6060
@interface AObjcType
6161
@end
6262

63-
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=11:54 -print-raw-response | %FileCheck %s --check-prefix=CHECKAOBJ
63+
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=12:54 -print-raw-response | %FileCheck %s --check-prefix=CHECKAOBJ
6464
// CHECKAOBJ: key.name: "AObjcType"
6565
// CHECKAOBJ: key.line: [[@LINE-5]]
6666
// CHECKAOBJ: key.column: 12
@@ -72,7 +72,7 @@ framework module LibA {
7272
@interface ASubType
7373
@end
7474

75-
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=11:70 -print-raw-response | %FileCheck %s --check-prefix=CHECKASUB
75+
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=12:70 -print-raw-response | %FileCheck %s --check-prefix=CHECKASUB
7676
// CHECKASUB: key.name: "ASubType"
7777
// CHECKASUB: key.line: [[@LINE-5]]
7878
// CHECKASUB: key.column: 12
@@ -84,7 +84,7 @@ framework module LibA {
8484
public class BType {}
8585

8686
// LibB has source info
87-
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=13:32 -print-raw-response | %FileCheck %s --check-prefix=CHECKB
87+
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=14:32 -print-raw-response | %FileCheck %s --check-prefix=CHECKB
8888
// CHECKB: key.name: "BType"
8989
// CHECKB: key.line: [[@LINE-5]]
9090
// CHECKB: key.column: 14
@@ -96,7 +96,7 @@ public class BType {}
9696
public class CType {}
9797

9898
// LibC has no source info
99-
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=13:47 -print-raw-response | %FileCheck %s --check-prefix=CHECKC
99+
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=14:47 -print-raw-response | %FileCheck %s --check-prefix=CHECKC
100100
// CHECKC: key.name: "CType"
101101
// CHECKC: key.modulename: "LibC"
102102
// CHECKC: key.decl_lang: source.lang.swift
@@ -105,7 +105,7 @@ public class CType {}
105105
@interface DType
106106
@end
107107
108-
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=13:57 -print-raw-response | %FileCheck %s --check-prefix=CHECKD
108+
// RUN: %sourcekitd-test -req=interface-gen-open -module LibA -- -F %t/frameworks -target %target-triple == -req=cursor -pos=14:57 -print-raw-response | %FileCheck %s --check-prefix=CHECKD
109109
// CHECKD: key.name: "DType"
110110
// CHECKD: key.line: [[@LINE-5]]
111111
// CHECKD: key.column: 12

test/SourceKit/CursorInfo/cursor_info_async.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import Foo
1111
// RUN: %sourcekitd-test -req=interface-gen-open -module Foo -- \
1212
// RUN: -F %S/../Inputs/libIDE-mock-sdk \
1313
// RUN: -target %target-triple %clang-importer-sdk-nosource -I %t \
14-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
15-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
16-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
17-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
18-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
19-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
20-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 \
21-
// RUN: == -async -dont-print-request -req=cursor -pos=49:15 | %FileCheck %s -check-prefix=CHECK-FOO
14+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
15+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
16+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
17+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
18+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
19+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
20+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 \
21+
// RUN: == -async -dont-print-request -req=cursor -pos=54:15 | %FileCheck %s -check-prefix=CHECK-FOO
2222

2323
// CHECK-FOO: <decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooRuncingOptions
2424
// CHECK-FOO: <decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>FooRuncingOptions

0 commit comments

Comments
 (0)