const-init.c revision baf524d2137f719f0465cf5e8dc8d5f5d8089fbf
1// RUN: clang-cc -triple i386-pc-linux-gnu -ffreestanding -verify -emit-llvm -o - %s | FileCheck %s
2
3#include <stdint.h>
4
5// Brace-enclosed string array initializers
6char a[] = { "asdf" };
7
8// Double-implicit-conversions of array/functions (not legal C, but
9// clang accepts it for gcc compat).
10intptr_t b = a; // expected-warning {{incompatible pointer to integer conversion}}
11int c();
12void *d = c;
13intptr_t e = c; // expected-warning {{incompatible pointer to integer conversion}}
14
15int f, *g = __extension__ &f, *h = (1 != 1) ? &f : &f;
16
17union s2 {
18  struct {
19    struct { } *f0;
20  } f0;
21};
22
23int g0 = (int)(&(((union s2 *) 0)->f0.f0) - 0);
24
25// CHECK: @g1x = global {{%.}} { double 1.000000e+00{{[0]*}}, double 0.000000e+00{{[0]*}} }
26_Complex double g1x = 1.0f;
27// CHECK: @g1y = global {{%.}} { double 0.000000e+00{{[0]*}}, double 1.000000e+00{{[0]*}} }
28_Complex double g1y = 1.0fi;
29// CHECK: @g1 = global {{%.}} { i8 1, i8 10 }
30_Complex char g1 = (char) 1 + (char) 10 * 1i;
31// CHECK: @g2 = global %2 { i32 1, i32 10 }
32_Complex int g2 = 1 + 10i;
33// CHECK: @g3 = global {{%.}} { float 1.000000e+00{{[0]*}}, float 1.000000e+0{{[0]*}}1 }
34_Complex float g3 = 1.0 + 10.0i;
35// CHECK: @g4 = global {{%.}} { double 1.000000e+00{{[0]*}}, double 1.000000e+0{{[0]*}}1 }
36_Complex double g4 = 1.0 + 10.0i;
37// CHECK: @g5 = global %2 zeroinitializer
38_Complex int g5 = (2 + 3i) == (5 + 7i);
39// CHECK: @g6 = global {{%.}} { double -1.100000e+0{{[0]*}}1, double 2.900000e+0{{[0]*}}1 }
40_Complex double g6 = (2.0 + 3.0i) * (5.0 + 7.0i);
41// CHECK: @g7 = global i32 1
42int g7 = (2 + 3i) * (5 + 7i) == (-11 + 29i);
43// CHECK: @g8 = global i32 1
44int g8 = (2.0 + 3.0i) * (5.0 + 7.0i) == (-11.0 + 29.0i);
45// CHECK: @g9 = global i32 0
46int g9 = (2 + 3i) * (5 + 7i) != (-11 + 29i);
47// CHECK: @g10 = global i32 0
48int g10 = (2.0 + 3.0i) * (5.0 + 7.0i) != (-11.0 + 29.0i);
49
50// PR5108
51// CHECK: @gv1 = global %4 <{ i32 0, i8 7 }>, align 1
52struct {
53  unsigned long a;
54  unsigned long b:3;
55} __attribute__((__packed__)) gv1  = { .a = 0x0, .b = 7,  };
56
57// PR5118
58// CHECK: @gv2 = global %5 <{ i8 1, i8* null }>, align 1
59struct {
60  unsigned char a;
61  char *b;
62} __attribute__((__packed__)) gv2 = { 1, (void*)0 };
63
64// Global references
65// CHECK: @g11.l0 = internal global i32 ptrtoint (i32 ()* @g11 to i32)
66long g11() {
67  static long l0 = (long) g11;
68  return l0;
69}
70
71// CHECK: @g12 = global i32 ptrtoint (i8* @g12_tmp to i32)
72static char g12_tmp;
73long g12 = (long) &g12_tmp;
74
75// CHECK: @g13 = global [1 x %struct.g13_s0] [%struct.g13_s0 { i32 ptrtoint (i8* @g12_tmp to i32) }]
76struct g13_s0 {
77   long a;
78};
79struct g13_s0 g13[] = {
80   { (long) &g12_tmp }
81};
82
83// CHECK: @g14 = global i8* inttoptr (i64 100 to i8*)
84void *g14 = (void*) 100;
85
86// CHECK: @g15 = global i32 -1
87int g15 = (int) (char) ((void*) 0 + 255);
88
89// CHECK: @g16 = global i64 4294967295
90long long g16 = (long long) ((void*) 0xFFFFFFFF);
91
92// CHECK: @g17 = global i32* @g15
93int *g17 = (int *) ((long) &g15);
94
95// CHECK: @g18.p = internal global [1 x i32*] [i32* @g19]
96void g18(void) {
97  extern int g19;
98  static int *p[] = { &g19 };
99}
100
101// CHECK: @g20.l0 = internal global %struct.g20_s1 { %struct.g20_s0* null, %struct.g20_s0** getelementptr inbounds (%struct.g20_s1* @g20.l0, i32 0, i32 0) }
102struct g20_s0;
103struct g20_s1 {
104  struct g20_s0 *f0, **f1;
105};
106void *g20(void) {
107  static struct g20_s1 l0 = { ((void*) 0), &l0.f0 };
108  return l0.f1;
109}
110
111// PR4108
112struct g21 {int g21;};
113const struct g21 g21 = (struct g21){1};
114
115// PR5474
116struct g22 {int x;} __attribute((packed));
117struct g23 {char a; short b; char c; struct g22 d;};
118struct g23 g24 = {1,2,3,4};
119
120// CHECK: @__func__.g25 = private constant [4 x i8] c"g25\00"
121// CHECK: @g25.g26 = internal global i8* getelementptr inbounds ([4 x i8]* @__func__.g25, i32 0, i32 0)
122int g25() {
123  static const char *g26 = __func__;
124  return *g26;
125}
126