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