Skip to content

Commit a6eec7a

Browse files
committed
[ClangImporter] Try to warn about missing types
1 parent 07583f5 commit a6eec7a

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
@@ -292,5 +292,15 @@ NOTE(forward_declared_protocol_clashes_with_imported_objc_Swift_protocol, none,
292292
WARNING(return_escapable_with_lifetimebound, none, "the returned type '%0' is annotated as escapable; it cannot have lifetime dependencies", (StringRef))
293293
WARNING(return_nonescapable_without_lifetimebound, none, "the returned type '%0' is annotated as non-escapable; its lifetime dependencies must be annotated", (StringRef))
294294

295+
NOTE(bridged_type_not_found_in_module,none,
296+
"could not find type '%0' for bridging; module '%1' may be broken",
297+
(StringRef, StringRef))
298+
299+
NOTE(bridged_pointer_type_not_found,none,
300+
"could not find type '%select{UnsafeMutableRawPointer|UnsafeRawPointer|"
301+
"UnsafeMutablePointer|UnsafePointer|AutoreleasingUnsafeMutablePointer}0' "
302+
"for bridging; module 'Swift' may be broken",
303+
(unsigned))
304+
295305
#define UNDEFINE_DIAGNOSTIC_MACROS
296306
#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) {
@@ -1382,7 +1387,8 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
13821387

13831388
static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
13841389
Type importedType,
1385-
ImportTypeAttrs attrs) {
1390+
ImportTypeAttrs attrs,
1391+
llvm::function_ref<void(Diagnostic &&)> addImportDiagnostic) {
13861392
PointerTypeKind PTK;
13871393
auto elementType = importedType->getAnyPointerElementType(PTK);
13881394
if (!elementType || PTK != PTK_UnsafeMutablePointer)
@@ -1414,6 +1420,9 @@ static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14141420
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
14151421

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

@@ -1451,7 +1460,12 @@ static ImportedType adjustTypeForConcreteImport(
14511460
importKind != ImportTypeKind::Result) {
14521461
return {Type(), false};
14531462
}
1454-
importedType = impl.getNamedSwiftType(impl.getStdlibModule(), "Void");
1463+
importedType = impl.SwiftContext.getVoidType();
1464+
if (!importedType) {
1465+
addImportDiagnostic(Diagnostic(diag::bridged_type_not_found_in_module,
1466+
"Void", "Swift"));
1467+
return {Type(), false};
1468+
}
14551469
break;
14561470

14571471
case ImportHint::ObjCBridged:
@@ -1567,7 +1581,8 @@ static ImportedType adjustTypeForConcreteImport(
15671581
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter) ||
15681582
attrs.contains(ImportTypeAttr::CFUnretainedOutParameter)) {
15691583
if (Type outParamTy =
1570-
maybeImportCFOutParameter(impl, importedType, attrs)) {
1584+
maybeImportCFOutParameter(impl, importedType, attrs,
1585+
addImportDiagnostic)) {
15711586
importedType = outParamTy;
15721587
break;
15731588
}
@@ -2184,8 +2199,15 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
21842199
op == clang::OverloadedOperatorKind::OO_MinusEqual ||
21852200
op == clang::OverloadedOperatorKind::OO_StarEqual ||
21862201
op == clang::OverloadedOperatorKind::OO_SlashEqual) &&
2187-
clangDecl->getReturnType()->isReferenceType())
2188-
return {SwiftContext.getVoidType(), false};
2202+
clangDecl->getReturnType()->isReferenceType()) {
2203+
auto voidTy = SwiftContext.getVoidType();
2204+
if (!voidTy)
2205+
addImportDiagnostic(clangDecl,
2206+
Diagnostic(diag::bridged_type_not_found_in_module,
2207+
"Void", "Swift"),
2208+
clangDecl->getLocation());
2209+
return {voidTy, false};
2210+
}
21892211

21902212
// Fix up optionality.
21912213
OptionalTypeKind OptionalityOfReturn;
@@ -2352,7 +2374,10 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23522374
auto genericType =
23532375
findGenericTypeInGenericDecls(*this, templateParamType, genericParams,
23542376
getImportTypeAttrs(clangDecl), addDiag);
2355-
importedType = {genericType->wrapInPointer(pointerKind), false};
2377+
auto genericPointerType = genericType->wrapInPointer(pointerKind);
2378+
if (!genericPointerType)
2379+
addDiag(Diagnostic(diag::bridged_pointer_type_not_found, pointerKind));
2380+
importedType = {genericPointerType, false};
23562381
} else if (!(isa<clang::RecordType>(returnType) ||
23572382
isa<clang::TemplateSpecializationType>(returnType)) ||
23582383
// TODO: we currently don't lazily load operator return types, but
@@ -2435,8 +2460,11 @@ ClangImporter::Implementation::importParameterType(
24352460
auto genericType = findGenericTypeInGenericDecls(
24362461
*this, templateParamType, genericParams, attrs, addImportDiagnosticFn);
24372462
swiftParamTy = genericType->wrapInPointer(pointerKind);
2438-
if (!swiftParamTy)
2463+
if (!swiftParamTy) {
2464+
addImportDiagnosticFn(Diagnostic(diag::bridged_pointer_type_not_found,
2465+
pointerKind));
24392466
return std::nullopt;
2467+
}
24402468
} else if (isa<clang::ReferenceType>(paramTy) &&
24412469
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
24422470
// We don't support rvalue reference / universal perfect ref, bail.
@@ -3459,6 +3487,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
34593487

34603488
*params = ParameterList::create(SwiftContext, paramInfo);
34613489
resultTy = SwiftContext.getVoidType();
3490+
if (!resultTy)
3491+
addImportDiagnostic(clangDecl,
3492+
Diagnostic(diag::bridged_type_not_found_in_module,
3493+
"Void", "Swift"),
3494+
clangDecl->getLocation());
34623495
isIUO = false;
34633496
}
34643497

0 commit comments

Comments
 (0)