Skip to content

Commit 66b4737

Browse files
committed
TBDGen: use active platform versions to genearate linker directives
1 parent f098448 commit 66b4737

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

include/swift/AST/Attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,8 @@ class OriginallyDefinedInAttr: public DeclAttribute {
16071607
/// Indicates when the symbol was moved here.
16081608
const llvm::VersionTuple MovedVersion;
16091609

1610+
/// Returns true if this attribute is active given the current platform.
1611+
bool isActivePlatform(const ASTContext &ctx) const;
16101612
static bool classof(const DeclAttribute *DA) {
16111613
return DA->getKind() == DAK_OriginallyDefinedIn;
16121614
}

lib/AST/Attr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,10 @@ bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
12231223
return isPlatformActive(Platform, ctx.LangOpts);
12241224
}
12251225

1226+
bool OriginallyDefinedInAttr::isActivePlatform(const ASTContext &ctx) const {
1227+
return isPlatformActive(Platform, ctx.LangOpts);
1228+
}
1229+
12261230
bool AvailableAttr::isLanguageVersionSpecific() const {
12271231
if (PlatformAgnostic ==
12281232
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific)

lib/TBDGen/TBDGen.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,38 @@ void TBDGenVisitor::addSymbolInternal(StringRef name,
6969
}
7070
}
7171

72+
static Optional<llvm::VersionTuple> getDeclMoveOSVersion(Decl *D) {
73+
for (auto *attr: D->getAttrs()) {
74+
if (auto *ODA = dyn_cast<OriginallyDefinedInAttr>(attr)) {
75+
if (ODA->isActivePlatform(D->getASTContext()))
76+
return ODA->MovedVersion;
77+
}
78+
}
79+
return None;
80+
}
81+
7282
void TBDGenVisitor::addLinkerDirectiveSymbols(StringRef name,
7383
llvm::MachO::SymbolKind kind) {
7484
if (kind != llvm::MachO::SymbolKind::GlobalSymbol)
7585
return;
7686
if (!TopLevelDecl)
7787
return;
78-
auto ODA = TopLevelDecl->getAttrs().getAttribute<OriginallyDefinedInAttr>();
79-
if (!ODA)
88+
auto MovedVer = getDeclMoveOSVersion(TopLevelDecl);
89+
if (!MovedVer.hasValue())
8090
return;
91+
assert(MovedVer.hasValue());
8192
unsigned Major[2];
8293
unsigned Minor[2];
83-
Major[1] = ODA->MovedVersion.getMajor();
84-
Minor[1] = ODA->MovedVersion.getMinor().hasValue() ?
85-
*ODA->MovedVersion.getMinor(): 0;
94+
Major[1] = MovedVer->getMajor();
95+
Minor[1] = MovedVer->getMinor().hasValue() ? *MovedVer->getMinor(): 0;
8696
auto AvailRange = AvailabilityInference::availableRange(TopLevelDecl,
8797
TopLevelDecl->getASTContext()).getOSVersion();
8898
assert(AvailRange.hasLowerEndpoint() &&
8999
"cannot find the start point of availability");
90100
if (!AvailRange.hasLowerEndpoint())
91101
return;
92-
assert(AvailRange.getLowerEndpoint() < ODA->MovedVersion);
93-
if (AvailRange.getLowerEndpoint() >= ODA->MovedVersion)
102+
assert(AvailRange.getLowerEndpoint() < *MovedVer);
103+
if (AvailRange.getLowerEndpoint() >= *MovedVer)
94104
return;
95105
Major[0] = AvailRange.getLowerEndpoint().getMajor();
96106
Minor[0] = AvailRange.getLowerEndpoint().getMinor().hasValue() ?
@@ -691,9 +701,7 @@ static bool isApplicationExtensionSafe(const LangOptions &LangOpts) {
691701
}
692702

693703
static bool hasLinkerDirective(Decl *D) {
694-
if (D->getAttrs().hasAttribute<OriginallyDefinedInAttr>())
695-
return true;
696-
return false;
704+
return getDeclMoveOSVersion(D).hasValue();
697705
}
698706

699707
static void enumeratePublicSymbolsAndWrite(ModuleDecl *M, FileUnit *singleFile,

0 commit comments

Comments
 (0)