Skip to content

Commit a870b06

Browse files
committed
ABI Checker: include mangled names in ABI descriptor files
Previously, we use USR as a delegate for mangled name. However, USR won't incorporate name changes made by attributes like @_silgen_name. Instead, we should add a dedicated field for canonical mangled names. rdar://84202064
1 parent bab9189 commit a870b06

File tree

9 files changed

+364
-82
lines changed

9 files changed

+364
-82
lines changed

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ struct PlatformIntroVersion {
339339
class SDKNodeDecl: public SDKNode {
340340
DeclKind DKind;
341341
StringRef Usr;
342+
StringRef MangledName;
342343
SourceLoc Loc;
343344
StringRef Location;
344345
StringRef ModuleName;
@@ -463,6 +464,7 @@ class SDKNodeType: public SDKNode {
463464

464465
class SDKNodeTypeNominal : public SDKNodeType {
465466
StringRef USR;
467+
StringRef MangledName;
466468
public:
467469
SDKNodeTypeNominal(SDKNodeInitInfo Info);
468470
// Get the usr of the corresponding nominal type decl.
@@ -583,6 +585,7 @@ class SDKNodeDeclType: public SDKNodeDecl {
583585
/// in the conformance, thus getName() will give us the name of the protocol.
584586
class SDKNodeConformance: public SDKNode {
585587
StringRef Usr;
588+
StringRef MangledName;
586589
SDKNodeDeclType *TypeDecl;
587590
friend class SDKNodeDeclType;
588591
bool IsABIPlaceholder;

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class ASTMangler : public Mangler {
271271

272272
std::string mangleTypeAsContextUSR(const NominalTypeDecl *type);
273273

274+
std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix);
274275
std::string mangleDeclAsUSR(const ValueDecl *Decl, StringRef USRPrefix);
275276

276277
std::string mangleAccessorEntityAsUSR(AccessorKind kind,

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ KEY(kind)
142142
KEY_STRING(Name, name)
143143
KEY_STRING(PrintedName, printedName)
144144
KEY_STRING(Usr, usr)
145+
KEY_STRING(MangledName, mangledName)
145146
KEY_STRING(Location, location)
146147
KEY_STRING(ModuleName, moduleName)
147148
KEY_STRING(SuperclassUsr, superclassUsr)

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "llvm/ADT/STLExtras.h"
22
#include "swift/Basic/Defer.h"
33
#include "swift/SIL/SILDeclRef.h"
4+
#include "swift/AST/ASTMangler.h"
45
#include <swift/APIDigester/ModuleAnalyzerNodes.h>
56
#include <algorithm>
67

@@ -98,7 +99,8 @@ SDKNodeRoot::SDKNodeRoot(SDKNodeInitInfo Info): SDKNode(Info, SDKNodeKind::Root)
9899
JsonFormatVer(Info.JsonFormatVer.hasValue() ? *Info.JsonFormatVer : DIGESTER_JSON_DEFAULT_VERSION) {}
99100

100101
SDKNodeDecl::SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
101-
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr), Loc(Info.Loc),
102+
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr),
103+
MangledName(Info.MangledName), Loc(Info.Loc),
102104
Location(Info.Location), ModuleName(Info.ModuleName),
103105
DeclAttributes(Info.DeclAttrs), IsImplicit(Info.IsImplicit),
104106
IsStatic(Info.IsStatic), IsDeprecated(Info.IsDeprecated),
@@ -120,7 +122,8 @@ SDKNodeType::SDKNodeType(SDKNodeInitInfo Info, SDKNodeKind Kind):
120122
ParamValueOwnership(Info.ParamValueOwnership) {}
121123

122124
SDKNodeTypeNominal::SDKNodeTypeNominal(SDKNodeInitInfo Info):
123-
SDKNodeType(Info, SDKNodeKind::TypeNominal), USR(Info.Usr) {}
125+
SDKNodeType(Info, SDKNodeKind::TypeNominal), USR(Info.Usr),
126+
MangledName(Info.MangledName) {}
124127

125128
SDKNodeTypeFunc::SDKNodeTypeFunc(SDKNodeInitInfo Info):
126129
SDKNodeType(Info, SDKNodeKind::TypeFunc) {}
@@ -139,7 +142,8 @@ SDKNodeDeclType::SDKNodeDeclType(SDKNodeInitInfo Info):
139142

140143
SDKNodeConformance::SDKNodeConformance(SDKNodeInitInfo Info):
141144
SDKNode(Info, SDKNodeKind::Conformance),
142-
Usr(Info.Usr), IsABIPlaceholder(Info.IsABIPlaceholder) {}
145+
Usr(Info.Usr), MangledName(Info.MangledName),
146+
IsABIPlaceholder(Info.IsABIPlaceholder) {}
143147

144148
SDKNodeTypeWitness::SDKNodeTypeWitness(SDKNodeInitInfo Info):
145149
SDKNode(Info, SDKNodeKind::TypeWitness) {}
@@ -1036,6 +1040,18 @@ static StringRef calculateUsr(SDKContext &Ctx, ValueDecl *VD) {
10361040
return StringRef();
10371041
}
10381042

1043+
static StringRef calculateMangledName(SDKContext &Ctx, ValueDecl *VD) {
1044+
if (isFromClang(VD)) {
1045+
// Don't mangle clang symbols.
1046+
return StringRef();
1047+
}
1048+
if (auto *attr = VD->getAttrs().getAttribute<SILGenNameAttr>()) {
1049+
return Ctx.buffer(attr->Name);
1050+
}
1051+
Mangle::ASTMangler NewMangler;
1052+
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, false));
1053+
}
1054+
10391055
static StringRef calculateLocation(SDKContext &SDKCtx, Decl *D) {
10401056
if (SDKCtx.getOpts().AvoidLocation)
10411057
return StringRef();
@@ -1298,6 +1314,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info) :
12981314
// If this is a nominal type, get its Usr.
12991315
if (auto *ND = Ty->getAnyNominal()) {
13001316
Usr = calculateUsr(Ctx, ND);
1317+
MangledName = calculateMangledName(Ctx, ND);
13011318
}
13021319
}
13031320

@@ -1415,6 +1432,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
14151432
Name = getSimpleName(VD);
14161433
PrintedName = getPrintedName(Ctx, VD);
14171434
Usr = calculateUsr(Ctx, VD);
1435+
MangledName = calculateMangledName(Ctx, VD);
14181436
IsThrowing = isFuncThrowing(VD);
14191437
IsStatic = VD->isStatic();
14201438
IsOverriding = VD->getOverriddenDecl();
@@ -1969,13 +1987,15 @@ void SDKNodeRoot::jsonize(json::Output &out) {
19691987
void SDKNodeConformance::jsonize(json::Output &out) {
19701988
SDKNode::jsonize(out);
19711989
output(out, KeyKind::KK_usr, Usr);
1990+
output(out, KeyKind::KK_mangledName, MangledName);
19721991
output(out, KeyKind::KK_isABIPlaceholder, IsABIPlaceholder);
19731992
}
19741993

19751994
void SDKNodeDecl::jsonize(json::Output &out) {
19761995
SDKNode::jsonize(out);
19771996
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_declKind).data(), DKind);
19781997
output(out, KeyKind::KK_usr, Usr);
1998+
output(out, KeyKind::KK_mangledName, MangledName);
19791999
output(out, KeyKind::KK_location, Location);
19802000
output(out, KeyKind::KK_moduleName, ModuleName);
19812001
output(out, KeyKind::KK_genericSig, GenericSig);

lib/AST/ASTMangler.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -756,16 +756,14 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
756756
return finalize();
757757
}
758758

759-
std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
760-
StringRef USRPrefix) {
761-
#if SWIFT_BUILD_ONLY_SYNTAXPARSERLIB
762-
return std::string(); // not needed for the parser library.
763-
#endif
764-
759+
std::string ASTMangler::mangleAnyDecl(const ValueDecl *Decl, bool prefix) {
765760
DWARFMangling = true;
766-
beginManglingWithoutPrefix();
761+
if (prefix) {
762+
beginMangling();
763+
} else {
764+
beginManglingWithoutPrefix();
765+
}
767766
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
768-
Buffer << USRPrefix;
769767

770768
if (auto Ctor = dyn_cast<ConstructorDecl>(Decl)) {
771769
appendConstructorEntity(Ctor, /*isAllocating=*/false);
@@ -784,10 +782,18 @@ std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
784782
// We have a custom prefix, so finalize() won't verify for us. If we're not
785783
// in invalid code (coming from an IDE caller) verify manually.
786784
if (!Decl->isInvalid())
787-
verify(Storage.str().drop_front(USRPrefix.size()));
785+
verify(Storage.str());
788786
return finalize();
789787
}
790788

789+
std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
790+
StringRef USRPrefix) {
791+
#if SWIFT_BUILD_ONLY_SYNTAXPARSERLIB
792+
return std::string(); // not needed for the parser library.
793+
#endif
794+
return (llvm::Twine(USRPrefix) + mangleAnyDecl(Decl, false)).str();
795+
}
796+
791797
std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
792798
const AbstractStorageDecl *decl,
793799
StringRef USRPrefix,

test/api-digester/Inputs/cake.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,6 @@ extension SwiftObjcClass {
142142

143143
@_alwaysEmitIntoClient
144144
public func emitIntoClientFunc() {}
145+
146+
@_silgen_name("silgenName")
147+
public func silgenNamedFunc() {}

0 commit comments

Comments
 (0)