Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 2 | |
| 3 | struct { |
| 4 | int x; |
| 5 | int y; |
| 6 | } point; |
| 7 | |
| 8 | void fn1() { |
| 9 | point.x = 42; |
| 10 | } |
| 11 | |
| 12 | /* Nested member */ |
| 13 | struct { |
| 14 | struct { |
| 15 | int a; |
| 16 | int b; |
| 17 | } p1; |
| 18 | } point2; |
| 19 | |
| 20 | void fn2() { |
| 21 | point2.p1.a = 42; |
| 22 | } |
| 23 | |
| 24 | /* Indirect reference */ |
| 25 | typedef struct __sf { |
| 26 | unsigned char *c; |
| 27 | short flags; |
| 28 | } F; |
| 29 | |
| 30 | typedef struct __sf2 { |
| 31 | F *ff; |
| 32 | } F2; |
| 33 | |
| 34 | int fn3(F2 *c) { |
| 35 | if (c->ff->c >= 0) |
| 36 | return 1; |
| 37 | else |
| 38 | return 0; |
| 39 | } |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 40 | |
| 41 | /* Nested structs */ |
| 42 | typedef struct NA { |
| 43 | int data; |
| 44 | struct NA *next; |
| 45 | } NA; |
Devang Patel | dbb46b1 | 2007-10-23 23:29:51 +0000 | [diff] [blame] | 46 | void f1() { NA a; } |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 47 | |
| 48 | typedef struct NB { |
| 49 | int d1; |
| 50 | struct _B2 { |
| 51 | int d2; |
| 52 | struct NB *n2; |
| 53 | } B2; |
| 54 | } NB; |
| 55 | |
| 56 | void f2() { NB b; } |
| 57 | |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 58 | extern NB *f3(); |
| 59 | void f4() { |
| 60 | f3()->d1 = 42; |
| 61 | } |
Devang Patel | 0a96118 | 2007-10-26 18:15:21 +0000 | [diff] [blame] | 62 | |
| 63 | void f5() { |
| 64 | (f3())->d1 = 42; |
| 65 | } |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 66 | |
| 67 | /* Function calls */ |
| 68 | typedef struct { |
| 69 | int location; |
| 70 | int length; |
| 71 | } range; |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 72 | extern range f6(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | void f7() { |
Anders Carlsson | a1ff3e9 | 2007-11-02 16:59:10 +0000 | [diff] [blame] | 74 | range r = f6(); |
| 75 | } |
| 76 | |
| 77 | /* Member expressions */ |
| 78 | typedef struct { |
| 79 | range range1; |
| 80 | range range2; |
| 81 | } rangepair; |
| 82 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | void f8() { |
Anders Carlsson | a1ff3e9 | 2007-11-02 16:59:10 +0000 | [diff] [blame] | 84 | rangepair p; |
| 85 | |
| 86 | range r = p.range1; |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | void f9(range *p) { |
Seo Sanghyeon | 7777bb2 | 2007-12-14 01:09:11 +0000 | [diff] [blame] | 90 | range r = *p; |
| 91 | } |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 92 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | void f10(range *p) { |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 94 | range r = p[0]; |
| 95 | } |
| 96 | |
Devang Patel | 14c7500 | 2007-12-10 18:25:34 +0000 | [diff] [blame] | 97 | /* _Bool types */ |
| 98 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | struct _w { |
Devang Patel | 14c7500 | 2007-12-10 18:25:34 +0000 | [diff] [blame] | 100 | short a,b; |
| 101 | short c,d; |
| 102 | short e,f; |
| 103 | short g; |
| 104 | |
| 105 | unsigned int h,i; |
| 106 | |
| 107 | _Bool j,k; |
| 108 | } ws; |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 109 | |
| 110 | /* Implicit casts (due to typedefs) */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | typedef struct _a { |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 112 | int a; |
| 113 | } a; |
| 114 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | void f11() { |
| 116 | struct _a a1; |
| 117 | a a2; |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 118 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | a1 = a2; |
| 120 | a2 = a1; |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 121 | } |
Anders Carlsson | a46b759 | 2008-01-18 02:25:57 +0000 | [diff] [blame] | 122 | |
| 123 | /* Implicit casts (due to const) */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | void f12() { |
| 125 | struct _a a1; |
| 126 | const struct _a a2; |
| 127 | |
| 128 | a1 = a2; |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* struct initialization */ |
Eli Friedman | 75afb58 | 2008-02-06 05:33:51 +0000 | [diff] [blame] | 132 | struct a13 {int b; int c;}; |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 133 | struct a13 c13 = {5}; |
Devang Patel | bf20b68 | 2008-05-27 22:44:22 +0000 | [diff] [blame] | 134 | typedef struct a13 a13; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 135 | struct a14 { short a; int b; } x = {1, 1}; |
Eli Friedman | 75afb58 | 2008-02-06 05:33:51 +0000 | [diff] [blame] | 136 | |
| 137 | /* flexible array members */ |
| 138 | struct a15 {char a; int b[];} c15; |
| 139 | int a16(void) {c15.a = 1;} |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 140 | |
| 141 | /* compound literals */ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | void f13() { |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 143 | a13 x; x = (a13){1,2}; |
| 144 | } |
| 145 | |
| 146 | /* va_arg */ |
| 147 | int f14(int i, ...) { |
| 148 | __builtin_va_list l; |
| 149 | __builtin_va_start(l,i); |
| 150 | a13 b = __builtin_va_arg(l, a13); |
Daniel Dunbar | 4dc346e | 2009-02-12 08:41:10 +0000 | [diff] [blame] | 151 | int c = __builtin_va_arg(l, a13).c; |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 152 | return b.b; |
| 153 | } |
Devang Patel | 2fb86e6 | 2008-05-27 22:45:40 +0000 | [diff] [blame] | 154 | |
| 155 | /* Attribute packed */ |
| 156 | struct __attribute__((packed)) S2839 { double a[19]; signed char b; } s2839[5]; |
Eli Friedman | 0408f68 | 2008-05-29 10:58:49 +0000 | [diff] [blame] | 157 | |
| 158 | struct __attribute__((packed)) SS { long double a; char b; } SS; |
| 159 | |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 160 | |
| 161 | /* As lvalue */ |
| 162 | |
| 163 | int f15() { |
| 164 | extern range f15_ext(); |
| 165 | return f15_ext().location; |
| 166 | } |
| 167 | |
| 168 | range f16() { |
| 169 | extern rangepair f16_ext(); |
| 170 | return f16_ext().range1; |
| 171 | } |
| 172 | |
| 173 | int f17() { |
| 174 | extern range f17_ext(); |
| 175 | range r; |
| 176 | return (r = f17_ext()).location; |
| 177 | } |
| 178 | |
| 179 | range f18() { |
| 180 | extern rangepair f18_ext(); |
| 181 | rangepair rp; |
| 182 | return (rp = f18_ext()).range1; |
| 183 | } |
Chris Lattner | f0a8679 | 2011-07-10 03:47:27 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | |
| 187 | // Complex forward reference of struct. |
| 188 | struct f19S; |
| 189 | extern struct f19T { |
| 190 | struct f19S (*p)(void); |
| 191 | } t; |
| 192 | struct f19S { int i; }; |
| 193 | void f19(void) { |
| 194 | t.p(); |
| 195 | } |
| 196 | |