Skip to content

Commit 1d1c642

Browse files
committed
Don't attach @_SwiftifyImport if macro declaration not found
1 parent bd11926 commit 1d1c642

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8988,16 +8988,25 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
89888988
}
89898989

89908990
namespace {
8991+
ValueDecl *getKnownSingleDecl(ASTContext &SwiftContext, StringRef DeclName) {
8992+
SmallVector<ValueDecl *, 1> decls;
8993+
SwiftContext.lookupInSwiftModule(DeclName, decls);
8994+
assert(decls.size() < 2);
8995+
if (decls.size() != 1) return nullptr;
8996+
return decls[0];
8997+
}
8998+
89918999
class SwiftifyInfoPrinter {
89929000
public:
89939001
static const ssize_t SELF_PARAM_INDEX = -2;
89949002
static const ssize_t RETURN_VALUE_INDEX = -1;
89959003
clang::ASTContext &ctx;
89969004
ASTContext &SwiftContext;
89979005
llvm::raw_ostream &out;
9006+
MacroDecl &SwiftifyImportDecl;
89989007
bool firstParam = true;
8999-
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out)
9000-
: ctx(ctx), SwiftContext(SwiftContext), out(out) {
9008+
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out, MacroDecl &SwiftifyImportDecl)
9009+
: ctx(ctx), SwiftContext(SwiftContext), out(out), SwiftifyImportDecl(SwiftifyImportDecl) {
90019010
out << "@_SwiftifyImport(";
90029011
}
90039012
~SwiftifyInfoPrinter() { out << ")"; }
@@ -9064,27 +9073,14 @@ class SwiftifyInfoPrinter {
90649073

90659074
private:
90669075
bool hasMacroParameter(StringRef ParamName) {
9067-
ValueDecl *D = getDecl("_SwiftifyImport");
9068-
auto *SwiftifyImportDecl = dyn_cast_or_null<MacroDecl>(D);
9069-
assert(SwiftifyImportDecl);
9070-
if (!SwiftifyImportDecl)
9071-
return false;
9072-
for (auto *Param : *SwiftifyImportDecl->parameterList)
9076+
for (auto *Param : *SwiftifyImportDecl.parameterList)
90739077
if (Param->getArgumentName().str() == ParamName)
90749078
return true;
90759079
return false;
90769080
}
90779081

9078-
ValueDecl *getDecl(StringRef DeclName) {
9079-
SmallVector<ValueDecl *, 1> decls;
9080-
SwiftContext.lookupInSwiftModule(DeclName, decls);
9081-
assert(decls.size() == 1);
9082-
if (decls.size() != 1) return nullptr;
9083-
return decls[0];
9084-
}
9085-
90869082
void printAvailabilityOfType(StringRef Name) {
9087-
ValueDecl *D = getDecl(Name);
9083+
ValueDecl *D = getKnownSingleDecl(SwiftContext, Name);
90889084
out << "\"";
90899085
llvm::SaveAndRestore<bool> hasAvailbilitySeparatorRestore(firstParam, true);
90909086
for (auto attr : D->getSemanticAvailableAttrs(/*includingInactive=*/true)) {
@@ -9248,6 +9244,10 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
92489244
if (ClangDecl->getNumParams() != MappedDecl->getParameters()->size())
92499245
return;
92509246

9247+
MacroDecl *SwiftifyImportDecl = dyn_cast_or_null<MacroDecl>(getKnownSingleDecl(SwiftContext, "_SwiftifyImport"));
9248+
if (!SwiftifyImportDecl)
9249+
return;
9250+
92519251
{
92529252
UnaliasedInstantiationVisitor visitor;
92539253
visitor.TraverseType(ClangDecl->getType());
@@ -9276,7 +9276,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
92769276
}
92779277
return false;
92789278
};
9279-
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out);
9279+
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out, *SwiftifyImportDecl);
92809280
Type swiftReturnTy;
92819281
if (const auto *funcDecl = dyn_cast<FuncDecl>(MappedDecl))
92829282
swiftReturnTy = funcDecl->getResultInterfaceType();

0 commit comments

Comments
 (0)