Skip to content

Commit 4c49e67

Browse files
committed
ModulePrinting: Sort the extensions inside a merge group so that actual extensions get printed before synthesized ones.
1 parent 2de0a6f commit 4c49e67

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,29 @@ struct SynthesizedExtensionAnalyzer::Implementation {
213213
operator bool() const { return Ext; }
214214
SynthesizedExtensionInfo(bool IsSynthesized = true) :
215215
IsSynthesized(IsSynthesized) {}
216+
bool operator< (const SynthesizedExtensionInfo& Rhs) const {
217+
218+
// Synthesized are always after actual ones.
219+
if (IsSynthesized != Rhs.IsSynthesized)
220+
return !IsSynthesized;
221+
222+
// If not from the same file, sort by file name.
223+
if (auto LFile = Ext->getSourceFileName()) {
224+
if (auto RFile = Rhs.Ext->getSourceFileName()) {
225+
int Result = LFile.getValue().compare(RFile.getValue());
226+
if (Result != 0)
227+
return Result < 0;
228+
}
229+
}
230+
231+
// Otherwise, sort by source order.
232+
if (auto LeftOrder = Ext->getSourceOrder()) {
233+
if (auto RightOrder = Rhs.Ext->getSourceOrder()) {
234+
return LeftOrder.getValue() < RightOrder.getValue();
235+
}
236+
}
237+
return false;
238+
}
216239
};
217240

218241
struct ExtensionMergeInfo {
@@ -270,6 +293,14 @@ struct SynthesizedExtensionAnalyzer::Implementation {
270293
MergeGroupKind::UnmergableWithTypeDef) {
271294
Members.push_back(Info);
272295
}
296+
297+
void sortMembers() {
298+
std::sort(Members.begin(), Members.end(),
299+
[](SynthesizedExtensionInfo *LHS, SynthesizedExtensionInfo *RHS) {
300+
return (*LHS) < (*RHS);
301+
});
302+
}
303+
273304
bool operator< (const ExtensionMergeGroup& Rhs) const {
274305
if (RequirementsCount == Rhs.RequirementsCount)
275306
return InheritanceCount < Rhs.InheritanceCount;
@@ -493,6 +524,9 @@ struct SynthesizedExtensionAnalyzer::Implementation {
493524

494525
populateMergeGroup(*InfoMap, MergeInfoMap, AllGroups);
495526
std::sort(AllGroups.begin(), AllGroups.end());
527+
for (auto &Group : AllGroups) {
528+
Group.sortMembers();
529+
}
496530
return InfoMap;
497531
}
498532
};

test/IDE/print_synthesized_extensions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ public extension S6 {
201201
// CHECK9-NEXT: <decl:TypeAlias>public typealias <loc>T2</loc> = <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S5</ref></decl>
202202
// CHECK9-NEXT: <decl:Func>public func <loc>f1(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref>.Type.<ref:TypeAlias>T1</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref>.Type.<ref:TypeAlias>T1</ref></decl>
203203
// CHECK9-NEXT: <decl:Func>public func <loc>f2(<decl:Param>t: <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref>.Type.<ref:TypeAlias>T2</ref></decl>)</loc> -> <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S6</ref>.Type.<ref:TypeAlias>T2</ref></decl></decl>
204-
// CHECK9-NEXT: <synthesized><decl:Func>public func <loc>p3Func(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl></synthesized>
205-
// CHECK9-NEXT: <synthesized><decl:Func>public func <loc>ef5(<decl:Param>t: <ref:Struct>S5</ref></decl>)</loc></decl></synthesized>
206204
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>f3()</loc></decl></decl>
207-
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>fromActualExtension()</loc></decl>
208-
// CHECK9-NEXT: }</decl>
205+
// CHECK9-NEXT: <decl:Extension><decl:Func>public func <loc>fromActualExtension()</loc></decl></decl>
206+
// CHECK9-NEXT: <synthesized><decl:Func>public func <loc>p3Func(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl></synthesized>
207+
// CHECK9-NEXT: <synthesized><decl:Func>public func <loc>ef5(<decl:Param>t: <ref:Struct>S5</ref></decl>)</loc></decl>
208+
// CHECK9-NEXT: }</synthesized>
209209

210210
// CHECK10: <synthesized>extension <ref:module>print_synthesized_extensions</ref>.<ref:Struct>S7</ref>.<ref:Struct>S8</ref> {
211211
// CHECK10-NEXT: <decl:Func>public func <loc>p3Func(<decl:Param>i: <ref:Struct>Int</ref></decl>)</loc> -> <ref:Struct>Int</ref></decl></synthesized>

0 commit comments

Comments
 (0)