Skip to content

Commit 685d35b

Browse files
authored
Merge pull request #30306 from xymus/print-objc-ext
[PrintAsObjC] Don't print imports for empty extensions
2 parents 20c0267 + 4e8867e commit 685d35b

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ class DeclAndTypePrinter::Implementation
155155
return owningPrinter.shouldInclude(VD);
156156
}
157157

158+
bool isEmptyExtensionDecl(const ExtensionDecl *ED) {
159+
auto members = ED->getMembers();
160+
auto hasMembers = std::any_of(members.begin(), members.end(),
161+
[this](const Decl *D) -> bool {
162+
if (auto VD = dyn_cast<ValueDecl>(D))
163+
if (shouldInclude(VD))
164+
return true;
165+
return false;
166+
});
167+
168+
auto protocols = ED->getLocalProtocols(ConformanceLookupKind::OnlyExplicit);
169+
auto hasProtocols = std::any_of(protocols.begin(), protocols.end(),
170+
[this](const ProtocolDecl *PD) -> bool {
171+
return shouldInclude(PD);
172+
});
173+
174+
return (!hasMembers && !hasProtocols);
175+
}
176+
158177
private:
159178
/// Prints a protocol adoption list: <code>&lt;NSCoding, NSCopying&gt;</code>
160179
///
@@ -311,25 +330,6 @@ class DeclAndTypePrinter::Implementation
311330
os << "@end\n";
312331
}
313332

314-
bool isEmptyExtensionDecl(ExtensionDecl *ED) {
315-
auto members = ED->getMembers();
316-
auto hasMembers = std::any_of(members.begin(), members.end(),
317-
[this](const Decl *D) -> bool {
318-
if (auto VD = dyn_cast<ValueDecl>(D))
319-
if (shouldInclude(VD))
320-
return true;
321-
return false;
322-
});
323-
324-
auto protocols = ED->getLocalProtocols(ConformanceLookupKind::OnlyExplicit);
325-
auto hasProtocols = std::any_of(protocols.begin(), protocols.end(),
326-
[this](const ProtocolDecl *PD) -> bool {
327-
return shouldInclude(PD);
328-
});
329-
330-
return (!hasMembers && !hasProtocols);
331-
}
332-
333333
void visitExtensionDecl(ExtensionDecl *ED) {
334334
if (isEmptyExtensionDecl(ED))
335335
return;
@@ -2063,6 +2063,10 @@ void DeclAndTypePrinter::printAdHocCategory(
20632063
getImpl().printAdHocCategory(members);
20642064
}
20652065

2066+
bool DeclAndTypePrinter::isEmptyExtensionDecl(const ExtensionDecl *ED) {
2067+
return getImpl().isEmptyExtensionDecl(ED);
2068+
}
2069+
20662070
StringRef
20672071
DeclAndTypePrinter::maybeGetOSObjectBaseName(const clang::NamedDecl *decl) {
20682072
StringRef name = decl->getName();

lib/PrintAsObjC/DeclAndTypePrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class DeclAndTypePrinter {
7070
void print(const Decl *D);
7171
void print(Type ty);
7272

73+
/// Is \p ED empty of members and protocol conformances to include?
74+
bool isEmptyExtensionDecl(const ExtensionDecl *ED);
75+
7376
/// Prints a category declaring the given members.
7477
///
7578
/// All members must have the same parent type. The list must not be empty.

lib/PrintAsObjC/ModuleContentsWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ class ModuleWriter {
400400
}
401401

402402
bool writeExtension(const ExtensionDecl *ED) {
403+
if (printer.isEmptyExtensionDecl(ED))
404+
return true;
405+
403406
bool allRequirementsSatisfied = true;
404407

405408
const ClassDecl *CD = ED->getSelfClassDecl();

test/PrintAsObjC/Inputs/custom-modules/MiserablePileOfSecrets.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
@interface NSObject (Secrets)
44
- (void)secretMethod;
55
@end
6+
7+
@interface SecretClass : NSObject
8+
@end

test/PrintAsObjC/imports.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ import MostlyPrivate2_Private
7777
@objc public class TestSubclass: NSObject {
7878
@_implementationOnly public override func secretMethod() {}
7979
}
80+
81+
extension SecretClass {
82+
private func somethingThatShouldNotBeIncluded() {}
83+
}

0 commit comments

Comments
 (0)