Skip to content

Commit 6f71581

Browse files
committed
[NFC] Thread import diagnostics through ImportType
`ClangImporter::Implementation::importType()` and associated parts of the importer are now passed an `llvm::function_ref` they can use to emit import diagnostics on the declaration they’re importing, and the `ImportDiagnosticAdder` helper class provides a convenient way to construct such a function. This capability is not actually *used* in this commit—we are simply threading this function through the importer—so there is no change in behavior.
1 parent 1b4b5f5 commit 6f71581

File tree

4 files changed

+124
-62
lines changed

4 files changed

+124
-62
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,8 +2846,9 @@ namespace {
28462846
// or the original C type.
28472847
clang::QualType ClangType = Decl->getUnderlyingType();
28482848
SwiftType = Impl.importTypeIgnoreIUO(
2849-
ClangType, ImportTypeKind::Typedef, isInSystemModule(DC),
2850-
getTypedefBridgeability(Decl), OTK_Optional);
2849+
ClangType, ImportTypeKind::Typedef,
2850+
ImportDiagnosticAdder(Impl, Decl, Decl->getLocation()),
2851+
isInSystemModule(DC), getTypedefBridgeability(Decl), OTK_Optional);
28512852
}
28522853

28532854
if (!SwiftType)
@@ -2950,6 +2951,7 @@ namespace {
29502951
auto name = importedName.getDeclName().getBaseIdentifier();
29512952

29522953
// Create the enum declaration and record it.
2954+
ImportDiagnosticAdder addDiag(Impl, decl, decl->getLocation());
29532955
StructDecl *errorWrapper = nullptr;
29542956
NominalTypeDecl *result;
29552957
auto enumInfo = Impl.getEnumInfo(decl);
@@ -2964,8 +2966,8 @@ namespace {
29642966
case EnumKind::Unknown: {
29652967
// Compute the underlying type of the enumeration.
29662968
auto underlyingType = Impl.importTypeIgnoreIUO(
2967-
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
2968-
Bridgeability::None);
2969+
decl->getIntegerType(), ImportTypeKind::Enum, addDiag,
2970+
isInSystemModule(dc), Bridgeability::None);
29692971
if (!underlyingType)
29702972
return nullptr;
29712973

@@ -2997,8 +2999,8 @@ namespace {
29972999

29983000
// Compute the underlying type.
29993001
auto underlyingType = Impl.importTypeIgnoreIUO(
3000-
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
3001-
Bridgeability::None);
3002+
decl->getIntegerType(), ImportTypeKind::Enum, addDiag,
3003+
isInSystemModule(dc), Bridgeability::None);
30023004
if (!underlyingType)
30033005
return nullptr;
30043006

@@ -3929,6 +3931,7 @@ namespace {
39293931
auto &clangContext = Impl.getClangASTContext();
39303932
auto type = Impl.importTypeIgnoreIUO(
39313933
clangContext.getTagDeclType(clangEnum), ImportTypeKind::Value,
3934+
ImportDiagnosticAdder(Impl, clangEnum, clangEnum->getLocation()),
39323935
isInSystemModule(dc), Bridgeability::None);
39333936
if (!type)
39343937
return nullptr;
@@ -3995,6 +3998,7 @@ namespace {
39953998

39963999
auto importedType =
39974000
Impl.importType(decl->getType(), ImportTypeKind::Variable,
4001+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
39984002
isInSystemModule(dc), Bridgeability::None);
39994003
if (!importedType)
40004004
return nullptr;
@@ -4517,6 +4521,7 @@ namespace {
45174521

45184522
auto importedType =
45194523
Impl.importType(decl->getType(), ImportTypeKind::RecordField,
4524+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
45204525
isInSystemModule(dc), Bridgeability::None);
45214526
if (!importedType) {
45224527
Impl.addImportDiagnostic(
@@ -4643,6 +4648,7 @@ namespace {
46434648
Impl.importType(declType,
46444649
(isAudited ? ImportTypeKind::AuditedVariable
46454650
: ImportTypeKind::Variable),
4651+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
46464652
isInSystemModule(dc), Bridgeability::None);
46474653

46484654
if (!importedType)
@@ -5844,8 +5850,9 @@ namespace {
58445850
clangSuperclassType =
58455851
clangCtx.getObjCObjectPointerType(clangSuperclassType);
58465852
superclassType = Impl.importTypeIgnoreIUO(
5847-
clangSuperclassType, ImportTypeKind::Abstract, isInSystemModule(dc),
5848-
Bridgeability::None);
5853+
clangSuperclassType, ImportTypeKind::Abstract,
5854+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
5855+
isInSystemModule(dc), Bridgeability::None);
58495856
if (superclassType) {
58505857
assert(superclassType->is<ClassType>() ||
58515858
superclassType->is<BoundGenericClassType>());
@@ -6439,9 +6446,10 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
64396446
decl, AccessLevel::Public, Loc, name, Loc, None, nullptr, dc);
64406447

64416448
// Import the type of the underlying storage
6449+
ImportDiagnosticAdder addImportDiag(Impl, decl, decl->getLocation());
64426450
auto storedUnderlyingType = Impl.importTypeIgnoreIUO(
6443-
decl->getUnderlyingType(), ImportTypeKind::Value, isInSystemModule(dc),
6444-
Bridgeability::None, OTK_None);
6451+
decl->getUnderlyingType(), ImportTypeKind::Value, addImportDiag,
6452+
isInSystemModule(dc), Bridgeability::None, OTK_None);
64456453

64466454
if (!storedUnderlyingType)
64476455
return nullptr;
@@ -6461,8 +6469,8 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
64616469

64626470
// Find a bridged type, which may be different
64636471
auto computedPropertyUnderlyingType = Impl.importTypeIgnoreIUO(
6464-
decl->getUnderlyingType(), ImportTypeKind::Property, isInSystemModule(dc),
6465-
Bridgeability::Full, OTK_None);
6472+
decl->getUnderlyingType(), ImportTypeKind::Property, addImportDiag,
6473+
isInSystemModule(dc), Bridgeability::Full, OTK_None);
64666474
if (auto objTy = computedPropertyUnderlyingType->getOptionalObjectType())
64676475
computedPropertyUnderlyingType = objTy;
64686476

@@ -6721,8 +6729,9 @@ SwiftDeclConverter::importAsOptionSetType(DeclContext *dc, Identifier name,
67216729

67226730
// Compute the underlying type.
67236731
auto underlyingType = Impl.importTypeIgnoreIUO(
6724-
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
6725-
Bridgeability::None);
6732+
decl->getIntegerType(), ImportTypeKind::Enum,
6733+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
6734+
isInSystemModule(dc), Bridgeability::None);
67266735
if (!underlyingType)
67276736
return nullptr;
67286737

@@ -6941,6 +6950,7 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
69416950
bool isFromSystemModule = isInSystemModule(dc);
69426951
auto importedType = Impl.importType(
69436952
propertyType, ImportTypeKind::Property,
6953+
ImportDiagnosticAdder(Impl, getter, getter->getLocation()),
69446954
Impl.shouldAllowNSUIntegerAsInt(isFromSystemModule, getter),
69456955
Bridgeability::Full, OTK_ImplicitlyUnwrappedOptional);
69466956
if (!importedType)
@@ -8127,6 +8137,7 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
81278137
clangBound->stripObjCKindOfTypeAndQuals(Impl.getClangASTContext());
81288138
Type superclassType = Impl.importTypeIgnoreIUO(
81298139
clang::QualType(unqualifiedClangBound, 0), ImportTypeKind::Abstract,
8140+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
81308141
false, Bridgeability::None);
81318142
if (!superclassType) {
81328143
return None;

lib/ClangImporter/ImportMacro.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
102102
if (const clang::Expr *parsed = parseNumericLiteral<>(Impl, tok)) {
103103
auto clangTy = parsed->getType();
104104
auto literalType = Impl.importTypeIgnoreIUO(
105-
clangTy, ImportTypeKind::Value, isInSystemModule(DC),
106-
Bridgeability::None);
105+
clangTy, ImportTypeKind::Value,
106+
ImportDiagnosticAdder(Impl, MI, tok.getLocation()),
107+
isInSystemModule(DC), Bridgeability::None);
107108
if (!literalType)
108109
return nullptr;
109110

@@ -112,8 +113,9 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
112113
constantType = literalType;
113114
} else {
114115
constantType = Impl.importTypeIgnoreIUO(
115-
castType, ImportTypeKind::Value, isInSystemModule(DC),
116-
Bridgeability::None);
116+
castType, ImportTypeKind::Value,
117+
ImportDiagnosticAdder(Impl, MI, MI->getDefinitionLoc()),
118+
isInSystemModule(DC), Bridgeability::None);
117119
if (!constantType)
118120
return nullptr;
119121
}
@@ -308,6 +310,7 @@ static Optional<clang::QualType> builtinTypeForToken(const clang::Token &tok,
308310

309311
static Optional<std::pair<llvm::APSInt, Type>>
310312
getIntegerConstantForMacroToken(ClangImporter::Implementation &impl,
313+
const clang::MacroInfo *macro,
311314
DeclContext *DC,
312315
const clang::Token &token) {
313316

@@ -317,8 +320,9 @@ static Optional<std::pair<llvm::APSInt, Type>>
317320
auto value = llvm::APSInt { literal->getValue(),
318321
literal->getType()->isUnsignedIntegerType() };
319322
auto type = impl.importTypeIgnoreIUO(
320-
literal->getType(), ImportTypeKind::Value, isInSystemModule(DC),
321-
Bridgeability::None);
323+
literal->getType(), ImportTypeKind::Value,
324+
ImportDiagnosticAdder(impl, macro, token.getLocation()),
325+
isInSystemModule(DC), Bridgeability::None);
322326
return {{ value, type }};
323327
}
324328

@@ -551,7 +555,8 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
551555
// Parse INT1.
552556
llvm::APSInt firstValue;
553557
Type firstSwiftType = nullptr;
554-
if (auto firstInt = getIntegerConstantForMacroToken(impl, DC, tokenI[0])) {
558+
if (auto firstInt = getIntegerConstantForMacroToken(impl, macro, DC,
559+
tokenI[0])) {
555560
firstValue = firstInt->first;
556561
firstSwiftType = firstInt->second;
557562
} else {
@@ -566,7 +571,8 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
566571
// Parse INT2.
567572
llvm::APSInt secondValue;
568573
Type secondSwiftType = nullptr;
569-
if (auto secondInt = getIntegerConstantForMacroToken(impl, DC, tokenI[2])) {
574+
if (auto secondInt = getIntegerConstantForMacroToken(impl, macro, DC,
575+
tokenI[2])) {
570576
secondValue = secondInt->first;
571577
secondSwiftType = secondInt->second;
572578
} else {

0 commit comments

Comments
 (0)