Skip to content

[clang] Allow parameterized isWeakImport based on an enclosing platform version #10555

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

Draft
wants to merge 1 commit into
base: stable/20240723
Choose a base branch
from
Draft
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: 1 addition & 1 deletion clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ class alignas(8) Decl {
/// 'weak_import' attribute, but may also be marked with an
/// 'availability' attribute where we're targing a platform prior to
/// the introduction of this feature.
bool isWeakImported() const;
bool isWeakImported(VersionTuple EnclosingVersion = VersionTuple()) const;

/// Determines whether this symbol can be weak-imported,
/// e.g., whether it would be well-formed to add the weak_import
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/DeclBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ bool Decl::canBeWeakImported(bool &IsDefinition) const {
return false;
}

bool Decl::isWeakImported() const {
bool Decl::isWeakImported(VersionTuple EnclosingVersion) const {
bool IsDefinition;
if (!canBeWeakImported(IsDefinition))
return false;
Expand All @@ -851,7 +851,7 @@ bool Decl::isWeakImported() const {

if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
if (CheckAvailability(getASTContext(), Availability, nullptr,
VersionTuple()) == AR_NotYetIntroduced)
EnclosingVersion) == AR_NotYetIntroduced)
return true;
} else if (const auto *DA = dyn_cast<DomainAvailabilityAttr>(A)) {
auto DomainName = DA->getDomain();
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2758,14 +2758,15 @@ void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
setNonAliasAttributes(GD, F);
}

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

Expand Down Expand Up @@ -2870,7 +2871,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
// Only a few attributes are set on declarations; these may later be
// overridden by a definition.

setLinkageForGV(F, FD);
setLinkageForGV(F, FD, Target.getTriple().getOSVersion());
setGVProperties(F, FD);

// Setup target-specific attributes.
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/AST/DeclTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ TEST(Decl, Availability) {
clang::AR_Unavailable) {
setFailure("failed obsoleted");
}
if (Node.isWeakImported(clang::VersionTuple(10, 1)) != true) {
setFailure("failed not weak imported");
}
if (Node.isWeakImported(clang::VersionTuple(10, 10)) != false) {
setFailure("failed weak imported");
}

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