Skip to content

Commit 6b0c22c

Browse files
authored
Merge pull request #64311 from tshortli/print-accessor-attributes
AST: Print attributes on accessor declarations
2 parents 31914a8 + dd41a3e commit 6b0c22c

File tree

2 files changed

+89
-48
lines changed

2 files changed

+89
-48
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,36 +2211,6 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22112211
// Otherwise, print all the concrete defining accessors.
22122212
bool PrintAccessorBody = Options.FunctionDefinitions;
22132213

2214-
// Helper to print an accessor. Returns true if the
2215-
// accessor was present but skipped.
2216-
auto PrintAccessor = [&](AccessorKind Kind) -> bool {
2217-
auto *Accessor = ASD->getAccessor(Kind);
2218-
if (!Accessor || !shouldPrint(Accessor))
2219-
return true;
2220-
if (!PrintAccessorBody) {
2221-
Printer << " ";
2222-
printSelfAccessKindModifiersIfNeeded(Accessor);
2223-
2224-
Printer.printKeyword(getAccessorLabel(Accessor->getAccessorKind()), Options);
2225-
2226-
// handle any effects specifiers
2227-
if (Accessor->getAccessorKind() == AccessorKind::Get) {
2228-
if (asyncGet) printWithSpace("async");
2229-
if (throwsGet) printWithSpace("throws");
2230-
}
2231-
2232-
} else {
2233-
{
2234-
IndentRAII IndentMore(*this);
2235-
indent();
2236-
visit(Accessor);
2237-
}
2238-
indent();
2239-
Printer.printNewline();
2240-
}
2241-
return false;
2242-
};
2243-
22442214
// Determine if we should print the getter without the 'get { ... }'
22452215
// block around it.
22462216
bool isOnlyGetter = impl.getReadImpl() == ReadImplKind::Get &&
@@ -2254,28 +2224,31 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22542224
return;
22552225
}
22562226

2257-
Printer << " {";
2258-
2259-
if (PrintAccessorBody)
2260-
Printer.printNewline();
2227+
// Collect the accessor declarations that we should print.
2228+
SmallVector<AccessorDecl *, 4> accessorsToPrint;
2229+
auto AddAccessorToPrint = [&](AccessorKind kind) {
2230+
auto *Accessor = ASD->getAccessor(kind);
2231+
if (Accessor && shouldPrint(Accessor))
2232+
accessorsToPrint.push_back(Accessor);
2233+
};
22612234

22622235
if (PrintAbstract) {
2263-
PrintAccessor(AccessorKind::Get);
2236+
AddAccessorToPrint(AccessorKind::Get);
22642237
if (ASD->supportsMutation())
2265-
PrintAccessor(AccessorKind::Set);
2238+
AddAccessorToPrint(AccessorKind::Set);
22662239
} else {
22672240
switch (impl.getReadImpl()) {
22682241
case ReadImplKind::Stored:
22692242
case ReadImplKind::Inherited:
22702243
break;
22712244
case ReadImplKind::Get:
2272-
PrintAccessor(AccessorKind::Get);
2245+
AddAccessorToPrint(AccessorKind::Get);
22732246
break;
22742247
case ReadImplKind::Address:
2275-
PrintAccessor(AccessorKind::Address);
2248+
AddAccessorToPrint(AccessorKind::Address);
22762249
break;
22772250
case ReadImplKind::Read:
2278-
PrintAccessor(AccessorKind::Read);
2251+
AddAccessorToPrint(AccessorKind::Read);
22792252
break;
22802253
}
22812254
switch (impl.getWriteImpl()) {
@@ -2285,27 +2258,67 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22852258
llvm_unreachable("simply-stored variable should have been filtered out");
22862259
case WriteImplKind::StoredWithObservers:
22872260
case WriteImplKind::InheritedWithObservers: {
2288-
PrintAccessor(AccessorKind::Get);
2289-
PrintAccessor(AccessorKind::Set);
2261+
AddAccessorToPrint(AccessorKind::Get);
2262+
AddAccessorToPrint(AccessorKind::Set);
22902263
break;
22912264
}
22922265
case WriteImplKind::Set:
2293-
PrintAccessor(AccessorKind::Set);
2266+
AddAccessorToPrint(AccessorKind::Set);
22942267
if (!shouldHideModifyAccessor())
2295-
PrintAccessor(AccessorKind::Modify);
2268+
AddAccessorToPrint(AccessorKind::Modify);
22962269
break;
22972270
case WriteImplKind::MutableAddress:
2298-
PrintAccessor(AccessorKind::MutableAddress);
2299-
PrintAccessor(AccessorKind::WillSet);
2300-
PrintAccessor(AccessorKind::DidSet);
2271+
AddAccessorToPrint(AccessorKind::MutableAddress);
2272+
AddAccessorToPrint(AccessorKind::WillSet);
2273+
AddAccessorToPrint(AccessorKind::DidSet);
23012274
break;
23022275
case WriteImplKind::Modify:
2303-
PrintAccessor(AccessorKind::Modify);
2276+
AddAccessorToPrint(AccessorKind::Modify);
23042277
break;
23052278
}
23062279
}
23072280

2308-
if (!PrintAccessorBody)
2281+
// If we're not printing the accessor bodies and none of the accessors have
2282+
// attributes then we can print in a shorter, compact form.
2283+
bool PrintCompactAccessors =
2284+
!PrintAccessorBody &&
2285+
std::all_of(accessorsToPrint.begin(), accessorsToPrint.end(),
2286+
[](AccessorDecl *accessor) {
2287+
return accessor->getAttrs().isEmpty();
2288+
});
2289+
2290+
Printer << " {";
2291+
2292+
if (!PrintCompactAccessors)
2293+
Printer.printNewline();
2294+
2295+
for (auto *accessor : accessorsToPrint) {
2296+
if (PrintCompactAccessors) {
2297+
Printer << " ";
2298+
printSelfAccessKindModifiersIfNeeded(accessor);
2299+
2300+
Printer.printKeyword(getAccessorLabel(accessor->getAccessorKind()),
2301+
Options);
2302+
2303+
// handle any effects specifiers
2304+
if (accessor->getAccessorKind() == AccessorKind::Get) {
2305+
if (asyncGet)
2306+
printWithSpace("async");
2307+
if (throwsGet)
2308+
printWithSpace("throws");
2309+
}
2310+
} else {
2311+
{
2312+
IndentRAII IndentMore(*this);
2313+
indent();
2314+
visit(accessor);
2315+
}
2316+
indent();
2317+
Printer.printNewline();
2318+
}
2319+
}
2320+
2321+
if (PrintCompactAccessors)
23092322
Printer << " ";
23102323

23112324
Printer << "}";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -module-name Library -target %target-swift-abi-5.3-triple
3+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library
4+
// RUN: %FileCheck %s < %t/Library.swiftinterface
5+
6+
@available(SwiftStdlib 5.2, *)
7+
@propertyWrapper
8+
public struct ConditionallyAvailableWrapper<T> {
9+
public var wrappedValue: T
10+
11+
public init(wrappedValue: T) {
12+
self.wrappedValue = wrappedValue
13+
}
14+
}
15+
16+
// CHECK: public struct HasWrappers {
17+
public struct HasWrappers {
18+
// CHECK: @available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *)
19+
// CHECK-NEXT: @Library.ConditionallyAvailableWrapper public var x: Swift.Int {
20+
// CHECK-NEXT: get
21+
// CHECK-NEXT: @available(iOS 13.4, tvOS 13.4, watchOS 6.2, macOS 10.15.4, *)
22+
// CHECK-NEXT: set
23+
// CHECK-NEXT: @available(iOS 13.4, tvOS 13.4, watchOS 6.2, macOS 10.15.4, *)
24+
// CHECK-NEXT: _modify
25+
// CHECK-NEXT: }
26+
@available(SwiftStdlib 5.2, *)
27+
@ConditionallyAvailableWrapper public var x: Int
28+
}

0 commit comments

Comments
 (0)