Skip to content

Commit ec24b74

Browse files
committed
[ClangImporter] Add more test coverage in ClangImporter/const_values.swift + ClangImporter/const_values_cxx.swift
1 parent a39f60b commit ec24b74

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

test/ClangImporter/const_values.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ static const long static_const_long = 42;
2020
static const float static_const_float = 42.0;
2121
static const double static_const_double = 42.0;
2222

23+
static const char static_const_char_array[4] = {1, 2, 3, 4};
24+
static const char *static_const_pointer = 0;
25+
26+
typedef enum MyEnum: char {
27+
MyEnumCase0 = 0, MyEnumCase1, MyEnumCase2
28+
} MyEnum;
29+
static const MyEnum static_const_enum = MyEnumCase1;
30+
31+
2332
//--- main.swift
2433
func foo() {
2534
print(MACRO_INT)
@@ -33,10 +42,17 @@ func foo() {
3342
print(static_const_long)
3443
print(static_const_float)
3544
print(static_const_double)
45+
46+
print(static_const_char_array)
47+
print(static_const_pointer)
48+
49+
print(static_const_enum)
3650
}
3751

38-
// Only 'static' cannot get the getter imported, because it's not guaranteed to be constant.
52+
// Globals that don't get their value imported stay as public_external:
3953
// CHECK: sil_global public_external @static_int : $Int32
54+
// CHECK: sil_global public_external [let] @static_const_char_array : $(Int8, Int8, Int8, Int8)
55+
// CHECK: sil_global public_external @static_const_pointer : $Optional<UnsafePointer<Int8>>
4056

4157
// CHECK: sil shared [transparent] @$sSC9MACRO_INTs5Int32Vvg : $@convention(thin) () -> Int32 {
4258
// CHECK-NEXT: bb0:
@@ -93,3 +109,5 @@ func foo() {
93109
// CHECK-NEXT: %1 = struct $Double (%0)
94110
// CHECK-NEXT: return %1
95111
// CHECK-NEXT: }
112+
113+
// TODO: Add static_const_enum
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// RUN: %empty-directory(%t/src)
2+
// RUN: split-file %s %t/src
3+
4+
// RUN: %target-swiftxx-frontend %t/src/main.swift \
5+
// RUN: -import-bridging-header %t/src/test.h \
6+
// RUN: -module-name main -I %t -emit-sil | %FileCheck %s
7+
8+
//--- test.h
9+
10+
const int const_int = 42;
11+
namespace Namespace {
12+
const int namespaced_const_int = 42;
13+
}
14+
15+
static inline int foo() { return 42; }
16+
const int not_really_constant = foo();
17+
18+
constexpr int constexpr_func() { return 42; }
19+
constexpr int constexpr_int = constexpr_func();
20+
static constexpr int static_constexpr_int = constexpr_func();
21+
22+
class MyClass {
23+
public:
24+
const int class_const_int = 42; // member field, don't expect to import as a global, no CHECKs for this
25+
static const int class_static_const_int = 42;
26+
};
27+
28+
namespace OtherNamespace {
29+
constexpr int namespaced_constexpr_int = constexpr_func();
30+
static constexpr int namespaced_static_constexpr_int = constexpr_func();
31+
}
32+
class OtherClass {
33+
static constexpr int class_static_constexpr_int = 42;
34+
};
35+
36+
37+
//--- main.swift
38+
func foo() {
39+
print(const_int)
40+
print(Namespace.namespaced_const_int)
41+
print(not_really_constant)
42+
print(constexpr_int)
43+
print(static_constexpr_int)
44+
45+
// TODO: Non-top-level constexpr variables currently are not imported at all (would be imported as static members)
46+
//print(OtherNamespace.namespaced_constexpr_int)
47+
//print(OtherNamespace.namespaced_static_constexpr_int)
48+
//print(MyClass.class_static_constexpr_int)
49+
50+
print(MyClass().class_const_int)
51+
print(MyClass.class_static_const_int)
52+
}
53+
54+
// Only imported as external declarations:
55+
// CHECK: sil_global public_external [let] @not_really_constant : $Int32
56+
57+
// CHECK: sil shared [transparent] @$sSo9const_ints5Int32Vvg : $@convention(thin) () -> Int32 {
58+
// CHECK-NEXT: bb0:
59+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42
60+
// CHECK-NEXT: %1 = struct $Int32 (%0)
61+
// CHECK-NEXT: return %1
62+
// CHECK-NEXT: }
63+
64+
// CHECK: sil shared [transparent] @$sSo9NamespaceO20namespaced_const_ints5Int32VvgZ : $@convention(method) (@thin Namespace.Type) -> Int32 {
65+
// CHECK-NEXT: // %0 "self"
66+
// CHECK-NEXT: bb0(%0 : $@thin Namespace.Type):
67+
// CHECK-NEXT: debug_value %0, let, name "self", argno 1
68+
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 42
69+
// CHECK-NEXT: %3 = struct $Int32 (%2)
70+
// CHECK-NEXT: return %3
71+
// CHECK-NEXT: }
72+
73+
// CHECK: sil shared [transparent] @$sSo13constexpr_ints5Int32Vvg : $@convention(thin) () -> Int32 {
74+
// CHECK-NEXT: bb0:
75+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42
76+
// CHECK-NEXT: %1 = struct $Int32 (%0)
77+
// CHECK-NEXT: return %1
78+
// CHECK-NEXT: }
79+
80+
// CHECK: sil shared [transparent] @$sSo20static_constexpr_ints5Int32Vvg : $@convention(thin) () -> Int32 {
81+
// CHECK-NEXT: bb0:
82+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int32, 42
83+
// CHECK-NEXT: %1 = struct $Int32 (%0)
84+
// CHECK-NEXT: return %1
85+
// CHECK-NEXT: }
86+
87+
// CHECK: sil shared [transparent] @$sSo7MyClassV22class_static_const_ints5Int32VvgZ : $@convention(method) (@thin MyClass.Type) -> Int32 {
88+
// CHECK-NEXT: // %0 "self"
89+
// CHECK-NEXT: bb0(%0 : $@thin MyClass.Type):
90+
// CHECK-NEXT: debug_value %0, let, name "self", argno 1
91+
// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 42
92+
// CHECK-NEXT: %3 = struct $Int32 (%2)
93+
// CHECK-NEXT: return %3
94+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)