From 253e624dd9935c5c7f80f95b7d156c9c9a166f11 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 31 Aug 2023 17:23:00 -0700 Subject: [PATCH 1/2] Basic: avoid truncation `Swift.Int` is *NOT* `int` or `long`, it is `uintptr_t`. Unfortunately, we cannot include `stdint.h` here and thus cannot use `uintptr_t`. Perhaps we should consider using `__swift_uintptr_t`. For now, assume that we are always 64-bit and use `long long` and `unsigned long long` which repairs the builds with non-LP64 targets. --- include/swift/Basic/CBasicBridging.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 1a99b83c703c2..d2568c6132923 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -40,7 +40,7 @@ SWIFT_BEGIN_ASSUME_NONNULL typedef struct BridgedData { const char *_Nullable baseAddress; - unsigned long size; + size_t size; } BridgedData; void BridgedData_free(BridgedData data); From 223ebe059436bc4a1f540c611074dce4033011c8 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 31 Aug 2023 17:24:31 -0700 Subject: [PATCH 2/2] AST: avoid truncation `Swift.Int` is *NOT* `int` or `long`, it is `uintptr_t`. Replace many of the size parameters with `size_t`. --- include/swift/AST/CASTBridging.h | 14 +++++++------- lib/AST/CASTBridging.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/swift/AST/CASTBridging.h b/include/swift/AST/CASTBridging.h index 2d6a5a7409113..5f7d0a48c49da 100644 --- a/include/swift/AST/CASTBridging.h +++ b/include/swift/AST/CASTBridging.h @@ -25,17 +25,17 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS SWIFT_BEGIN_ASSUME_NONNULL -typedef long SwiftInt; -typedef unsigned long SwiftUInt; +typedef long long SwiftInt; +typedef unsigned long long SwiftUInt; typedef struct { const unsigned char *_Nullable data; - long length; + size_t length; } BridgedString; typedef struct { const void *_Nullable data; - long numElements; + size_t numElements; } BridgedArrayRef; typedef struct BridgedASTContext { @@ -113,7 +113,7 @@ typedef struct BridgedDiagnosticEngine { void *raw; } BridgedDiagnosticEngine; -typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long { +typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : size_t { /// An expanded macro. BridgedExpandedMacro = 0, /// An external macro, spelled with either the old spelling (Module.Type) @@ -307,7 +307,7 @@ void *IfStmt_create(BridgedASTContext cContext, BridgedSourceLoc cIfLoc, void *BraceStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLBLoc, BridgedArrayRef elements, BridgedSourceLoc cRBLoc); -BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len); +BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, size_t len); void *ParamDecl_create(BridgedASTContext cContext, BridgedSourceLoc cLoc, BridgedSourceLoc cArgLoc, BridgedIdentifier argName, @@ -357,7 +357,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext, BridgedIdentifier name, BridgedSourceLoc cNameLoc, - BridgedSourceLoc cEachLoc, long index, + BridgedSourceLoc cEachLoc, size_t index, _Bool isParameterPack); void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext, void *Param, void *ty); diff --git a/lib/AST/CASTBridging.cpp b/lib/AST/CASTBridging.cpp index e629b4057b0e9..e2078c570d825 100644 --- a/lib/AST/CASTBridging.cpp +++ b/lib/AST/CASTBridging.cpp @@ -204,7 +204,7 @@ void *ImportDecl_create(BridgedASTContext cContext, std::move(importPath).get()); } -BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len) { +BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, size_t len) { SourceLoc loc = convertSourceLoc(cLoc).getAdvancedLoc(len); return {loc.getOpaquePointerValue()}; } @@ -764,7 +764,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext, BridgedIdentifier name, BridgedSourceLoc cNameLoc, - BridgedSourceLoc cEachLoc, long index, + BridgedSourceLoc cEachLoc, size_t index, bool isParameterPack) { return GenericTypeParamDecl::createParsed( convertDeclContext(cDeclContext), convertIdentifier(name),