Skip to content

Commit 43d8b13

Browse files
authored
Merge pull request #82644 from hnrklssn/cherry-pick-macro-adjustment-check
Cherry-pick "Merge pull request #81855 from hnrklssn/swiftify-availability-check-sdk"
2 parents 0022c31 + 9de099a commit 43d8b13

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8966,16 +8966,25 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
89668966
}
89678967

89688968
namespace {
8969+
ValueDecl *getKnownSingleDecl(ASTContext &SwiftContext, StringRef DeclName) {
8970+
SmallVector<ValueDecl *, 1> decls;
8971+
SwiftContext.lookupInSwiftModule(DeclName, decls);
8972+
assert(decls.size() < 2);
8973+
if (decls.size() != 1) return nullptr;
8974+
return decls[0];
8975+
}
8976+
89698977
class SwiftifyInfoPrinter {
89708978
public:
89718979
static const ssize_t SELF_PARAM_INDEX = -2;
89728980
static const ssize_t RETURN_VALUE_INDEX = -1;
89738981
clang::ASTContext &ctx;
89748982
ASTContext &SwiftContext;
89758983
llvm::raw_ostream &out;
8984+
MacroDecl &SwiftifyImportDecl;
89768985
bool firstParam = true;
8977-
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out)
8978-
: ctx(ctx), SwiftContext(SwiftContext), out(out) {
8986+
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out, MacroDecl &SwiftifyImportDecl)
8987+
: ctx(ctx), SwiftContext(SwiftContext), out(out), SwiftifyImportDecl(SwiftifyImportDecl) {
89798988
out << "@_SwiftifyImport(";
89808989
}
89818990
~SwiftifyInfoPrinter() { out << ")"; }
@@ -9033,22 +9042,23 @@ class SwiftifyInfoPrinter {
90339042
}
90349043

90359044
void printAvailability() {
9045+
if (!hasMacroParameter("spanAvailability"))
9046+
return;
90369047
printSeparator();
90379048
out << "spanAvailability: ";
90389049
printAvailabilityOfType("Span");
90399050
}
90409051

90419052
private:
9042-
ValueDecl *getDecl(StringRef DeclName) {
9043-
SmallVector<ValueDecl *, 1> decls;
9044-
SwiftContext.lookupInSwiftModule(DeclName, decls);
9045-
assert(decls.size() == 1);
9046-
if (decls.size() != 1) return nullptr;
9047-
return decls[0];
9053+
bool hasMacroParameter(StringRef ParamName) {
9054+
for (auto *Param : *SwiftifyImportDecl.parameterList)
9055+
if (Param->getArgumentName().str() == ParamName)
9056+
return true;
9057+
return false;
90489058
}
90499059

90509060
void printAvailabilityOfType(StringRef Name) {
9051-
ValueDecl *D = getDecl(Name);
9061+
ValueDecl *D = getKnownSingleDecl(SwiftContext, Name);
90529062
out << "\"";
90539063
llvm::SaveAndRestore<bool> hasAvailbilitySeparatorRestore(firstParam, true);
90549064
for (auto attr : D->getSemanticAvailableAttrs(/*includingInactive=*/true)) {
@@ -9213,6 +9223,10 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
92139223
if (ClangDecl->getNumParams() != MappedDecl->getParameters()->size())
92149224
return;
92159225

9226+
MacroDecl *SwiftifyImportDecl = dyn_cast_or_null<MacroDecl>(getKnownSingleDecl(SwiftContext, "_SwiftifyImport"));
9227+
if (!SwiftifyImportDecl)
9228+
return;
9229+
92169230
{
92179231
UnaliasedInstantiationVisitor visitor;
92189232
visitor.TraverseType(ClangDecl->getType());
@@ -9241,7 +9255,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
92419255
}
92429256
return false;
92439257
};
9244-
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out);
9258+
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out, *SwiftifyImportDecl);
92459259
Type swiftReturnTy;
92469260
if (const auto *funcDecl = dyn_cast<FuncDecl>(MappedDecl))
92479261
swiftReturnTy = funcDecl->getResultInterfaceType();

stdlib/public/core/SwiftifyImport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ public func _swiftifyOverrideLifetime<
104104
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
105105
// should be expressed by a builtin that is hidden within the function body.
106106
dependent
107-
}
107+
}

0 commit comments

Comments
 (0)