Skip to content

Commit 7581b0a

Browse files
committed
[ClangImporter] Try to warn about missing types
1 parent 4726543 commit 7581b0a

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,15 @@ NOTE(forward_declared_protocol_clashes_with_imported_objc_Swift_protocol, none,
308308
WARNING(return_escapable_with_lifetimebound, none, "the returned type '%0' is annotated as escapable; it cannot have lifetime dependencies", (StringRef))
309309
WARNING(return_nonescapable_without_lifetimebound, none, "the returned type '%0' is annotated as non-escapable; its lifetime dependencies must be annotated", (StringRef))
310310

311+
NOTE(bridged_type_not_found_in_module,none,
312+
"could not find type '%0' for bridging; module '%1' may be broken",
313+
(StringRef, StringRef))
314+
315+
NOTE(bridged_pointer_type_not_found,none,
316+
"could not find type '%select{UnsafeMutableRawPointer|UnsafeRawPointer|"
317+
"UnsafeMutablePointer|UnsafePointer|AutoreleasingUnsafeMutablePointer}0' "
318+
"for bridging; module 'Swift' may be broken",
319+
(unsigned))
320+
311321
#define UNDEFINE_DIAGNOSTIC_MACROS
312322
#include "DefineDiagnosticMacros.h"

lib/ClangImporter/ImportType.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ namespace {
531531
if (auto wrapped = pointeeType->wrapInPointer(pointerKind)) {
532532
return {wrapped, ImportHint::OtherPointer};
533533
} else {
534+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
535+
pointerKind));
534536
return Type();
535537
}
536538
}
@@ -588,8 +590,11 @@ namespace {
588590
pointerKind = PTK_UnsafeMutablePointer;
589591
}
590592

591-
return {pointeeType->wrapInPointer(pointerKind),
592-
ImportHint::None};
593+
auto pointerType = pointeeType->wrapInPointer(pointerKind);
594+
if (!pointerType)
595+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
596+
pointerKind));
597+
return {pointerType, ImportHint::None};
593598
}
594599

595600
ImportResult VisitMemberPointer(const clang::MemberPointerType *type) {
@@ -1381,7 +1386,8 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
13811386

13821387
static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
13831388
Type importedType,
1384-
ImportTypeAttrs attrs) {
1389+
ImportTypeAttrs attrs,
1390+
llvm::function_ref<void(Diagnostic &&)> addImportDiagnostic) {
13851391
PointerTypeKind PTK;
13861392
auto elementType = importedType->getAnyPointerElementType(PTK);
13871393
if (!elementType || PTK != PTK_UnsafeMutablePointer)
@@ -1413,6 +1419,9 @@ static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14131419
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
14141420

14151421
resultTy = resultTy->wrapInPointer(pointerKind);
1422+
if (!resultTy)
1423+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
1424+
pointerKind));
14161425
return resultTy;
14171426
}
14181427

@@ -1450,7 +1459,12 @@ static ImportedType adjustTypeForConcreteImport(
14501459
importKind != ImportTypeKind::Result) {
14511460
return {Type(), false};
14521461
}
1453-
importedType = impl.getNamedSwiftType(impl.getStdlibModule(), "Void");
1462+
importedType = impl.SwiftContext.getVoidType();
1463+
if (!importedType) {
1464+
addImportDiagnostic(Diagnostic(diag::bridged_type_not_found_in_module,
1465+
"Void", "Swift"));
1466+
return {Type(), false};
1467+
}
14541468
break;
14551469

14561470
case ImportHint::ObjCBridged:
@@ -1566,7 +1580,8 @@ static ImportedType adjustTypeForConcreteImport(
15661580
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter) ||
15671581
attrs.contains(ImportTypeAttr::CFUnretainedOutParameter)) {
15681582
if (Type outParamTy =
1569-
maybeImportCFOutParameter(impl, importedType, attrs)) {
1583+
maybeImportCFOutParameter(impl, importedType, attrs,
1584+
addImportDiagnostic)) {
15701585
importedType = outParamTy;
15711586
break;
15721587
}
@@ -2183,8 +2198,15 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
21832198
op == clang::OverloadedOperatorKind::OO_MinusEqual ||
21842199
op == clang::OverloadedOperatorKind::OO_StarEqual ||
21852200
op == clang::OverloadedOperatorKind::OO_SlashEqual) &&
2186-
clangDecl->getReturnType()->isReferenceType())
2187-
return {SwiftContext.getVoidType(), false};
2201+
clangDecl->getReturnType()->isReferenceType()) {
2202+
auto voidTy = SwiftContext.getVoidType();
2203+
if (!voidTy)
2204+
addImportDiagnostic(clangDecl,
2205+
Diagnostic(diag::bridged_type_not_found_in_module,
2206+
"Void", "Swift"),
2207+
clangDecl->getLocation());
2208+
return {voidTy, false};
2209+
}
21882210

21892211
// Fix up optionality.
21902212
OptionalTypeKind OptionalityOfReturn;
@@ -2351,7 +2373,10 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23512373
auto genericType =
23522374
findGenericTypeInGenericDecls(*this, templateParamType, genericParams,
23532375
getImportTypeAttrs(clangDecl), addDiag);
2354-
importedType = {genericType->wrapInPointer(pointerKind), false};
2376+
auto genericPointerType = genericType->wrapInPointer(pointerKind);
2377+
if (!genericPointerType)
2378+
addDiag(Diagnostic(diag::bridged_pointer_type_not_found, pointerKind));
2379+
importedType = {genericPointerType, false};
23552380
} else if (!(isa<clang::RecordType>(returnType) ||
23562381
isa<clang::TemplateSpecializationType>(returnType)) ||
23572382
// TODO: we currently don't lazily load operator return types, but
@@ -2434,8 +2459,11 @@ ClangImporter::Implementation::importParameterType(
24342459
auto genericType = findGenericTypeInGenericDecls(
24352460
*this, templateParamType, genericParams, attrs, addImportDiagnosticFn);
24362461
swiftParamTy = genericType->wrapInPointer(pointerKind);
2437-
if (!swiftParamTy)
2462+
if (!swiftParamTy) {
2463+
addImportDiagnosticFn(Diagnostic(diag::bridged_pointer_type_not_found,
2464+
pointerKind));
24382465
return std::nullopt;
2466+
}
24392467
} else if (isa<clang::ReferenceType>(paramTy) &&
24402468
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
24412469
// We don't support rvalue reference / universal perfect ref, bail.
@@ -3466,6 +3494,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
34663494

34673495
*params = ParameterList::create(SwiftContext, paramInfo);
34683496
resultTy = SwiftContext.getVoidType();
3497+
if (!resultTy)
3498+
addImportDiagnostic(clangDecl,
3499+
Diagnostic(diag::bridged_type_not_found_in_module,
3500+
"Void", "Swift"),
3501+
clangDecl->getLocation());
34693502
isIUO = false;
34703503
}
34713504

0 commit comments

Comments
 (0)