Skip to content

Commit f4257ed

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 6b0c110 commit f4257ed

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
@@ -771,7 +771,7 @@ class alignas(8) Decl {
771771
/// 'weak_import' attribute, but may also be marked with an
772772
/// 'availability' attribute where we're targing a platform prior to
773773
/// the introduction of this feature.
774-
bool isWeakImported() const;
774+
bool isWeakImported(VersionTuple EnclosingVersion = VersionTuple()) const;
775775

776776
/// Determines whether this symbol can be weak-imported,
777777
/// 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
@@ -846,7 +846,7 @@ bool Decl::canBeWeakImported(bool &IsDefinition) const {
846846
return false;
847847
}
848848

849-
bool Decl::isWeakImported() const {
849+
bool Decl::isWeakImported(VersionTuple EnclosingVersion) const {
850850
bool IsDefinition;
851851
if (!canBeWeakImported(IsDefinition))
852852
return false;
@@ -857,7 +857,7 @@ bool Decl::isWeakImported() const {
857857

858858
if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
859859
if (CheckAvailability(getASTContext(), Availability, nullptr,
860-
VersionTuple()) == AR_NotYetIntroduced)
860+
EnclosingVersion) == AR_NotYetIntroduced)
861861
return true;
862862
} else if (const auto *DA = dyn_cast<DomainAvailabilityAttr>(A)) {
863863
auto DomainName = DA->getDomain();

clang/lib/CodeGen/CodeGenModule.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2860,14 +2860,15 @@ void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
28602860
setNonAliasAttributes(GD, F);
28612861
}
28622862

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

@@ -2971,7 +2972,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
29712972
// Only a few attributes are set on declarations; these may later be
29722973
// overridden by a definition.
29732974

2974-
setLinkageForGV(F, FD);
2975+
setLinkageForGV(F, FD, Target.getTriple().getOSVersion());
29752976
setGVProperties(F, FD);
29762977

29772978
// 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)