Skip to content

Commit dc65773

Browse files
committed
[ClangImporter] Add more const values importing tests cases
1 parent 936e911 commit dc65773

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

test/ClangImporter/const_values.swift

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// RUN: %empty-directory(%t/src)
22
// RUN: split-file %s %t/src
33

4-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %t/src/main.swift \
4+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \
55
// RUN: -import-bridging-header %t/src/test.h \
6-
// RUN: -module-name main -I %t -emit-sil | %FileCheck %s
6+
// RUN: -module-name main -I %t -emit-sil -serialize-diagnostics -serialize-diagnostics-path %t/test.diag | %FileCheck %s
7+
8+
// RUN: c-index-test -read-diagnostics %t/test.diag 2>&1 | %FileCheck --check-prefix CHECK-DIAG %t/src/test.h
79

810
//--- test.h
911
#include <stdbool.h>
@@ -16,18 +18,28 @@ static const int static_const_int = 42;
1618

1719
static const bool static_const_bool = true;
1820
static const char static_const_char = 42;
21+
static const char static_const_char_with_long_literal = 42ull;
22+
static const char static_const_char_with_overflow_literal = 777ull; // CHECK-DIAG: [[@LINE]]:{{.*}}: warning: implicit conversion from 'unsigned long long' to 'char' changes value from 777 to 9
1923
static const long static_const_long = 42;
2024
static const float static_const_float = 42.0;
2125
static const double static_const_double = 42.0;
2226

2327
static const char static_const_char_array[4] = {1, 2, 3, 4};
2428
static const char *static_const_pointer = 0;
2529

30+
static const char static_const_char_referencing_other_const = 1 + static_const_char;
31+
2632
typedef enum MyEnum: char {
2733
MyEnumCase0 = 0, MyEnumCase1, MyEnumCase2
2834
} MyEnum;
2935
static const MyEnum static_const_enum = MyEnumCase1;
3036

37+
struct MyStruct {
38+
int field;
39+
};
40+
__attribute__((swift_name("MyStruct.static_const_int_as_a_member")))
41+
static const int static_const_int_as_a_member = 42;
42+
3143
//--- main.swift
3244
func foo() {
3345
print(MACRO_INT)
@@ -38,14 +50,20 @@ func foo() {
3850

3951
print(static_const_bool)
4052
print(static_const_char)
53+
print(static_const_char_with_long_literal)
54+
print(static_const_char_with_overflow_literal)
4155
print(static_const_long)
4256
print(static_const_float)
4357
print(static_const_double)
4458

4559
print(static_const_char_array)
4660
print(static_const_pointer)
4761

62+
print(static_const_char_referencing_other_const)
63+
4864
print(static_const_enum)
65+
66+
print(MyStruct.static_const_int_as_a_member)
4967
}
5068

5169
// Globals that don't get their value imported stay as public_external:
@@ -88,6 +106,20 @@ func foo() {
88106
// CHECK-NEXT: return %1
89107
// CHECK-NEXT: }
90108

109+
// CHECK: sil shared [transparent] @$sSo35static_const_char_with_long_literals4Int8Vvg : $@convention(thin) () -> Int8 {
110+
// CHECK-NEXT: bb0:
111+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 42
112+
// CHECK-NEXT: %1 = struct $Int8 (%0)
113+
// CHECK-NEXT: return %1
114+
// CHECK-NEXT: }
115+
116+
// CHECK: sil shared [transparent] @$sSo39static_const_char_with_overflow_literals4Int8Vvg : $@convention(thin) () -> Int8 {
117+
// CHECK-NEXT: bb0:
118+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 9
119+
// CHECK-NEXT: %1 = struct $Int8 (%0)
120+
// CHECK-NEXT: return %1
121+
// CHECK-NEXT: }
122+
91123
// CHECK: sil shared [transparent] @$sSo17static_const_longSivg : $@convention(thin) () -> Int {
92124
// CHECK-NEXT: bb0:
93125
// CHECK-NEXT: %0 = integer_literal $Builtin.Int64, 42
@@ -109,10 +141,26 @@ func foo() {
109141
// CHECK-NEXT: return %1
110142
// CHECK-NEXT: }
111143

144+
// CHECK: sil shared [transparent] @$sSo036static_const_char_referencing_other_B0s4Int8Vvg : $@convention(thin) () -> Int8 {
145+
// CHECK-NEXT: bb0:
146+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 43
147+
// CHECK-NEXT: %1 = struct $Int8 (%0)
148+
// CHECK-NEXT: return %1
149+
// CHECK-NEXT: }
150+
112151
// CHECK: sil shared [transparent] @$sSo17static_const_enumSo6MyEnumVvg : $@convention(thin) () -> MyEnum {
113152
// CHECK-NEXT: bb0:
114153
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 1
115154
// CHECK-NEXT: %1 = struct $Int8 (%0)
116155
// CHECK-NEXT: %2 = struct $MyEnum (%1)
117156
// CHECK-NEXT: return %2
118157
// CHECK-NEXT: }
158+
159+
// CHECK: sil shared [transparent] @$sSo8MyStructV28static_const_int_as_a_members5Int32VvgZ : $@convention(method) (@thin MyStruct.Type) -> Int32 {
160+
// CHECK-NEXT: // %0 "self"
161+
// CHECK-NEXT: bb0(%0 : $@thin MyStruct.Type):
162+
// CHECK-NEXT: debug_value %0, let, name "self", argno 1
163+
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 42
164+
// CHECK-NEXT: %3 = struct $Int32 (%2)
165+
// CHECK-NEXT: return %3
166+
// CHECK-NEXT: }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t/src)
2+
// RUN: split-file %s %t/src
3+
4+
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \
5+
// RUN: -import-bridging-header %t/src/test.h \
6+
// RUN: -module-name main -I %t -emit-sil -serialize-diagnostics -serialize-diagnostics-path %t/test.diag
7+
8+
// RUN: c-index-test -read-diagnostics %t/test.diag 2>&1 | %FileCheck --check-prefix CHECK-DIAG %t/src/test.h
9+
10+
//--- test.h
11+
static char other = 42;
12+
static const char static_const_char_that_is_not_const = 1 + other;
13+
// CHECK-DIAG: [[@LINE-1]]:{{.*}}: error: initializer element is not a compile-time constant
14+
// CHECK-DIAG: error: failed to import bridging header
15+
16+
//--- main.swift
17+
func foo() {
18+
print(static_const_char_that_is_not_const)
19+
}

0 commit comments

Comments
 (0)