Skip to content

Commit 8db1ad6

Browse files
authored
Cherry-pick [Swiftify] Don't import counted_by with suffixed integer literals (#82469) (#82594)
Integer literal expressions with types that are not of type `int` are printed with a suffix to indicate the type (e.g. `123U` or `456L` for `unsigned` and `long`). This is not valid syntax for integer literals in Swift, so until we fully translate the count expr syntax to Swift we need to avoid importing these count expressions. Also fixes some -Werror related stuff in test cases. rdar://154141719 (cherry picked from commit 374658a)
1 parent 9b66d7c commit 8db1ad6

File tree

9 files changed

+86
-17
lines changed

9 files changed

+86
-17
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9083,9 +9083,34 @@ namespace {
90839083
/// Look for any side effects within a Stmt.
90849084
struct CATExprValidator : clang::ConstStmtVisitor<CATExprValidator, bool> {
90859085
bool VisitDeclRefExpr(const clang::DeclRefExpr *e) { return true; }
9086-
bool VisitIntegerLiteral(const clang::IntegerLiteral *) { return true; }
9087-
bool VisitImplicitCastExpr(const clang::ImplicitCastExpr *c) { return this->Visit(c->getSubExpr()); }
9088-
bool VisitParenExpr(const clang::ParenExpr *p) { return this->Visit(p->getSubExpr()); }
9086+
9087+
bool VisitIntegerLiteral(const clang::IntegerLiteral *IL) {
9088+
switch (IL->getType()->castAs<clang::BuiltinType>()->getKind()) {
9089+
case clang::BuiltinType::Char_S:
9090+
case clang::BuiltinType::Char_U:
9091+
case clang::BuiltinType::UChar:
9092+
case clang::BuiltinType::SChar:
9093+
case clang::BuiltinType::Short:
9094+
case clang::BuiltinType::UShort:
9095+
case clang::BuiltinType::UInt:
9096+
case clang::BuiltinType::Long:
9097+
case clang::BuiltinType::ULong:
9098+
case clang::BuiltinType::LongLong:
9099+
case clang::BuiltinType::ULongLong:
9100+
// These integer literals are printed with a suffix that isn't valid Swift
9101+
// syntax
9102+
return false;
9103+
default:
9104+
return true;
9105+
}
9106+
}
9107+
9108+
bool VisitImplicitCastExpr(const clang::ImplicitCastExpr *c) {
9109+
return this->Visit(c->getSubExpr());
9110+
}
9111+
bool VisitParenExpr(const clang::ParenExpr *p) {
9112+
return this->Visit(p->getSubExpr());
9113+
}
90899114

90909115
#define SUPPORTED_UNOP(UNOP) \
90919116
bool VisitUnary ## UNOP(const clang::UnaryOperator *unop) { \

test/Interop/C/swiftify-import/Inputs/counted-by.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ void floatCastToInt(float meters, int * __counted_by((int) meters) p);
5252
void pointerCastToInt(int *square, int * __counted_by((int) square) p);
5353

5454
void nanAsInt(int * __counted_by((int) (0 / 0)) p);
55+
56+
void unsignedLiteral(int * __counted_by(2u) p);
57+
58+
void longLiteral(int * __counted_by(2l) p);
59+
60+
void hexLiteral(int * __counted_by(0xfa) p);
61+
62+
void binaryLiteral(int * __counted_by(0b10) p);
63+
64+
void octalLiteral(int * __counted_by(0777) p);

test/Interop/C/swiftify-import/Inputs/sized-by-noescape.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include <stdint.h>
44

5+
#ifndef __sized_by
56
#define __sized_by(x) __attribute__((__sized_by__(x)))
7+
#endif
68
#define __noescape __attribute__((noescape))
79

810
void simple(int len, const void * __sized_by(len) __noescape p);

test/Interop/C/swiftify-import/counted-by-no-swiftify.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast | %FileCheck %s
22

33
// REQUIRES: swift_feature_SafeInteropWrappers
44

@@ -12,3 +12,7 @@
1212
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} floatCastToInt
1313
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} pointerCastToInt
1414
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} nanAsInt
15+
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} unsignedLiteral
16+
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} longLiteral
17+
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} sizeofType
18+
// CHECK-NOT: @_alwaysEmitIntoClient {{.*}} sizeofParam

test/Interop/C/swiftify-import/counted-by-noescape.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// REQUIRES: swift_feature_SafeInteropWrappers
22
// REQUIRES: swift_feature_Lifetimes
33

4-
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByNoEscapeClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature Lifetimes -Xcc -Wno-nullability-completeness | %FileCheck %s
4+
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByNoEscapeClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature Lifetimes -Xcc -Werror -Xcc -Wno-ignored-attributes -Xcc -Wno-nullability-completeness | %FileCheck %s
55

66
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
77
// RUN: %empty-directory(%t)
8-
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedByNoEscape.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature Lifetimes -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s
8+
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedByNoEscape.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -enable-experimental-feature Lifetimes -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-ignored-attributes -Xcc -Wno-nullability-completeness %s
99

1010
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __counted_by __noescape parameters.
1111

test/Interop/C/swiftify-import/counted-by.swift

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
// REQUIRES: swift_feature_SafeInteropWrappers
22

3-
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s
3+
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast | %FileCheck %s
44

55
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
66
// RUN: %empty-directory(%t)
7-
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedBy.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s
7+
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedBy.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast %s
88

99
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __counted_by parameters.
1010

1111
import CountedByClang
1212

1313

1414
// CHECK: /// This is an auto-generated wrapper for safer interop
15+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func binaryLiteral(_ p: UnsafeMutableBufferPointer<Int32>)
16+
17+
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
1518
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func bitshift(_ m: Int32, _ n: Int32, _ o: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
1619

1720
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
@@ -26,6 +29,9 @@ import CountedByClang
2629
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
2730
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func constInt(_ p: UnsafeMutableBufferPointer<Int32>)
2831

32+
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
33+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func hexLiteral(_ p: UnsafeMutableBufferPointer<Int32>)
34+
2935
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
3036
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func nonnull(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
3137

@@ -35,6 +41,9 @@ import CountedByClang
3541
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
3642
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func nullable(_ p: UnsafeMutableBufferPointer<Int{{.*}}>?)
3743

44+
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
45+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func octalLiteral(_ p: UnsafeMutableBufferPointer<Int32>)
46+
3847
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
3948
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func offByOne(_ len: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
4049

@@ -57,19 +66,28 @@ import CountedByClang
5766
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func simpleFlipped(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
5867

5968
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
60-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func sizeofParam(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
69+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func swiftAttr(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
6170

62-
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
63-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func sizeofType(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
71+
@inlinable
72+
public func callBitshift(_ m: CInt, n: CInt, o: CInt, _ p: UnsafeMutableBufferPointer<CInt>) {
73+
unsafe bitshift(m, n, o, p)
74+
}
6475

65-
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
66-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func swiftAttr(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
76+
@inlinable
77+
public func callBitwise(_ m: CInt, n: CInt, o: CInt, _ p: UnsafeMutableBufferPointer<CInt>) {
78+
unsafe bitwise(m, n, o, p)
79+
}
6780

6881
@inlinable
6982
public func callComplexExpr(_ p: UnsafeMutableBufferPointer<CInt>) {
7083
unsafe complexExpr(CInt(p.count), 1, p)
7184
}
7285

86+
@inlinable
87+
public func callConstFloatCastedToInt(_ p: UnsafeMutableBufferPointer<CInt>) {
88+
unsafe constFloatCastedToInt(p)
89+
}
90+
7391
@inlinable
7492
public func callConstInt(_ p: UnsafeMutableBufferPointer<CInt>) {
7593
unsafe constInt(p)
@@ -90,11 +108,21 @@ public func callNullable(_ p: UnsafeMutableBufferPointer<CInt>?) {
90108
unsafe nullable(p)
91109
}
92110

111+
@inlinable
112+
public func callOctalLiteral(_ p: UnsafeMutableBufferPointer<CInt>) {
113+
unsafe octalLiteral(p)
114+
}
115+
93116
@inlinable
94117
public func callOffByOne(_ p: UnsafeMutableBufferPointer<CInt>) {
95118
unsafe offByOne(0, p)
96119
}
97120

121+
@inlinable
122+
public func callOffBySome(_ p: UnsafeMutableBufferPointer<CInt>) {
123+
unsafe offBySome(0, 1, p)
124+
}
125+
98126
@inlinable
99127
public func callReturnPointer() {
100128
let _: UnsafeMutableBufferPointer<CInt>? = returnPointer(4) // call wrapper

test/Interop/C/swiftify-import/sized-by-lifetimebound.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// REQUIRES: swift_feature_SafeInteropWrappers
22
// REQUIRES: swift_feature_Lifetimes
33

4-
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByLifetimeboundClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s
4+
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByLifetimeboundClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Werror -Xcc -Wno-nullability-completeness | %FileCheck %s
55

66
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
77
// RUN: %empty-directory(%t)

test/Interop/C/swiftify-import/sized-by-noescape.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// REQUIRES: swift_feature_SafeInteropWrappers
22
// REQUIRES: swift_feature_Lifetimes
33

4-
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByNoEscapeClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature Lifetimes -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s
4+
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByNoEscapeClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature Lifetimes -enable-experimental-feature SafeInteropWrappers -Xcc -Werror -Xcc -Wno-ignored-attributes -Xcc -Wno-nullability-completeness | %FileCheck %s
55

66
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
77
// RUN: %empty-directory(%t)
8-
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/SizedByNoEscape.swiftmodule -I %S/Inputs -enable-experimental-feature Lifetimes -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s
8+
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/SizedByNoEscape.swiftmodule -I %S/Inputs -enable-experimental-feature Lifetimes -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-ignored-attributes -Xcc -Wno-nullability-completeness %s
99

1010
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __sized_by __noescape parameters.
1111
import SizedByNoEscapeClang

test/Interop/C/swiftify-import/sized-by.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: swift_feature_SafeInteropWrappers
22

3-
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s
3+
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Werror -Xcc -Wno-nullability-completeness | %FileCheck %s
44

55
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
66
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s

0 commit comments

Comments
 (0)