Skip to content

ABIChecker: diagnose mangled name changes #39010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsModuleDiffer.def
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ ERROR(not_inheriting_convenience_inits,APIDigesterBreakage,"%0 no longer inherit

ERROR(enum_case_added,APIDigesterBreakage,"%0 has been added as a new enum case", (StringRef))

ERROR(demangled_name_changed,APIDigesterBreakage,"%0 has mangled name changing from '%1' to '%2'", (StringRef, StringRef, StringRef))

WARNING(cannot_read_allowlist,none,"cannot read breakage allowlist at '%0'", (StringRef))

#define UNDEFINE_DIAGNOSTIC_MACROS
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/USRGeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ bool printExtensionUSR(const ExtensionDecl *ED, raw_ostream &OS);
/// \returns true if it failed, false on success.
bool printDeclUSR(const Decl *D, raw_ostream &OS);

/// Demangle a mangle-name-based USR to a human readable name.
std::string demangleUSR(StringRef mangled);

} // namespace ide
} // namespace swift

Expand Down
9 changes: 9 additions & 0 deletions lib/APIDigester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,8 @@ static bool isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
if (Left->getFixedBinaryOrder() != Right->getFixedBinaryOrder())
return false;
}
if (Left->getUsr() != Right->getUsr())
return false;
LLVM_FALLTHROUGH;
}
case SDKNodeKind::Conformance:
Expand Down Expand Up @@ -2551,6 +2553,13 @@ void swift::ide::api::SDKNodeDecl::diagnose(SDKNode *Right) {
emitDiag(Loc, diag::decl_reorder, getFixedBinaryOrder(),
RD->getFixedBinaryOrder());
}
if (getUsr() != RD->getUsr()) {
auto left = demangleUSR(getUsr());
auto right = demangleUSR(RD->getUsr());
if (left != right) {
emitDiag(Loc, diag::demangled_name_changed, left, right);
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/APIDigester/ModuleDiagsConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static StringRef getCategoryName(uint32_t ID) {
case LocalDiagID::raw_type_change:
return "/* RawRepresentable Changes */";
case LocalDiagID::generic_sig_change:
case LocalDiagID::demangled_name_changed:
return "/* Generic Signature Changes */";
case LocalDiagID::enum_case_added:
case LocalDiagID::decl_added:
Expand Down
13 changes: 13 additions & 0 deletions lib/AST/USRGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/AST/SwiftNameTranslation.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/USRGeneration.h"
#include "swift/Demangling/Demangler.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -255,6 +256,18 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
return NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
}

std::string ide::demangleUSR(StringRef mangled) {
if (mangled.startswith(getUSRSpacePrefix())) {
mangled = mangled.substr(getUSRSpacePrefix().size());
}
SmallString<128> buffer;
buffer += "$s";
buffer += mangled;
mangled = buffer.str();
Demangler Dem;
return nodeToString(Dem.demangleSymbol(mangled));
}

std::string
swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,
const TypeDecl *D) const {
Expand Down
10 changes: 10 additions & 0 deletions test/api-digester/Outputs/Cake-abi.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@

/* Generic Signature Changes */
cake: Constructor S1.init(_:) has mangled name changing from 'cake.S1.init(Swift.Int) -> cake.S1' to 'cake.S1.init(Swift.Double) -> cake.S1'
cake: Func C1.foo1() has mangled name changing from 'static cake.C1.foo1() -> ()' to 'cake.C1.foo1() -> ()'
cake: Func C1.foo2(_:) has mangled name changing from 'cake.C1.foo2(Swift.Int) -> ()' to 'cake.C1.foo2(() -> ()) -> ()'
cake: Func P1.P1Constraint() has generic signature change from <Self where Self : cake.P1, Self : cake.P2> to <Self where Self : cake.P1>
cake: Func P1.P1Constraint() has mangled name changing from '(extension in cake):cake.P1< where A: cake.P2>.P1Constraint() -> ()' to '(extension in cake):cake.P1.P1Constraint() -> ()'
cake: Func S1.foo3() has mangled name changing from 'cake.S1.foo3() -> ()' to 'static cake.S1.foo3() -> ()'
cake: Func S1.foo5(x:y:) has mangled name changing from 'cake.S1.foo5(x: Swift.Int, y: Swift.Int) -> ()' to 'cake.S1.foo5(x: Swift.Int, y: Swift.Int, z: Swift.Int) -> ()'
cake: Func Somestruct2.foo1(_:) has mangled name changing from 'static cake.Somestruct2.foo1(cake.C3) -> ()' to 'static cake.NSSomestruct2.foo1(cake.C1) -> ()'
cake: Func ownershipChange(_:_:) has mangled name changing from 'cake.ownershipChange(inout Swift.Int, __shared Swift.Int) -> ()' to 'cake.ownershipChange(Swift.Int, __owned Swift.Int) -> ()'
cake: Func returnFunctionTypeOwnershipChange() has mangled name changing from 'cake.returnFunctionTypeOwnershipChange() -> (cake.C1) -> ()' to 'cake.returnFunctionTypeOwnershipChange() -> (__owned cake.C1) -> ()'
cake: Protocol P3 has generic signature change from <Self : cake.P1, Self : cake.P2> to <Self : cake.P1, Self : cake.P4>
cake: Struct Somestruct2 has mangled name changing from 'cake.Somestruct2' to 'cake.NSSomestruct2'

/* RawRepresentable Changes */

Expand Down
21 changes: 21 additions & 0 deletions test/api-digester/diff-demangled-name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %empty-directory(%t)
// RUN: echo "public func foo() {}" > %t/Foo.swift

// RUN: echo "public protocol P { associatedtype A }" > %t/Foo-1.swift
// RUN: echo "public extension P { public func f() where A == Int {} }" >> %t/Foo-1.swift

// RUN: echo "public protocol P { associatedtype A }" > %t/Foo-2.swift
// RUN: echo "public extension P where A == Int { public func f() {} }" >> %t/Foo-2.swift

// RUN: %target-swift-frontend -emit-module %t/Foo-1.swift -module-name Foo -emit-module-interface-path %t/Foo1.swiftinterface
// RUN: %target-swift-frontend -emit-module %t/Foo-2.swift -module-name Foo -emit-module-interface-path %t/Foo2.swiftinterface

// RUN: %target-swift-frontend -compile-module-from-interface %t/Foo1.swiftinterface -o %t/Foo1.swiftmodule -module-name Foo -emit-abi-descriptor-path %t/Foo1.json

// RUN: %target-swift-frontend -compile-module-from-interface %t/Foo2.swiftinterface -o %t/Foo2.swiftmodule -module-name Foo -emit-abi-descriptor-path %t/Foo2.json

// RUN: %api-digester -diagnose-sdk -print-module --input-paths %t/Foo1.json -input-paths %t/Foo2.json -abi -o %t/result.txt

// RUN: %FileCheck %s < %t/result.txt

// CHECK: Foo: Func P.f() has mangled name changing from '(extension in Foo):Foo.P.f< where A.A == Swift.Int>() -> ()' to '(extension in Foo):Foo.P< where A.A == Swift.Int>.f() -> ()'