Skip to content

Commit 942893d

Browse files
authored
ClangImporter: Preserve CChar type (swiftlang#32692)
Addresses SR-466.
1 parent eb84f5e commit 942893d

File tree

12 files changed

+758
-735
lines changed

12 files changed

+758
-735
lines changed

include/swift/ClangImporter/BuiltinMappedTypes.def

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,32 @@
2121
// - CLANG_BUILTIN_KIND is the kind of builtin type, clang::BuiltinType
2222
// - SWIFT_TYPE_NAME is the name of the corresponding stdlib type.
2323
//
24+
// MAP_BUILTIN_CCHAR_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME)
25+
// - CLANG_BUILTIN_KIND is the kind of builtin type, clang::BuiltinType
26+
// - SWIFT_TYPE_NAME is the name of the corresponding stdlib type.
27+
//
2428
//===----------------------------------------------------------------------===//
2529

2630
#ifndef MAP_BUILTIN_INTEGER_TYPE
2731
#define MAP_BUILTIN_INTEGER_TYPE(CLANG, SWIFT) MAP_BUILTIN_TYPE(CLANG, SWIFT)
2832
#endif
2933

34+
#ifndef MAP_BUILTIN_CCHAR_TYPE
35+
#define MAP_BUILTIN_CCHAR_TYPE(CLANG, SWIFT) MAP_BUILTIN_TYPE(CLANG, SWIFT)
36+
#endif
37+
38+
MAP_BUILTIN_CCHAR_TYPE(Char_U, CChar)
39+
MAP_BUILTIN_CCHAR_TYPE(Char_S, CChar)
40+
MAP_BUILTIN_CCHAR_TYPE(WChar_S, CWideChar)
41+
MAP_BUILTIN_CCHAR_TYPE(WChar_U, CWideChar)
42+
3043
MAP_BUILTIN_TYPE(Bool, CBool)
31-
MAP_BUILTIN_INTEGER_TYPE(Char_U, CChar)
32-
MAP_BUILTIN_INTEGER_TYPE(Char_S, CChar)
3344
MAP_BUILTIN_INTEGER_TYPE(UChar, CUnsignedChar)
3445
MAP_BUILTIN_INTEGER_TYPE(UShort, CUnsignedShort)
3546
MAP_BUILTIN_INTEGER_TYPE(UInt, CUnsignedInt)
3647
MAP_BUILTIN_INTEGER_TYPE(ULong, CUnsignedLong)
3748
MAP_BUILTIN_INTEGER_TYPE(ULongLong, CUnsignedLongLong)
3849
MAP_BUILTIN_INTEGER_TYPE(UInt128, CUnsignedInt128)
39-
MAP_BUILTIN_INTEGER_TYPE(WChar_S, CWideChar)
40-
MAP_BUILTIN_INTEGER_TYPE(WChar_U, CWideChar)
4150
MAP_BUILTIN_INTEGER_TYPE(Char16, CChar16)
4251
MAP_BUILTIN_INTEGER_TYPE(Char32, CChar32)
4352
MAP_BUILTIN_INTEGER_TYPE(SChar, CSignedChar)
@@ -54,4 +63,4 @@ MAP_BUILTIN_TYPE(LongDouble, CLongDouble)
5463

5564
#undef MAP_BUILTIN_TYPE
5665
#undef MAP_BUILTIN_INTEGER_TYPE
57-
66+
#undef MAP_BUILTIN_CCHAR_TYPE

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ namespace {
236236
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
237237
return unwrapCType(Impl.getNamedSwiftType(Impl.getStdlibModule(), \
238238
#SWIFT_TYPE_NAME));
239-
239+
#define MAP_BUILTIN_CCHAR_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
240+
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
241+
return Impl.getNamedSwiftType(Impl.getStdlibModule(), #SWIFT_TYPE_NAME);
240242
#include "swift/ClangImporter/BuiltinMappedTypes.def"
241243

242244
// Types that cannot be mapped into Swift, and probably won't ever be.

test/ClangImporter/ctypes_parse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func testStructDefaultInit() {
223223

224224
func testArrays() {
225225
nonnullArrayParameters([], [], [])
226-
nonnullArrayParameters(nil, [], []) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<Int8>'}}
226+
nonnullArrayParameters(nil, [], []) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<CChar>'}}
227227
nonnullArrayParameters([], nil, []) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<UnsafeMutableRawPointer?>'}}
228228
nonnullArrayParameters([], [], nil) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<Int32>'}}
229229

@@ -233,7 +233,7 @@ func testArrays() {
233233
// It would also be nice to warn here about the arrays being too short, but
234234
// that's probably beyond us for a while.
235235
staticBoundsArray([])
236-
staticBoundsArray(nil) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<Int8>'}}
236+
staticBoundsArray(nil) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<CChar>'}}
237237
}
238238

239239
func testVaList() {

test/ClangImporter/ctypes_parse_swift4.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ func testArrays() {
66
// It would also be nice to warn here about the arrays being too short, but
77
// that's probably beyond us for a while.
88
staticBoundsArray([])
9-
staticBoundsArray(nil) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<Int8>'}}
9+
staticBoundsArray(nil) // expected-error {{'nil' is not compatible with expected argument type 'UnsafePointer<CChar>'}}
1010
}

test/IDE/infer_import_as_member.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ let _ = mine.getCollisionNonProperty(1)
8989
// PRINT-NEXT: }
9090
// PRINT-NEXT: extension IAMMutableStruct1 {
9191
// PRINT-NEXT: init(with withIAMStruct1: IAMStruct1)
92-
// PRINT-NEXT: init(url url: UnsafePointer<Int8>!)
92+
// PRINT-NEXT: init(url url: UnsafePointer<CChar>!)
9393
// PRINT-NEXT: func doSomething()
9494
// PRINT-NEXT: }
9595
//

test/IDE/print_omit_needless_words.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
// CHECK-FOUNDATION: var setShouldBeInfinite: Bool { get }
173173

174174
// "UTF8" initialisms.
175-
// CHECK-FOUNDATION: init?(utf8String: UnsafePointer<Int8>!)
175+
// CHECK-FOUNDATION: init?(utf8String: UnsafePointer<CChar>!)
176176

177177
// Don't strip prefixes from globals.
178178
// CHECK-FOUNDATION: let NSGlobalConstant: String

test/IDE/print_omit_needless_words_appkit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// CHECK-APPKIT: func same() -> Self
1919

2020
// Note: Unsafe(Mutable)Pointers don't get defaulted to 'nil'
21-
// CHECK-APPKIT: func getRGBAComponents(_: UnsafeMutablePointer<Int8>?)
21+
// CHECK-APPKIT: func getRGBAComponents(_: UnsafeMutablePointer<CChar>?)
2222

2323
// Note: Skipping over "3D"
2424
// CHECK-APPKIT: func drawInAir(at: Point3D)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <stddef.h>
2+
3+
extern char a_char;
4+
extern wchar_t a_wchar;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ImportCCharTypes {
2+
header "import-cchar-types.h"
3+
export *
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=ImportCCharTypes -I %S/Inputs -source-filename=x | %FileCheck %s
2+
3+
// CHECK: var a_char: CChar
4+
// CHECK: var a_wchar: wchar_t

0 commit comments

Comments
 (0)