Skip to content

Commit 3be46df

Browse files
committed
ABIChecker: for decls with @_originallyDefinedIn, use original module names in ABI descriptors
rdar://93615410
1 parent da531be commit 3be46df

File tree

8 files changed

+97
-4
lines changed

8 files changed

+97
-4
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ class ASTMangler : public Mangler {
297297

298298
std::string mangleTypeAsContextUSR(const NominalTypeDecl *type);
299299

300-
std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix);
300+
std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix,
301+
bool respectOriginallyDefinedIn = false);
301302
std::string mangleDeclAsUSR(const ValueDecl *Decl, StringRef USRPrefix);
302303

303304
std::string mangleAccessorEntityAsUSR(AccessorKind kind,

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ struct PrintOptions {
209209
/// \see FileUnit::getExportedModuleName
210210
bool UseExportedModuleNames = false;
211211

212+
/// Use the original module name to qualify a symbol.
213+
bool UseOriginallyDefinedInModuleNames = false;
214+
212215
/// Print Swift.Array and Swift.Optional with sugared syntax
213216
/// ([] and ?), even if there are no sugar type nodes.
214217
bool SynthesizeSugarOnTypes = false;

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace {
1717
static PrintOptions getTypePrintOpts(CheckerOptions CheckerOpts) {
1818
PrintOptions Opts;
1919
Opts.SynthesizeSugarOnTypes = true;
20+
Opts.UseOriginallyDefinedInModuleNames = true;
2021
if (!CheckerOpts.Migrator) {
2122
// We should always print fully qualified type names for checking either
2223
// API or ABI stability.
@@ -1096,7 +1097,8 @@ static StringRef calculateMangledName(SDKContext &Ctx, ValueDecl *VD) {
10961097
return Ctx.buffer(attr->Name);
10971098
}
10981099
Mangle::ASTMangler NewMangler;
1099-
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, true));
1100+
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, true,
1101+
/*bool respectOriginallyDefinedIn*/true));
11001102
}
11011103

11021104
static StringRef calculateLocation(SDKContext &SDKCtx, Decl *D) {

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,12 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
759759
return finalize();
760760
}
761761

762-
std::string ASTMangler::mangleAnyDecl(const ValueDecl *Decl, bool prefix) {
762+
std::string
763+
ASTMangler::mangleAnyDecl(const ValueDecl *Decl,
764+
bool prefix,
765+
bool respectOriginallyDefinedIn) {
763766
DWARFMangling = true;
764-
RespectOriginallyDefinedIn = false;
767+
RespectOriginallyDefinedIn = respectOriginallyDefinedIn;
765768
if (prefix) {
766769
beginMangling();
767770
} else {

lib/AST/ASTPrinter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5252,6 +5252,16 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
52525252
Name = Mod->getASTContext().getIdentifier(ExportedModuleName);
52535253
}
52545254

5255+
if (Options.UseOriginallyDefinedInModuleNames) {
5256+
Decl *D = Ty->getDecl();
5257+
for (auto attr: D->getAttrs().getAttributes<OriginallyDefinedInAttr>()) {
5258+
Name = Mod->getASTContext()
5259+
.getIdentifier(const_cast<OriginallyDefinedInAttr*>(attr)
5260+
->OriginalModuleName);
5261+
break;
5262+
}
5263+
}
5264+
52555265
Printer.printModuleRef(Mod, Name);
52565266
Printer << ".";
52575267
}

test/api-digester/Inputs/cake.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,9 @@ public func emitIntoClientFunc() {}
145145

146146
@_silgen_name("silgenName")
147147
public func silgenNamedFunc() {}
148+
149+
@available(OSX 10.7, *)
150+
@_originallyDefinedIn(module: "Bread", OSX 10.9)
151+
public class SinkingClass {
152+
public init() {}
153+
}

test/api-digester/Outputs/cake-abi.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,40 @@
16351635
],
16361636
"funcSelfKind": "NonMutating"
16371637
},
1638+
{
1639+
"kind": "TypeDecl",
1640+
"name": "SinkingClass",
1641+
"printedName": "SinkingClass",
1642+
"children": [
1643+
{
1644+
"kind": "Constructor",
1645+
"name": "init",
1646+
"printedName": "init()",
1647+
"children": [
1648+
{
1649+
"kind": "TypeNominal",
1650+
"name": "SinkingClass",
1651+
"printedName": "Bread.SinkingClass",
1652+
"usr": "s:4cake12SinkingClassC"
1653+
}
1654+
],
1655+
"declKind": "Constructor",
1656+
"usr": "s:4cake12SinkingClassCACycfc",
1657+
"mangledName": "$s5Bread12SinkingClassCACycfc",
1658+
"moduleName": "cake",
1659+
"init_kind": "Designated"
1660+
}
1661+
],
1662+
"declKind": "Class",
1663+
"usr": "s:4cake12SinkingClassC",
1664+
"mangledName": "$s5Bread12SinkingClassC",
1665+
"moduleName": "cake",
1666+
"intro_Macosx": "10.7",
1667+
"declAttributes": [
1668+
"OriginallyDefinedIn",
1669+
"Available"
1670+
]
1671+
},
16381672
{
16391673
"kind": "TypeDecl",
16401674
"name": "Int",

test/api-digester/Outputs/cake.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,40 @@
14961496
],
14971497
"funcSelfKind": "NonMutating"
14981498
},
1499+
{
1500+
"kind": "TypeDecl",
1501+
"name": "SinkingClass",
1502+
"printedName": "SinkingClass",
1503+
"children": [
1504+
{
1505+
"kind": "Constructor",
1506+
"name": "init",
1507+
"printedName": "init()",
1508+
"children": [
1509+
{
1510+
"kind": "TypeNominal",
1511+
"name": "SinkingClass",
1512+
"printedName": "Bread.SinkingClass",
1513+
"usr": "s:4cake12SinkingClassC"
1514+
}
1515+
],
1516+
"declKind": "Constructor",
1517+
"usr": "s:4cake12SinkingClassCACycfc",
1518+
"mangledName": "$s5Bread12SinkingClassCACycfc",
1519+
"moduleName": "cake",
1520+
"init_kind": "Designated"
1521+
}
1522+
],
1523+
"declKind": "Class",
1524+
"usr": "s:4cake12SinkingClassC",
1525+
"mangledName": "$s5Bread12SinkingClassC",
1526+
"moduleName": "cake",
1527+
"intro_Macosx": "10.7",
1528+
"declAttributes": [
1529+
"OriginallyDefinedIn",
1530+
"Available"
1531+
]
1532+
},
14991533
{
15001534
"kind": "TypeDecl",
15011535
"name": "Int",

0 commit comments

Comments
 (0)