Skip to content

Commit d5b7123

Browse files
authored
Merge pull request swiftlang#4527 from benlangmuir/astprinter-attr-tweaks
[ASTPrinter] Add attribute callbacks around conventions; fix spacing
2 parents 6f7693a + e441846 commit d5b7123

File tree

4 files changed

+66
-44
lines changed

4 files changed

+66
-44
lines changed

include/swift/AST/ASTPrinter.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ class ASTPrinter {
189189
printNamePost(PrintNameContext::Attribute);
190190
}
191191

192-
void printSimpleAttr(StringRef name, bool needAt = false) {
192+
ASTPrinter &printSimpleAttr(StringRef name, bool needAt = false) {
193193
callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
194194
printAttrName(name, needAt);
195195
printStructurePost(PrintStructureKind::BuiltinAttribute);
196+
return *this;
196197
}
197198

198199
void printName(Identifier Name,

lib/AST/ASTPrinter.cpp

+36-26
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,6 @@ ASTPrinter &ASTPrinter::operator<<(DeclName name) {
924924
return *this;
925925
}
926926

927-
// FIXME: We need to undef 'defer' when including Tokens.def. It is restored
928-
// below.
929-
#undef defer
930-
931927
ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
932928
StringRef name;
933929
switch (keyword) {
@@ -3791,75 +3787,89 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37913787
return;
37923788

37933789
if (info.isAutoClosure() && !Options.excludeAttrKind(TAK_autoclosure)) {
3794-
Printer.printSimpleAttr("@autoclosure");
3795-
Printer << " ";
3790+
Printer.printSimpleAttr("@autoclosure") << " ";
37963791
}
37973792
if (inParameterPrinting && !info.isNoEscape() &&
37983793
!Options.excludeAttrKind(TAK_escaping)) {
3799-
Printer.printSimpleAttr("@escaping");
3800-
Printer << " ";
3794+
Printer.printSimpleAttr("@escaping") << " ";
38013795
}
38023796

38033797
if (Options.PrintFunctionRepresentationAttrs &&
3804-
!Options.excludeAttrKind(TAK_convention)) {
3798+
!Options.excludeAttrKind(TAK_convention) &&
3799+
info.getSILRepresentation() != SILFunctionType::Representation::Thick) {
3800+
Printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
3801+
Printer.printAttrName("@convention");
3802+
Printer << "(";
38053803
// TODO: coalesce into a single convention attribute.
38063804
switch (info.getSILRepresentation()) {
38073805
case SILFunctionType::Representation::Thick:
3808-
break;
3806+
llvm_unreachable("thick is not printed");
38093807
case SILFunctionType::Representation::Thin:
3810-
Printer << "@convention(thin) ";
3808+
Printer << "thin";
38113809
break;
38123810
case SILFunctionType::Representation::Block:
3813-
Printer << "@convention(block) ";
3811+
Printer << "block";
38143812
break;
38153813
case SILFunctionType::Representation::CFunctionPointer:
3816-
Printer << "@convention(c) ";
3814+
Printer << "c";
38173815
break;
38183816
case SILFunctionType::Representation::Method:
3819-
Printer << "@convention(method) ";
3817+
Printer << "method";
38203818
break;
38213819
case SILFunctionType::Representation::ObjCMethod:
3822-
Printer << "@convention(objc_method) ";
3820+
Printer << "objc_method";
38233821
break;
38243822
case SILFunctionType::Representation::WitnessMethod:
3825-
Printer << "@convention(witness_method) ";
3823+
Printer << "witness_method";
38263824
break;
38273825
}
3826+
Printer << ")";
3827+
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
3828+
Printer << " ";
38283829
}
38293830
}
38303831

38313832
void printFunctionExtInfo(SILFunctionType::ExtInfo info) {
38323833
if (Options.SkipAttributes)
38333834
return;
38343835

3835-
if (Options.PrintFunctionRepresentationAttrs) {
3836+
if (Options.PrintFunctionRepresentationAttrs &&
3837+
!Options.excludeAttrKind(TAK_convention) &&
3838+
info.getRepresentation() != SILFunctionType::Representation::Thick) {
3839+
Printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
3840+
Printer.printAttrName("@convention");
3841+
Printer << "(";
38363842
// TODO: coalesce into a single convention attribute.
38373843
switch (info.getRepresentation()) {
38383844
case SILFunctionType::Representation::Thick:
3839-
break;
3845+
llvm_unreachable("thick is not printed");
38403846
case SILFunctionType::Representation::Thin:
3841-
Printer << "@convention(thin) ";
3847+
Printer << "thin";
38423848
break;
38433849
case SILFunctionType::Representation::Block:
3844-
Printer << "@convention(block) ";
3850+
Printer << "block";
38453851
break;
38463852
case SILFunctionType::Representation::CFunctionPointer:
3847-
Printer << "@convention(c) ";
3853+
Printer << "c";
38483854
break;
38493855
case SILFunctionType::Representation::Method:
3850-
Printer << "@convention(method) ";
3856+
Printer << "method";
38513857
break;
38523858
case SILFunctionType::Representation::ObjCMethod:
3853-
Printer << "@convention(objc_method) ";
3859+
Printer << "objc_method";
38543860
break;
38553861
case SILFunctionType::Representation::WitnessMethod:
3856-
Printer << "@convention(witness_method) ";
3862+
Printer << "witness_method";
38573863
break;
38583864
}
3865+
Printer << ")";
3866+
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
3867+
Printer << " ";
38593868
}
38603869

3861-
if (info.isPseudogeneric())
3862-
Printer << "@pseudogeneric ";
3870+
if (info.isPseudogeneric()) {
3871+
Printer.printSimpleAttr("@pseudogeneric") << " ";
3872+
}
38633873
}
38643874

38653875
void visitFunctionType(FunctionType *T) {

lib/AST/TypeRepr.cpp

+11-17
Original file line numberDiff line numberDiff line change
@@ -283,28 +283,22 @@ void AttributedTypeRepr::printAttrs(ASTPrinter &Printer,
283283
return Attrs.has(K);
284284
};
285285

286-
if (hasAttr(TAK_autoclosure)) {
287-
Printer.printSimpleAttr("@autoclosure");
288-
Printer << " ";
289-
}
290-
if (hasAttr(TAK_escaping)) {
291-
Printer.printSimpleAttr("@escaping");
292-
Printer << " ";
293-
}
286+
if (hasAttr(TAK_autoclosure))
287+
Printer.printSimpleAttr("@autoclosure") << " ";
288+
if (hasAttr(TAK_escaping))
289+
Printer.printSimpleAttr("@escaping") << " ";
290+
291+
if (hasAttr(TAK_thin))
292+
Printer.printSimpleAttr("@thin") << " ";
293+
if (hasAttr(TAK_thick))
294+
Printer.printSimpleAttr("@thick") << " ";
294295

295-
if (hasAttr(TAK_thin)) {
296-
Printer.printSimpleAttr("@thin");
297-
Printer << " ";
298-
}
299-
if (hasAttr(TAK_thick)) {
300-
Printer.printSimpleAttr("@thick");
301-
Printer << " ";
302-
}
303296
if (hasAttr(TAK_convention) && Attrs.convention.hasValue()) {
304297
Printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
305298
Printer.printAttrName("@convention");
306-
Printer << "(" << Attrs.convention.getValue() << ") ";
299+
Printer << "(" << Attrs.convention.getValue() << ")";
307300
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
301+
Printer << " ";
308302
}
309303
}
310304

test/SourceKit/CursorInfo/cursor_info.swift

+17
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ typealias MyVoid = ()
198198

199199
func rethrowingFunction1(_: (Int) throws -> Void) rethrows -> Void {}
200200

201+
func convention1(_: @convention(thick) ()->()) {}
202+
func convention2(_: @convention(thin) ()->()) {}
203+
func convention3(_: @convention(block) ()->()) {}
204+
func convention4(_: @convention(c) ()->()) {}
205+
func convention5(_: @convention(method) ()->()) {}
206+
func convention6(_: @convention(objc_method) ()->()) {}
207+
func convention7(_: @convention(witness_method) ()->()) {}
208+
201209
// RUN: rm -rf %t.tmp
202210
// RUN: mkdir %t.tmp
203211
// RUN: %swiftc_driver -emit-module -o %t.tmp/FooSwiftModule.swiftmodule %S/Inputs/FooSwiftModule.swift
@@ -686,3 +694,12 @@ func rethrowingFunction1(_: (Int) throws -> Void) rethrows -> Void {}
686694
// CHECK85-NOT: @rethrows
687695
// CHECK85: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>rethrowingFunction1</decl.name>({{.*}}) <syntaxtype.keyword>rethrows</syntaxtype.keyword></decl.function.free>
688696
// CHECK85-NOT: @rethrows
697+
698+
// RUN: %sourcekitd-test -req=cursor -pos=201:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
699+
// RUN: %sourcekitd-test -req=cursor -pos=202:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
700+
// RUN: %sourcekitd-test -req=cursor -pos=203:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
701+
// RUN: %sourcekitd-test -req=cursor -pos=204:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
702+
// RUN: %sourcekitd-test -req=cursor -pos=205:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
703+
// RUN: %sourcekitd-test -req=cursor -pos=206:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
704+
// RUN: %sourcekitd-test -req=cursor -pos=207:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK86 %s
705+
// CHECK86: <syntaxtype.attribute.builtin><syntaxtype.attribute.name>@convention</syntaxtype.attribute.name>({{[a-z_]*}})</syntaxtype.attribute.builtin>

0 commit comments

Comments
 (0)