[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.
This commit is contained in:
Becca Royal-Gordon
2025-02-17 18:09:03 -08:00
parent de63f47224
commit f26a6fb985
2 changed files with 20 additions and 12 deletions

View File

@@ -117,8 +117,6 @@ public:
}
};
namespace {
namespace compare_detail {
enum : int {
@@ -244,7 +242,7 @@ static int lastDitchSort(Decl *lhs, Decl *rhs, bool suppressDiagnostic) {
return result;
}
}
} // end namespace compare_detail
/// Comparator for use with \c llvm::array_pod_sort() . This sorts decls into
/// reverse order since they will be pushed onto a stack.
@@ -334,6 +332,7 @@ static int reverseCompareDecls(Decl * const *lhs, Decl * const *rhs) {
// Still nothing? Fine, we'll look for a difference between the members.
{
// First pass: compare names
for (auto pair : llvm::zip_equal(lhsMembers, rhsMembers)) {
auto *lhsMember = dyn_cast<ValueDecl>(std::get<0>(pair)),
*rhsMember = dyn_cast<ValueDecl>(std::get<1>(pair));
@@ -345,9 +344,19 @@ static int reverseCompareDecls(Decl * const *lhs, Decl * const *rhs) {
ASSERT(lhsMember && rhsMember);
COMPARE(getNameString(lhsMember), getNameString(rhsMember));
}
// Second pass: compare other traits.
for (auto pair : llvm::zip_equal(lhsMembers, rhsMembers)) {
auto *lhsMember = dyn_cast<ValueDecl>(std::get<0>(pair)),
*rhsMember = dyn_cast<ValueDecl>(std::get<1>(pair));
if (!lhsMember || !rhsMember)
continue;
COMPARE(getTypeString(lhsMember), getTypeString(rhsMember));
COMPARE(getGenericSignatureString(lhsMember),
getGenericSignatureString(rhsMember));
COMPARE(getMangledNameString(lhsMember), getMangledNameString(rhsMember));
}
}
@@ -361,7 +370,6 @@ static int reverseCompareDecls(Decl * const *lhs, Decl * const *rhs) {
#undef COMPARE
}
}
class ModuleWriter {
enum class EmissionState { NotYetDefined = 0, DefinitionRequested, Defined };

View File

@@ -96,22 +96,22 @@ extension A5 {
// CHECK-NEXT: @interface A6
@objc class A6 {}
extension A6 {
@objc(skippedInt:) func skipped(_: Int) {}
@objc func abc() {}
}
extension A6 {
@objc(skippedBool:) func skipped(_: Bool) {}
@objc func def() {}
}
extension A6 {
@objc(skippedInt:) func skipped(_: Int) {}
@objc func abc() {}
}
// CHECK: @interface A6 (SWIFT_EXTENSION(extensions))
// CHECK-NEXT: - (void)skippedBool:
// CHECK-NEXT: - (void)def
// CHECK-NEXT: - (void)skippedInt:
// CHECK-NEXT: - (void)abc
// CHECK-NEXT: @end
// CHECK-EMPTY:
// CHECK-NEXT: @interface A6 (SWIFT_EXTENSION(extensions))
// CHECK-NEXT: - (void)skippedInt:
// CHECK-NEXT: - (void)abc
// CHECK-NEXT: - (void)skippedBool:
// CHECK-NEXT: - (void)def
// CHECK-NEXT: @end
// CHECK-EMPTY: