Skip to content

Commit f26a6fb

Browse files
committed
[PrintAsClang] Tweak fallback member sort rules
Compare the names of all extension members first, before attempting weirder and more expensive comparisons like stringified type and mangled name. This gives us a sort order that’s a little more comprehensible to humans.
1 parent de63f47 commit f26a6fb

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ class ReferencedTypeFinder : public TypeDeclFinder {
117117
}
118118
};
119119

120-
namespace {
121-
122120
namespace compare_detail {
123121

124122
enum : int {
@@ -244,7 +242,7 @@ static int lastDitchSort(Decl *lhs, Decl *rhs, bool suppressDiagnostic) {
244242
return result;
245243
}
246244

247-
}
245+
} // end namespace compare_detail
248246

249247
/// Comparator for use with \c llvm::array_pod_sort() . This sorts decls into
250248
/// reverse order since they will be pushed onto a stack.
@@ -334,6 +332,7 @@ static int reverseCompareDecls(Decl * const *lhs, Decl * const *rhs) {
334332

335333
// Still nothing? Fine, we'll look for a difference between the members.
336334
{
335+
// First pass: compare names
337336
for (auto pair : llvm::zip_equal(lhsMembers, rhsMembers)) {
338337
auto *lhsMember = dyn_cast<ValueDecl>(std::get<0>(pair)),
339338
*rhsMember = dyn_cast<ValueDecl>(std::get<1>(pair));
@@ -345,9 +344,19 @@ static int reverseCompareDecls(Decl * const *lhs, Decl * const *rhs) {
345344
ASSERT(lhsMember && rhsMember);
346345

347346
COMPARE(getNameString(lhsMember), getNameString(rhsMember));
347+
}
348+
349+
// Second pass: compare other traits.
350+
for (auto pair : llvm::zip_equal(lhsMembers, rhsMembers)) {
351+
auto *lhsMember = dyn_cast<ValueDecl>(std::get<0>(pair)),
352+
*rhsMember = dyn_cast<ValueDecl>(std::get<1>(pair));
353+
if (!lhsMember || !rhsMember)
354+
continue;
355+
348356
COMPARE(getTypeString(lhsMember), getTypeString(rhsMember));
349357
COMPARE(getGenericSignatureString(lhsMember),
350358
getGenericSignatureString(rhsMember));
359+
COMPARE(getMangledNameString(lhsMember), getMangledNameString(rhsMember));
351360
}
352361
}
353362

@@ -361,7 +370,6 @@ static int reverseCompareDecls(Decl * const *lhs, Decl * const *rhs) {
361370

362371
#undef COMPARE
363372
}
364-
}
365373

366374
class ModuleWriter {
367375
enum class EmissionState { NotYetDefined = 0, DefinitionRequested, Defined };

test/PrintAsObjC/extensions.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,22 @@ extension A5 {
9696
// CHECK-NEXT: @interface A6
9797
@objc class A6 {}
9898

99-
extension A6 {
100-
@objc(skippedInt:) func skipped(_: Int) {}
101-
@objc func abc() {}
102-
}
10399
extension A6 {
104100
@objc(skippedBool:) func skipped(_: Bool) {}
105101
@objc func def() {}
106102
}
103+
extension A6 {
104+
@objc(skippedInt:) func skipped(_: Int) {}
105+
@objc func abc() {}
106+
}
107107
// CHECK: @interface A6 (SWIFT_EXTENSION(extensions))
108-
// CHECK-NEXT: - (void)skippedBool:
109-
// CHECK-NEXT: - (void)def
108+
// CHECK-NEXT: - (void)skippedInt:
109+
// CHECK-NEXT: - (void)abc
110110
// CHECK-NEXT: @end
111111
// CHECK-EMPTY:
112112
// CHECK-NEXT: @interface A6 (SWIFT_EXTENSION(extensions))
113-
// CHECK-NEXT: - (void)skippedInt:
114-
// CHECK-NEXT: - (void)abc
113+
// CHECK-NEXT: - (void)skippedBool:
114+
// CHECK-NEXT: - (void)def
115115
// CHECK-NEXT: @end
116116
// CHECK-EMPTY:
117117

0 commit comments

Comments
 (0)