Skip to content

Commit c503f04

Browse files
committed
[ClangImporter] Try to warn about missing types
1 parent 7b8c339 commit c503f04

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
@@ -322,5 +322,15 @@ ERROR(unknown_template_parameter,none,
322322
ERROR(type_template_parameter_expected,none,
323323
"template parameter '%0' expected to be a type parameter", (StringRef))
324324

325+
NOTE(bridged_type_not_found_in_module,none,
326+
"could not find type '%0' for bridging; module '%1' may be broken",
327+
(StringRef, StringRef))
328+
329+
NOTE(bridged_pointer_type_not_found,none,
330+
"could not find type '%select{UnsafeMutableRawPointer|UnsafeRawPointer|"
331+
"UnsafeMutablePointer|UnsafePointer|AutoreleasingUnsafeMutablePointer}0' "
332+
"for bridging; module 'Swift' may be broken",
333+
(unsigned))
334+
325335
#define UNDEFINE_DIAGNOSTIC_MACROS
326336
#include "DefineDiagnosticMacros.h"

lib/ClangImporter/ImportType.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ namespace {
554554
if (auto wrapped = pointeeType->wrapInPointer(pointerKind)) {
555555
return {wrapped, ImportHint::OtherPointer};
556556
} else {
557+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
558+
pointerKind));
557559
return Type();
558560
}
559561
}
@@ -611,8 +613,11 @@ namespace {
611613
pointerKind = PTK_UnsafeMutablePointer;
612614
}
613615

614-
return {pointeeType->wrapInPointer(pointerKind),
615-
ImportHint::None};
616+
auto pointerType = pointeeType->wrapInPointer(pointerKind);
617+
if (!pointerType)
618+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
619+
pointerKind));
620+
return {pointerType, ImportHint::None};
616621
}
617622

618623
ImportResult VisitMemberPointer(const clang::MemberPointerType *type) {
@@ -1414,7 +1419,8 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
14141419

14151420
static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14161421
Type importedType,
1417-
ImportTypeAttrs attrs) {
1422+
ImportTypeAttrs attrs,
1423+
llvm::function_ref<void(Diagnostic &&)> addImportDiagnostic) {
14181424
PointerTypeKind PTK;
14191425
auto elementType = importedType->getAnyPointerElementType(PTK);
14201426
if (!elementType || PTK != PTK_UnsafeMutablePointer)
@@ -1446,6 +1452,9 @@ static Type maybeImportCFOutParameter(ClangImporter::Implementation &impl,
14461452
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
14471453

14481454
resultTy = resultTy->wrapInPointer(pointerKind);
1455+
if (!resultTy)
1456+
addImportDiagnostic(Diagnostic(diag::bridged_pointer_type_not_found,
1457+
pointerKind));
14491458
return resultTy;
14501459
}
14511460

@@ -1483,7 +1492,12 @@ static ImportedType adjustTypeForConcreteImport(
14831492
importKind != ImportTypeKind::Result) {
14841493
return {Type(), false};
14851494
}
1486-
importedType = impl.getNamedSwiftType(impl.getStdlibModule(), "Void");
1495+
importedType = impl.SwiftContext.getVoidType();
1496+
if (!importedType) {
1497+
addImportDiagnostic(Diagnostic(diag::bridged_type_not_found_in_module,
1498+
"Void", "Swift"));
1499+
return {Type(), false};
1500+
}
14871501
break;
14881502

14891503
case ImportHint::ObjCBridged:
@@ -1599,7 +1613,8 @@ static ImportedType adjustTypeForConcreteImport(
15991613
if (attrs.contains(ImportTypeAttr::CFRetainedOutParameter) ||
16001614
attrs.contains(ImportTypeAttr::CFUnretainedOutParameter)) {
16011615
if (Type outParamTy =
1602-
maybeImportCFOutParameter(impl, importedType, attrs)) {
1616+
maybeImportCFOutParameter(impl, importedType, attrs,
1617+
addImportDiagnostic)) {
16031618
importedType = outParamTy;
16041619
break;
16051620
}
@@ -2220,8 +2235,15 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22202235
op == clang::OverloadedOperatorKind::OO_MinusEqual ||
22212236
op == clang::OverloadedOperatorKind::OO_StarEqual ||
22222237
op == clang::OverloadedOperatorKind::OO_SlashEqual) &&
2223-
clangDecl->getReturnType()->isReferenceType())
2224-
return {SwiftContext.getVoidType(), false};
2238+
clangDecl->getReturnType()->isReferenceType()) {
2239+
auto voidTy = SwiftContext.getVoidType();
2240+
if (!voidTy)
2241+
addImportDiagnostic(clangDecl,
2242+
Diagnostic(diag::bridged_type_not_found_in_module,
2243+
"Void", "Swift"),
2244+
clangDecl->getLocation());
2245+
return {voidTy, false};
2246+
}
22252247

22262248
// Fix up optionality.
22272249
OptionalTypeKind OptionalityOfReturn;
@@ -2388,7 +2410,10 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23882410
auto genericType =
23892411
findGenericTypeInGenericDecls(*this, templateParamType, genericParams,
23902412
getImportTypeAttrs(clangDecl), addDiag);
2391-
importedType = {genericType->wrapInPointer(pointerKind), false};
2413+
auto genericPointerType = genericType->wrapInPointer(pointerKind);
2414+
if (!genericPointerType)
2415+
addDiag(Diagnostic(diag::bridged_pointer_type_not_found, pointerKind));
2416+
importedType = {genericPointerType, false};
23922417
} else if (!(isa<clang::RecordType>(returnType) ||
23932418
isa<clang::TemplateSpecializationType>(returnType)) ||
23942419
// TODO: we currently don't lazily load operator return types, but
@@ -2472,8 +2497,11 @@ ClangImporter::Implementation::importParameterType(
24722497
auto genericType = findGenericTypeInGenericDecls(
24732498
*this, templateParamType, genericParams, attrs, addImportDiagnosticFn);
24742499
swiftParamTy = genericType->wrapInPointer(pointerKind);
2475-
if (!swiftParamTy)
2500+
if (!swiftParamTy) {
2501+
addImportDiagnosticFn(Diagnostic(diag::bridged_pointer_type_not_found,
2502+
pointerKind));
24762503
return std::nullopt;
2504+
}
24772505
} else if (isa<clang::ReferenceType>(paramTy) &&
24782506
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
24792507
// We don't support universal reference, bail.
@@ -3515,6 +3543,11 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
35153543

35163544
*params = ParameterList::create(SwiftContext, paramInfo);
35173545
resultTy = SwiftContext.getVoidType();
3546+
if (!resultTy)
3547+
addImportDiagnostic(clangDecl,
3548+
Diagnostic(diag::bridged_type_not_found_in_module,
3549+
"Void", "Swift"),
3550+
clangDecl->getLocation());
35183551
isIUO = false;
35193552
}
35203553

0 commit comments

Comments
 (0)