Skip to content

Commit 7d044bd

Browse files
committed
safe pointer -> bounds annotated
1 parent 88eda7c commit 7d044bd

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ createFuncOrAccessor(ClangImporter::Implementation &impl, SourceLoc funcLoc,
109109
SourceLoc nameLoc, GenericParamList *genericParams,
110110
ParameterList *bodyParams, Type resultTy, bool async,
111111
bool throws, DeclContext *dc, ClangNode clangNode,
112-
bool hasSafePointer) {
112+
bool hasBoundsAnnotation) {
113113
FuncDecl *decl;
114114
if (accessorInfo) {
115115
decl = AccessorDecl::create(
@@ -125,7 +125,7 @@ createFuncOrAccessor(ClangImporter::Implementation &impl, SourceLoc funcLoc,
125125
genericParams, dc, clangNode);
126126
}
127127
impl.importSwiftAttrAttributes(decl);
128-
if (hasSafePointer)
128+
if (hasBoundsAnnotation)
129129
impl.importBoundsAttributes(decl);
130130

131131
return decl;
@@ -3256,7 +3256,7 @@ namespace {
32563256
return Impl.importFunctionParameterList(
32573257
dc, decl, nonSelfParams, decl->isVariadic(), allowNSUIntegerAsInt,
32583258
argNames, genericParams, /*resultType=*/nullptr,
3259-
/*hasSafePointerParam=*/nullptr);
3259+
/*hasBoundsAnnotatedParam=*/nullptr);
32603260
}
32613261

32623262
Decl *
@@ -3637,7 +3637,7 @@ namespace {
36373637

36383638
bool importFuncWithoutSignature =
36393639
isa<clang::CXXMethodDecl>(decl) && Impl.importSymbolicCXXDecls;
3640-
bool hasSafePointer = false;
3640+
bool hasBoundsAnnotation = false;
36413641
if (!dc->isModuleScopeContext() && !isa<clang::CXXMethodDecl>(decl)) {
36423642
// Handle initializers.
36433643
if (name.getBaseName().isConstructor()) {
@@ -3734,7 +3734,7 @@ namespace {
37343734
importedType = Impl.importFunctionParamsAndReturnType(
37353735
dc, decl, {decl->param_begin(), decl->param_size()},
37363736
decl->isVariadic(), isInSystemModule(dc), name, bodyParams,
3737-
templateParams, &hasSafePointer);
3737+
templateParams, &hasBoundsAnnotation);
37383738
}
37393739

37403740
if (auto *mdecl = dyn_cast<clang::CXXMethodDecl>(decl)) {
@@ -3800,10 +3800,11 @@ namespace {
38003800
} else {
38013801
auto resultTy = importedType.getType();
38023802

3803-
FuncDecl *func = createFuncOrAccessor(
3804-
Impl, loc, accessorInfo, name, nameLoc, genericParams, bodyParams,
3805-
resultTy,
3806-
/*async=*/false, /*throws=*/false, dc, clangNode, hasSafePointer);
3803+
FuncDecl *func =
3804+
createFuncOrAccessor(Impl, loc, accessorInfo, name, nameLoc,
3805+
genericParams, bodyParams, resultTy,
3806+
/*async=*/false, /*throws=*/false, dc,
3807+
clangNode, hasBoundsAnnotation);
38073808
result = func;
38083809

38093810
if (!dc->isModuleScopeContext()) {
@@ -4846,13 +4847,14 @@ namespace {
48464847
}
48474848
}
48484849

4849-
bool hasSafePointer = false; // currently only implemented for functions
4850+
bool hasBoundsAnnotation =
4851+
false; // currently only implemented for functions
48504852
auto result = createFuncOrAccessor(
48514853
Impl,
48524854
/*funcLoc*/ SourceLoc(), accessorInfo, importedName.getDeclName(),
48534855
/*nameLoc*/ SourceLoc(),
48544856
/*genericParams=*/nullptr, bodyParams, resultTy, async, throws, dc,
4855-
decl, hasSafePointer);
4857+
decl, hasBoundsAnnotation);
48564858

48574859
result->setAccess(decl->isDirectMethod() ? AccessLevel::Public
48584860
: getOverridableAccessLevel(dc));
@@ -6496,7 +6498,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
64966498
parameterList = Impl.importFunctionParameterList(
64976499
dc, decl, {decl->param_begin(), decl->param_end()}, decl->isVariadic(),
64986500
allowNSUIntegerAsInt, argNames, /*genericParams=*/{},
6499-
/*resultType=*/nullptr, /*hasSafePointerParam=*/nullptr);
6501+
/*resultType=*/nullptr, /*hasBoundsAnnotatedParam=*/nullptr);
65006502
}
65016503
if (!parameterList)
65026504
return nullptr;

lib/ClangImporter/ImportType.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ namespace {
216216
Bridgeability Bridging;
217217
const clang::FunctionType *CompletionHandlerType;
218218
std::optional<unsigned> CompletionHandlerErrorParamIndex;
219-
bool isSafePointer = false;
219+
bool isBoundsAnnotated = false;
220220

221221
public:
222222
SwiftTypeConverter(ClangImporter::Implementation &impl,
@@ -239,7 +239,7 @@ namespace {
239239
return IR;
240240
}
241241

242-
bool hasSafePointer() { return isSafePointer; }
242+
bool hasBoundsAnnotation() { return isBoundsAnnotated; }
243243

244244
ImportResult VisitType(const Type*) = delete;
245245

@@ -415,17 +415,17 @@ namespace {
415415

416416
ImportResult VisitCountAttributedType(
417417
const clang::CountAttributedType *type) {
418-
isSafePointer = true;
418+
isBoundsAnnotated = true;
419419
return Visit(type->desugar());
420420
}
421421
ImportResult
422422
VisitBoundsAttributedType(const clang::BoundsAttributedType *type) {
423-
isSafePointer = true;
423+
isBoundsAnnotated = true;
424424
return Visit(type->desugar());
425425
}
426426
ImportResult
427427
VisitValueTerminatedType(const clang::ValueTerminatedType *type) {
428-
isSafePointer = true;
428+
isBoundsAnnotated = true;
429429
return Visit(type->desugar());
430430
}
431431

@@ -480,7 +480,7 @@ namespace {
480480
pointeeQualType, ImportTypeKind::Value, addImportDiagnostic,
481481
AllowNSUIntegerAsInt, Bridgeability::None, ImportTypeAttrs(),
482482
OTK_ImplicitlyUnwrappedOptional, /*resugarNSErrorPointer=*/true,
483-
&isSafePointer);
483+
&isBoundsAnnotated);
484484

485485
// If this is imported as a reference type, ignore the innermost pointer.
486486
// (`T *` becomes `T`, but `T **` becomes `UnsafeMutablePointer<T>`.)
@@ -1710,7 +1710,7 @@ ImportedType ClangImporter::Implementation::importType(
17101710
bool allowNSUIntegerAsInt, Bridgeability bridging, ImportTypeAttrs attrs,
17111711
OptionalTypeKind optionality, bool resugarNSErrorPointer,
17121712
std::optional<unsigned> completionHandlerErrorParamIndex,
1713-
bool *isSafePointer) {
1713+
bool *isBoundsAnnotated) {
17141714
if (type.isNull())
17151715
return {Type(), false};
17161716

@@ -1772,8 +1772,8 @@ ImportedType ClangImporter::Implementation::importType(
17721772
*this, addImportDiagnosticFn, allowNSUIntegerAsInt, bridging,
17731773
completionHandlerType, completionHandlerErrorParamIndex);
17741774
auto importResult = converter.Visit(type);
1775-
if (isSafePointer)
1776-
*isSafePointer |= converter.hasSafePointer();
1775+
if (isBoundsAnnotated)
1776+
*isBoundsAnnotated |= converter.hasBoundsAnnotation();
17771777

17781778
// Now fix up the type based on how we're concretely using it.
17791779
auto adjustedType = adjustTypeForConcreteImport(
@@ -1789,11 +1789,12 @@ Type ClangImporter::Implementation::importTypeIgnoreIUO(
17891789
llvm::function_ref<void(Diagnostic &&)> addImportDiagnosticFn,
17901790
bool allowNSUIntegerAsInt, Bridgeability bridging, ImportTypeAttrs attrs,
17911791
OptionalTypeKind optionality, bool resugarNSErrorPointer,
1792-
bool *isSafePointer) {
1792+
bool *isBoundsAnnotated) {
17931793

1794-
auto importedType = importType(
1795-
type, importKind, addImportDiagnosticFn, allowNSUIntegerAsInt, bridging,
1796-
attrs, optionality, resugarNSErrorPointer, std::nullopt, isSafePointer);
1794+
auto importedType =
1795+
importType(type, importKind, addImportDiagnosticFn, allowNSUIntegerAsInt,
1796+
bridging, attrs, optionality, resugarNSErrorPointer,
1797+
std::nullopt, isBoundsAnnotated);
17971798

17981799
return importedType.getType();
17991800
}
@@ -2178,7 +2179,7 @@ applyImportTypeAttrs(ImportTypeAttrs attrs, Type type,
21782179

21792180
ImportedType ClangImporter::Implementation::importFunctionReturnType(
21802181
DeclContext *dc, const clang::FunctionDecl *clangDecl,
2181-
bool allowNSUIntegerAsInt, bool *isSafePointer) {
2182+
bool allowNSUIntegerAsInt, bool *isBoundsAnnotated) {
21822183

21832184
// Hardcode handling of certain result types for builtins.
21842185
if (auto builtinID = clangDecl->getBuiltinID()) {
@@ -2293,7 +2294,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
22932294
: ImportTypeKind::Result),
22942295
ImportDiagnosticAdder(*this, clangDecl, clangDecl->getLocation()),
22952296
allowNSUIntegerAsInt, Bridgeability::Full, getImportTypeAttrs(clangDecl),
2296-
OptionalityOfReturn, isSafePointer);
2297+
OptionalityOfReturn, isBoundsAnnotated);
22972298
}
22982299

22992300
static Type
@@ -2328,7 +2329,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23282329
DeclContext *dc, const clang::FunctionDecl *clangDecl,
23292330
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
23302331
bool isFromSystemModule, DeclName name, ParameterList *&parameterList,
2331-
ArrayRef<GenericTypeParamDecl *> genericParams, bool *hasSafePointer) {
2332+
ArrayRef<GenericTypeParamDecl *> genericParams, bool *hasBoundsAnnotation) {
23322333

23332334
bool allowNSUIntegerAsInt =
23342335
shouldAllowNSUIntegerAsInt(isFromSystemModule, clangDecl);
@@ -2386,7 +2387,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23862387
// was supposed to be used (instead of the typedef type).
23872388
if (!importedType) {
23882389
importedType = importFunctionReturnType(
2389-
dc, clangDecl, allowNSUIntegerAsInt, hasSafePointer);
2390+
dc, clangDecl, allowNSUIntegerAsInt, hasBoundsAnnotation);
23902391
if (!importedType) {
23912392
addDiag(Diagnostic(diag::return_type_not_imported));
23922393
return {Type(), false};
@@ -2398,7 +2399,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23982399
ArrayRef<Identifier> argNames = name.getArgumentNames();
23992400
parameterList = importFunctionParameterList(
24002401
dc, clangDecl, params, isVariadic, allowNSUIntegerAsInt, argNames,
2401-
genericParams, swiftResultTy, hasSafePointer);
2402+
genericParams, swiftResultTy, hasBoundsAnnotation);
24022403
if (!parameterList)
24032404
return {Type(), false};
24042405

@@ -2535,7 +2536,7 @@ ClangImporter::Implementation::importParameterType(
25352536
}
25362537
}
25372538

2538-
bool isSafePointer = false;
2539+
bool isBoundsAnnotated = false;
25392540
if (!swiftParamTy) {
25402541
// If this is the throws error parameter, we don't need to convert any
25412542
// NSError** arguments to the sugared NSErrorPointer typealias form,
@@ -2549,7 +2550,7 @@ ClangImporter::Implementation::importParameterType(
25492550
paramTy, importKind, addImportDiagnosticFn, allowNSUIntegerAsInt,
25502551
Bridgeability::Full, attrs, optionalityOfParam,
25512552
/*resugarNSErrorPointer=*/!paramIsError,
2552-
completionHandlerErrorParamIndex, &isSafePointer);
2553+
completionHandlerErrorParamIndex, &isBoundsAnnotated);
25532554
if (!importedType)
25542555
return std::nullopt;
25552556

@@ -2567,7 +2568,7 @@ ClangImporter::Implementation::importParameterType(
25672568
isInOut = false;
25682569

25692570
return ImportParameterTypeResult{
2570-
swiftParamTy, isInOut, isParamTypeImplicitlyUnwrapped, isSafePointer};
2571+
swiftParamTy, isInOut, isParamTypeImplicitlyUnwrapped, isBoundsAnnotated};
25712572
}
25722573

25732574
bool ClangImporter::Implementation::isDefaultArgSafeToImport(
@@ -2679,7 +2680,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
26792680
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
26802681
bool allowNSUIntegerAsInt, ArrayRef<Identifier> argNames,
26812682
ArrayRef<GenericTypeParamDecl *> genericParams, Type resultType,
2682-
bool *hasSafePointerParam) {
2683+
bool *hasBoundsAnnotatedParam) {
26832684
// Import the parameters.
26842685
SmallVector<ParamDecl *, 4> parameters;
26852686
unsigned index = 0;
@@ -2719,8 +2720,8 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
27192720
bool isInOut = swiftParamTyOpt->isInOut;
27202721
bool isParamTypeImplicitlyUnwrapped =
27212722
swiftParamTyOpt->isParamTypeImplicitlyUnwrapped;
2722-
if (swiftParamTyOpt->isSafePointer && hasSafePointerParam)
2723-
*hasSafePointerParam = true;
2723+
if (swiftParamTyOpt->isBoundsAnnotated && hasBoundsAnnotatedParam)
2724+
*hasBoundsAnnotatedParam = true;
27242725

27252726
// Retrieve the argument name.
27262727
Identifier name;

lib/ClangImporter/ImporterImpl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13581358
OptionalTypeKind optional = OTK_ImplicitlyUnwrappedOptional,
13591359
bool resugarNSErrorPointer = true,
13601360
std::optional<unsigned> completionHandlerErrorParamIndex = std::nullopt,
1361-
bool *isSafePointer = nullptr);
1361+
bool *isBoundsAnnotated = nullptr);
13621362

13631363
/// Import the given Clang type into Swift.
13641364
///
@@ -1375,7 +1375,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13751375
bool allowNSUIntegerAsInt, Bridgeability topLevelBridgeability,
13761376
ImportTypeAttrs attrs,
13771377
OptionalTypeKind optional = OTK_ImplicitlyUnwrappedOptional,
1378-
bool resugarNSErrorPointer = true, bool *isSafePointer = nullptr);
1378+
bool resugarNSErrorPointer = true, bool *isBoundsAnnotated = nullptr);
13791379

13801380
/// Import the given Clang type into Swift, returning the
13811381
/// Swift parameters and result type and whether we should treat it
@@ -1409,7 +1409,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14091409
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
14101410
bool isFromSystemModule, DeclName name, ParameterList *&parameterList,
14111411
ArrayRef<GenericTypeParamDecl *> genericParams,
1412-
bool *hasSafePointerParam);
1412+
bool *hasBoundsAnnotatedParam);
14131413

14141414
/// Import the given function return type.
14151415
///
@@ -1424,7 +1424,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14241424
ImportedType importFunctionReturnType(DeclContext *dc,
14251425
const clang::FunctionDecl *clangDecl,
14261426
bool allowNSUIntegerAsInt,
1427-
bool *isSafePointer = nullptr);
1427+
bool *isBoundsAnnotated = nullptr);
14281428

14291429
/// Import the parameter list for a function
14301430
///
@@ -1442,7 +1442,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14421442
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
14431443
bool allowNSUIntegerAsInt, ArrayRef<Identifier> argNames,
14441444
ArrayRef<GenericTypeParamDecl *> genericParams, Type resultType,
1445-
bool *hasSafePointerParam);
1445+
bool *hasBoundsAnnotatedParam);
14461446

14471447
struct ImportParameterTypeResult {
14481448
/// The imported parameter Swift type.
@@ -1452,7 +1452,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14521452
/// If the parameter is implicitly unwrapped or not.
14531453
bool isParamTypeImplicitlyUnwrapped;
14541454
/// If the parameter has (potentially nested) safe pointer types
1455-
bool isSafePointer;
1455+
bool isBoundsAnnotated;
14561456
};
14571457

14581458
/// Import a parameter type

0 commit comments

Comments
 (0)