Skip to content

Commit 42fe68d

Browse files
committed
[clang] Allow parameterized 'isWeakImport' based on an enclosing platform version
Similarly to 'CheckAvailability' and 'getAvailability', set 'Decl::isWeakImported' to allow querying using an external target platform version. In #7916 we have added support for configuring 'clang::CodeGenerator' with a differently-versioned target info, and this change adopts the code generator's target info in order to also determine weakly-imported linkage on declarations during code-gen. Before this change, they were relying on the 'ASTContext' to specify the target info, which may differ from code-gen's.
1 parent bc2590d commit 42fe68d

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

clang/include/clang/AST/DeclBase.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ class alignas(8) Decl {
775775
/// 'weak_import' attribute, but may also be marked with an
776776
/// 'availability' attribute where we're targing a platform prior to
777777
/// the introduction of this feature.
778-
bool isWeakImported() const;
778+
bool isWeakImported(VersionTuple EnclosingVersion = VersionTuple()) const;
779779

780780
/// Determines whether this symbol can be weak-imported,
781781
/// e.g., whether it would be well-formed to add the weak_import

clang/lib/AST/DeclBase.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ bool Decl::canBeWeakImported(bool &IsDefinition) const {
840840
return false;
841841
}
842842

843-
bool Decl::isWeakImported() const {
843+
bool Decl::isWeakImported(VersionTuple EnclosingVersion) const {
844844
bool IsDefinition;
845845
if (!canBeWeakImported(IsDefinition))
846846
return false;
@@ -851,7 +851,7 @@ bool Decl::isWeakImported() const {
851851

852852
if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
853853
if (CheckAvailability(getASTContext(), Availability, nullptr,
854-
VersionTuple()) == AR_NotYetIntroduced)
854+
EnclosingVersion) == AR_NotYetIntroduced)
855855
return true;
856856
}
857857
}

clang/lib/CodeGen/CodeGenModule.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2808,14 +2808,15 @@ void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
28082808
setNonAliasAttributes(GD, F);
28092809
}
28102810

2811-
static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
2811+
static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND,
2812+
VersionTuple EnclosingVersion = VersionTuple()) {
28122813
// Set linkage and visibility in case we never see a definition.
28132814
LinkageInfo LV = ND->getLinkageAndVisibility();
28142815
// Don't set internal linkage on declarations.
28152816
// "extern_weak" is overloaded in LLVM; we probably should have
28162817
// separate linkage types for this.
28172818
if (isExternallyVisible(LV.getLinkage()) &&
2818-
(ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
2819+
(ND->hasAttr<WeakAttr>() || ND->isWeakImported(EnclosingVersion)))
28192820
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
28202821
}
28212822

@@ -2920,7 +2921,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
29202921
// Only a few attributes are set on declarations; these may later be
29212922
// overridden by a definition.
29222923

2923-
setLinkageForGV(F, FD);
2924+
setLinkageForGV(F, FD, Target.getTriple().getOSVersion());
29242925
setGVProperties(F, FD);
29252926

29262927
// Setup target-specific attributes.

clang/unittests/AST/DeclTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ TEST(Decl, Availability) {
108108
clang::AR_Unavailable) {
109109
setFailure("failed obsoleted");
110110
}
111+
if (Node.isWeakImported(clang::VersionTuple(10, 1)) != true) {
112+
setFailure("failed not weak imported");
113+
}
114+
if (Node.isWeakImported(clang::VersionTuple(10, 10)) != false) {
115+
setFailure("failed weak imported");
116+
}
111117

112118
if (Node.getAvailability() != clang::AR_Deprecated)
113119
setFailure("did not default to target OS version");

0 commit comments

Comments
 (0)