Skip to content

Commit 34b18ab

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 0dc2e26 commit 34b18ab

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
@@ -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
} else if (const auto *DA = dyn_cast<DomainAvailabilityAttr>(A)) {
857857
auto DomainName = DA->getDomain();

clang/lib/CodeGen/CodeGenModule.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2758,14 +2758,15 @@ void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
27582758
setNonAliasAttributes(GD, F);
27592759
}
27602760

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

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

2873-
setLinkageForGV(F, FD);
2874+
setLinkageForGV(F, FD, Target.getPlatformMinVersion());
28742875
setGVProperties(F, FD);
28752876

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