Skip to content

Commit 11279ee

Browse files
committed
[PrintAsObjC] Simplify tracking of @optional in protocol printing
The state for @optional vs. @required was only ever referenced from one function, so we can just put it there.
1 parent efa350a commit 11279ee

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,8 @@ class DeclAndTypePrinter::Implementation
110110
SmallVector<const FunctionType *, 4> openFunctionTypes;
111111
const DelayedMemberSet &delayedMembers;
112112

113-
AccessLevel minRequiredAccess;
114-
bool protocolMembersOptional = false;
115-
116113
Optional<Type> NSCopyingType;
114+
AccessLevel minRequiredAccess;
117115

118116
public:
119117
explicit Implementation(ModuleDecl &mod, raw_ostream &out,
@@ -189,6 +187,7 @@ class DeclAndTypePrinter::Implementation
189187
/// Prints the members of a class, extension, or protocol.
190188
template <bool AllowDelayed = false, typename R>
191189
void printMembers(R &&members) {
190+
bool protocolMembersOptional = false;
192191
for (const Decl *member : members) {
193192
auto VD = dyn_cast<ValueDecl>(member);
194193
if (!VD || !shouldInclude(VD) || isa<TypeDecl>(VD))
@@ -199,8 +198,9 @@ class DeclAndTypePrinter::Implementation
199198
os << "// '" << VD->getFullName() << "' below\n";
200199
continue;
201200
}
202-
if (VD->getAttrs().hasAttribute<OptionalAttr>() != protocolMembersOptional) {
203-
protocolMembersOptional = VD->getAttrs().hasAttribute<OptionalAttr>();
201+
if (VD->getAttrs().hasAttribute<OptionalAttr>() !=
202+
protocolMembersOptional) {
203+
protocolMembersOptional = !protocolMembersOptional;
204204
os << (protocolMembersOptional ? "@optional\n" : "@required\n");
205205
}
206206
ASTVisitor::visit(const_cast<ValueDecl*>(VD));
@@ -364,9 +364,7 @@ class DeclAndTypePrinter::Implementation
364364

365365
printProtocols(PD->getInheritedProtocols());
366366
os << "\n";
367-
assert(!protocolMembersOptional && "protocols start required");
368367
printMembers(PD->getMembers());
369-
protocolMembersOptional = false;
370368
os << "@end\n";
371369
}
372370

0 commit comments

Comments
 (0)