| 1 |
// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - -O3 | FileCheck -check-prefix=CHECK -check-prefix=CHECK-LP64 %s |
| 2 |
// RUN: %clang_cc1 %s -triple=i386-apple-darwin10 -emit-llvm -o - -O3 | FileCheck %s |
| 3 |
// RUN: %clang_cc1 %s -triple=aarch64_be -emit-llvm -o - -O3 | FileCheck %s |
| 4 |
// RUN: %clang_cc1 %s -triple=thumbv7_be-none-eabi -emit-llvm -o - -O3 | FileCheck %s |
| 5 |
// RUN: %clang_cc1 %s -triple=x86_64-unknown-unknown -emit-llvm -o - -O3 -std=c++11 | FileCheck -check-prefix=CHECK -check-prefix=CHECK-LP64 %s |
| 6 |
|
| 7 |
// CHECK-LP64: %union.Test1 = type { i32, [4 x i8] } |
| 8 |
union Test1 { |
| 9 |
int a; |
| 10 |
int b: 39; |
| 11 |
}; |
| 12 |
Test1 t1; |
| 13 |
|
| 14 |
// CHECK-LP64: %union.Test2 = type { i8 } |
| 15 |
union Test2 { |
| 16 |
int : 6; |
| 17 |
} t2; |
| 18 |
|
| 19 |
// CHECK-LP64: %union.Test3 = type { i16 } |
| 20 |
union Test3 { |
| 21 |
int : 9; |
| 22 |
} t3; |
| 23 |
|
| 24 |
// CHECK: %union.Test4 = type { i8, i8 } |
| 25 |
union Test4 { |
| 26 |
char val : 16; |
| 27 |
}; |
| 28 |
Test4 t4; |
| 29 |
|
| 30 |
#define CHECK(x) if (!(x)) return __LINE__ |
| 31 |
|
| 32 |
// CHECK: define{{.*}} i32 @_Z11test_assignv() |
| 33 |
int test_assign() { |
| 34 |
struct { |
| 35 |
int a; |
| 36 |
|
| 37 |
unsigned long long b : 65; |
| 38 |
|
| 39 |
int c; |
| 40 |
} c; |
| 41 |
|
| 42 |
c.a = 0; |
| 43 |
c.b = (unsigned long long)-1; |
| 44 |
c.c = 0; |
| 45 |
|
| 46 |
CHECK(c.a == 0); |
| 47 |
CHECK(c.b == (unsigned long long)-1); |
| 48 |
CHECK(c.c == 0); |
| 49 |
|
| 50 |
Test1 u1; |
| 51 |
Test4 u2; |
| 52 |
|
| 53 |
u1.b = 1; |
| 54 |
u2.val = 42; |
| 55 |
|
| 56 |
CHECK(u1.b == 1); |
| 57 |
CHECK(u2.val == 42); |
| 58 |
|
| 59 |
// CHECK: ret i32 0 |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
// CHECK: define{{.*}} i32 @_Z9test_initv() |
| 64 |
int test_init() { |
| 65 |
struct S { |
| 66 |
int a; |
| 67 |
|
| 68 |
unsigned long long b : 65; |
| 69 |
|
| 70 |
int c; |
| 71 |
}; |
| 72 |
S s1 = {1, 42, 0}; |
| 73 |
|
| 74 |
CHECK(s1.a == 1); |
| 75 |
CHECK(s1.b == (unsigned long long)42); |
| 76 |
CHECK(s1.c == 0); |
| 77 |
|
| 78 |
Test1 u1 = {1}; |
| 79 |
Test4 u2 = {42}; |
| 80 |
|
| 81 |
CHECK(u1.a == 1); |
| 82 |
CHECK(u1.b == 1); |
| 83 |
CHECK(u2.val == 42); |
| 84 |
|
| 85 |
// CHECK: ret i32 0 |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
extern "C" { |
| 90 |
int test_trunc_int() { |
| 91 |
union { |
| 92 |
int i : 4; // truncated to 0b1111 == -1 |
| 93 |
} const U = {15}; // 0b00001111 |
| 94 |
return U.i; |
| 95 |
} |
| 96 |
// CHECK: define{{.*}} i32 @test_trunc_int() |
| 97 |
// CHECK: ret i32 -1 |
| 98 |
|
| 99 |
int test_trunc_three_bits() { |
| 100 |
union { |
| 101 |
int i : 3; // truncated to 0b111 == -1 |
| 102 |
} const U = {15}; // 0b00001111 |
| 103 |
return U.i; |
| 104 |
} |
| 105 |
// CHECK: define{{.*}} i32 @test_trunc_three_bits() |
| 106 |
// CHECK: ret i32 -1 |
| 107 |
|
| 108 |
int test_trunc_one_bit() { |
| 109 |
union { |
| 110 |
int i : 1; // truncated to 0b1 == -1 |
| 111 |
} const U = {1}; // 0b00000001 |
| 112 |
return U.i; |
| 113 |
} |
| 114 |
// CHECK: define{{.*}} i32 @test_trunc_one_bit() |
| 115 |
// CHECK: ret i32 -1 |
| 116 |
|
| 117 |
int test_trunc_1() { |
| 118 |
union { |
| 119 |
int i : 1; // truncated to 0b1 == -1 |
| 120 |
} const U = {15}; // 0b00001111 |
| 121 |
return U.i; |
| 122 |
} |
| 123 |
// CHECK: define{{.*}} i32 @test_trunc_1() |
| 124 |
// CHECK: ret i32 -1 |
| 125 |
|
| 126 |
int test_trunc_zero() { |
| 127 |
union { |
| 128 |
int i : 4; // truncated to 0b0000 == 0 |
| 129 |
} const U = {80}; // 0b01010000 |
| 130 |
return U.i; |
| 131 |
} |
| 132 |
// CHECK: define{{.*}} i32 @test_trunc_zero() |
| 133 |
// CHECK: ret i32 0 |
| 134 |
|
| 135 |
int test_constexpr() { |
| 136 |
union { |
| 137 |
int i : 3; // truncated to 0b111 == -1 |
| 138 |
} const U = {1 + 2 + 4 + 8}; // 0b00001111 |
| 139 |
return U.i; |
| 140 |
} |
| 141 |
// CHECK: define{{.*}} i32 @test_constexpr() |
| 142 |
// CHECK: ret i32 -1 |
| 143 |
|
| 144 |
int test_notrunc() { |
| 145 |
union { |
| 146 |
int i : 12; // not truncated |
| 147 |
} const U = {1 + 2 + 4 + 8}; // 0b00001111 |
| 148 |
return U.i; |
| 149 |
} |
| 150 |
// CHECK: define{{.*}} i32 @test_notrunc() |
| 151 |
// CHECK: ret i32 15 |
| 152 |
|
| 153 |
long long test_trunc_long_long() { |
| 154 |
union { |
| 155 |
long long i : 14; // truncated to 0b00111101001101 == |
| 156 |
} const U = {0b0100111101001101}; |
| 157 |
return U.i; |
| 158 |
} |
| 159 |
// CHECK: define{{.*}} i64 @test_trunc_long_long() |
| 160 |
// CHECK: ret i64 3917 |
| 161 |
} |