Skip to content

Commit f509dcb

Browse files
committed
[ClangImporter] Try to warn about missing types
1 parent cdd8fe9 commit f509dcb

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
@@ -289,5 +289,15 @@ NOTE(forward_declared_protocol_clashes_with_imported_objc_Swift_protocol, none,
289289
"its name conflicts with a %1 in module %2",
290290
(const clang::NamedDecl*, StringRef, StringRef))
291291

292+
NOTE(bridged_type_not_found_in_module,none,
293+
"could not find type '%0' for bridging; module '%1' may be broken",
294+
(StringRef, StringRef))
295+
296+
NOTE(bridged_pointer_type_not_found,none,
297+
"could not find type '%select{UnsafeMutableRawPointer|UnsafeRawPointer|"
298+
"UnsafeMutablePointer|UnsafePointer|AutoreleasingUnsafeMutablePointer}0' "
299+
"for bridging; module 'Swift' may be broken",
300+
(unsigned))
301+
292302
#define UNDEFINE_DIAGNOSTIC_MACROS
293303
#include "DefineDiagnosticMacros.h"

lib/ClangImporter/ImportType.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ namespace {
558558
if (auto wrapped = pointeeType->wrapInPointer(pointerKind)) {
559559
return {wrapped, ImportHint::OtherPointer};
560560
} else {
561+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
562+
pointerKind));
561563
return Type();
562564
}
563565
}
@@ -615,8 +617,11 @@ namespace {
615617
pointerKind = PTK_UnsafeMutablePointer;
616618
}
617619

618-
return {pointeeType->wrapInPointer(pointerKind),
619-
ImportHint::None};
620+
auto pointerType = pointeeType->wrapInPointer(pointerKind);
621+
if (!pointerType)
622+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
623+
pointerKind));
624+
return {pointerType, ImportHint::None};
620625
}
621626

622627
ImportResult VisitMemberPointer(const clang::MemberPointerType *type) {
@@ -1405,7 +1410,8 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
14051410

14061411
static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14071412
Type importedType,
1408-
ImportTypeAttrs attrs) {
1413+
ImportTypeAttrs attrs,
1414+
llvm::function_ref<void(Diagnostic &&)> addImportDiagnostic) {
14091415
PointerTypeKind PTK;
14101416
auto elementType = importedType->getAnyPointerElementType(PTK);
14111417
if (!elementType || PTK != PTK_UnsafeMutablePointer)
@@ -1437,6 +1443,9 @@ static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14371443
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
14381444

14391445
resultTy = resultTy->wrapInPointer(pointerKind);
1446+
if (!resultTy)
1447+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
1448+
pointerKind));
14401449
return resultTy;
14411450
}
14421451

@@ -1474,7 +1483,12 @@ static ImportedType adjustTypeForConcreteImport(
14741483
importKind != ImportTypeKind::Result) {
14751484
return {Type(), false};
14761485
}
1477-
importedType = impl.getNamedSwiftType(impl.getStdlibModule(), "Void");
1486+
importedType = impl.SwiftContext.getVoidType();
1487+
if (!importedType) {
1488+
addImportDiagnostic(Diagnostic(diag::bridged_type_not_found_in_module,
1489+
"Void", "Swift"));
1490+
return {Type(), false};
1491+
}
14781492
break;
14791493

14801494
case ImportHint::ObjCBridged:
@@ -1590,7 +1604,8 @@ static ImportedType adjustTypeForConcreteImport(
15901604
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter) ||
15911605
attrs.contains(ImportTypeAttr::CFUnretainedOutParameter)) {
15921606
if (Type outParamTy =
1593-
maybeImportCFOutParameter(impl, importedType, attrs)) {
1607+
maybeImportCFOutParameter(impl, importedType, attrs,
1608+
addImportDiagnostic)) {
15941609
importedType = outParamTy;
15951610
break;
15961611
}
@@ -2206,8 +2221,15 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22062221
if (op == clang::OverloadedOperatorKind::OO_PlusEqual ||
22072222
op == clang::OverloadedOperatorKind::OO_MinusEqual ||
22082223
op == clang::OverloadedOperatorKind::OO_StarEqual ||
2209-
op == clang::OverloadedOperatorKind::OO_SlashEqual)
2210-
return {SwiftContext.getVoidType(), false};
2224+
op == clang::OverloadedOperatorKind::OO_SlashEqual) {
2225+
auto voidTy = SwiftContext.getVoidType();
2226+
if (!voidTy)
2227+
addImportDiagnostic(clangDecl,
2228+
Diagnostic(diag::bridged_type_not_found_in_module,
2229+
"Void", "Swift"),
2230+
clangDecl->getLocation());
2231+
return {voidTy, false};
2232+
}
22112233

22122234
// Fix up optionality.
22132235
OptionalTypeKind OptionalityOfReturn;
@@ -2374,7 +2396,10 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23742396
auto genericType =
23752397
findGenericTypeInGenericDecls(*this, templateParamType, genericParams,
23762398
getImportTypeAttrs(clangDecl), addDiag);
2377-
importedType = {genericType->wrapInPointer(pointerKind), false};
2399+
auto genericPointerType = genericType->wrapInPointer(pointerKind);
2400+
if (!genericPointerType)
2401+
addDiag(Diagnostic(diag::bridged_pointer_type_not_found, pointerKind));
2402+
importedType = {genericPointerType, false};
23782403
} else if (!(isa<clang::RecordType>(returnType) ||
23792404
isa<clang::TemplateSpecializationType>(returnType)) ||
23802405
// TODO: we currently don't lazily load operator return types, but
@@ -2457,8 +2482,11 @@ ClangImporter::Implementation::importParameterType(
24572482
auto genericType = findGenericTypeInGenericDecls(
24582483
*this, templateParamType, genericParams, attrs, addImportDiagnosticFn);
24592484
swiftParamTy = genericType->wrapInPointer(pointerKind);
2460-
if (!swiftParamTy)
2485+
if (!swiftParamTy) {
2486+
addImportDiagnosticFn(Diagnostic(diag::bridged_pointer_type_not_found,
2487+
pointerKind));
24612488
return std::nullopt;
2489+
}
24622490
} else if (isa<clang::ReferenceType>(paramTy) &&
24632491
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
24642492
// We don't support rvalue reference / universal perfect ref, bail.
@@ -3481,6 +3509,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
34813509

34823510
*params = ParameterList::create(SwiftContext, paramInfo);
34833511
resultTy = SwiftContext.getVoidType();
3512+
if (!resultTy)
3513+
addImportDiagnostic(clangDecl,
3514+
Diagnostic(diag::bridged_type_not_found_in_module,
3515+
"Void", "Swift"),
3516+
clangDecl->getLocation());
34843517
isIUO = false;
34853518
}
34863519

0 commit comments

Comments
 (0)