aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/cpp/embed-1.c
blob: aaf6670dc615ca0297cc1fba254d3a199f86ad49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* { dg-do run } */
/* { dg-options "--embed-dir=${srcdir}/c-c++-common/cpp/embed-dir" } */
/* { dg-additional-options "-std=gnu99" { target c } } */

#if !defined(__STDC_EMBED_NOT_FOUND__) || __STDC_EMBED_NOT_FOUND__ != 0
#error "__STDC_EMBED_NOT_FOUND__ not defined or has invalid value"
#endif

#if __STDC_EMBED_FOUND__ != 1
#error "__STDC_EMBED_FOUND__ not defined or has invalid value"
#endif

#if __STDC_EMBED_EMPTY__ != 2
#error "__STDC_EMBED_EMPTY__ not defined or has invalid value"
#endif

#ifndef __has_embed
#error "__has_embed not defined"
#endif

#if __has_embed (__FILE__) != __STDC_EMBED_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (__FILE__ limit (NONEXISTENT_MACRO) prefix (1 2 3) __suffix__ () if_empty ({})) != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed (__FILE__ __limit__ (1) __prefix__ () suffix (1 / 0) __if_empty__ ((({{[0[0{0{0(0(0)1)1}1}]]}})))) != __STDC_EMBED_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (__FILE__ limit (0) gnu::nonexistent ({})) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (__FILE__ limit (0) __gnu__::__non_existent__ ({})) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (__FILE__ limit (0) gnu::__non_existent__ ({})) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (__FILE__ limit (0) non_existent_standard_arg (42)) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed ("non-existent-file" limit (NONEXISTENT_MACRO + 1)) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed ("embed-1.c" limit (0)) != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#define E1 "embed-1.c"
#define E2 limit (
#define E3 1)
#if __has_embed (E1 E2 E3) != __STDC_EMBED_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (<embed-1.c>) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#define E4 <embed-1.c>
#define E5 limit
#define E6 1)
#if __has_embed ( \
E4 \
E5 ( \
E6 \
) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed ("embed-1.inc") != __STDC_EMBED_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed ( "embed-1.inc" __limit__ ( 7 - 7 ) ) != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed (<embed-1.inc>) != __STDC_EMBED_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed (<embed-1.inc> limit(0)) != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed ("../empty.h") != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed (<../empty.h>) != __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed ("../../empty.h") != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed (<../../empty.h>) != __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed ("../empty.h" __limit__ (42)) !=  __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed (<../empty.h> __limit__ (42)) !=  __STDC_EMBED_NOT_FOUND__
#error "__has_embed fail"
#endif

#if __has_embed ("../../empty.h" limit (42)) !=  __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if __has_embed (<../../empty.h> __limit__ (42)) !=  __STDC_EMBED_EMPTY__
#error "__has_embed fail"
#endif

#if 1 + (2 + (3 + (4 + (5 + (6 + (7 + (8 + (9 + (10 + (11 + (12 + (13 + (14 + \
    (15 + (16 + (17 + (18 + (19 + (20 + (21 + (22 + (23 + (24 + (25 + (26 + \
    __has_embed (<magna-carta.txt> limit (17 + (16 + (15 + (14 + (13 \
    + 12))))))))))))))))))))))))))))))) != 27 * 26 / 2 + __STDC_EMBED_FOUND__
#error "__has_embed fail"
#endif

#if 0
#if __has_embed (<non-existent-file.bin> limit (1 + 2 + 3 + 4)) != 42
#error "__has_embed fail"
#endif
#endif

#if (0 && __has_embed (<non-existent-file.bin> limit (3))) != 0
#error "fail"
#endif

#if __STDC_VERSION__ >= 202311L || __cplusplus >= 201103L
static_assert (
#embed <embed-1.inc>
== 'a', "");
static_assert (
#embed "embed-1.inc" \
__limit__ (\
0) __if_empty__ (42) __prefix__ ( \
256 + ) __suffix__ (+ 17)
== 42, "");
static_assert (
#embed "embed-1.inc" limit (42) if_empty (42) prefix (256 + ) suffix (+ 17)
== 256 + 'a' + 17, "");
#endif

int a =
#embed __FILE__ limit (1)
;
int b =
#embed __FILE__ if_empty (42; int c = 0) limit (0)
;
const unsigned char magna_carta[] = {
#embed <magna-carta.txt> prefix (/* This is a prefix */) suffix (, 0) if_empty (0)
};
const unsigned char d[] = {
#define MAGNA_CARTA "magna-carta.txt"
#define MAGNA_CARTA_PARAMS prefix (239, 187, 191,) \
suffix (, 0) limit (15)
#embed MAGNA_CARTA MAGNA_CARTA_PARAMS \
\
if_empty (0{1[2[3{4(5(6)7)8}9]0]1}2)
};
#ifndef __cplusplus
const unsigned char e[] = {
#embed MAGNA_CARTA __limit__ (42) __suffix__ (, [21] = 'X', [22] = 'X', [23] = 'X')
};
#endif
int f;
struct S { int a; long b; unsigned short c; long long d; } g = {
#embed "magna-carta.txt" limit (4)
};
const unsigned char h[] = {
#embed __FILE__ suffix (, 0)
};
struct T { int a; };
struct U { unsigned short a; struct T b; long long c; };
struct V { unsigned long long a; struct U b; short int c; int d; };
struct V v = {
#embed "magna-carta.txt" __limit__(4)__prefix__(42,)__suffix__(,-42)
};
#ifndef __cplusplus
const unsigned char w[] = {
#embed <magna-carta.txt> prefix([0] = 42, [72] =) limit(1500)
};
#endif
const unsigned char x[] = {
#define XIFEMPTY1 0, 1
#define XIFEMPTY2 ,3,
#define XIFEMPTY3 4
#embed "../empty.h" if_empty (XIFEMPTY1,2 XIFEMPTY2 \
XIFEMPTY3) prefix (5, 6, 7, ) suffix (, 8, 9)
};
const unsigned char y[] = {
#embed <embed-3.c> suffix(,0)
};
const unsigned char z[] = {
#embed "embed-3.c" suffix(,0)
};

#if 0
#embed <non-existent-file.bin> limit (3)
#endif

#ifdef __cplusplus
#define C "C"
#else
#define C
#endif
extern C void abort (void);
#ifdef __cplusplus
extern C const char *strstr (const char *, const char *);
#else
extern C char *strstr (const char *, const char *);
#endif

void
foo (unsigned int a, unsigned long long b, unsigned char c, int d, int e)
{
  if (a != 'H' || b != 'e' || c != 'n' || d != 'r' || !e)
    abort ();
}

int
main ()
{
  if (a != '/' || b != 42 || c != 0)
    abort ();
  if (sizeof (magna_carta) != 17893 + 1
      /* Allow for DOS line endings instead of Unix just in case.  */
      && sizeof (magna_carta) != 17939 + 1)
    abort ();
  if (!strstr ((const char *) magna_carta, "imprisonétur"))
    abort ();
  if (sizeof (d) != 3 + 15 + 1
      || d[0] != 239 || d[1] != 187 || d[2] != 191 || d[3 + 15] != 0)
    abort ();
#ifndef __cplusplus
  if (sizeof (e) != 42 || e[21] != 'X' || e[22] != 'X' || e[23] != 'X')
    abort ();
#endif
  f = (
#embed "magna-carta.txt" limit (4) prefix (172 + ) suffix (+ 2)
  );
  if (f != 'r' + 2)
    abort ();
  if (g.a != 'H' || g.b != 'e' || g.c != 'n' || g.d != 'r')
    abort ();
  if (!strstr ((const char *) h, "imprisonétur")
      || !strstr ((const char *) h, "blah blah"))
    abort ();
  if (v.a != 42 || v.b.a != 'H' || v.b.b.a != 'e'
      || v.b.c != 'n' || v.c != 'r' || v.d != -42)
    abort ();
#ifndef __cplusplus
  if (sizeof (w) != 1572 || w[0] != 42 || w[1] != 0 || w[71] != 0
      || w[72] != 'H' || w[73] != 'e' || w[74] != 'n' || w[75] != 'r')
    abort ();
#endif
  if (sizeof (x) != 5 || x[0] != 0 || x[1] != 1 || x[2] != 2
      || x[3] != 3 || x[4] != 4)
    abort ();
  foo (
#embed <magna-carta.txt> limit(5)
  );
  if (sizeof (y) > 100
      || !strstr ((const char *) y, "Dummy file.")
      || strstr ((const char *) y, "__gnu__::__non_existent_parameter"))
    abort ();
  if (sizeof (z) < 100
      || strstr ((const char *) z, "Dummy file.")
      || !strstr ((const char *) z, "__gnu__::__non_existent_parameter"))
    abort ();
  if (sizeof (
#embed "embed-1.inc"
      ) != sizeof (int))
    abort ();
#ifndef __cplusplus
  if (_Generic (
#embed "embed-1.inc" __limit__ (1) __suffix__ (, int: 42, default: 0)
      ) != 42)
    abort ();
#endif
#if __STDC_VERSION__ >= 202311L
  typeof (
#embed "embed-1.inc" limit (1)
  ) j;
  if (sizeof (j) != sizeof (int))
    abort ();
#elif __cplusplus >= 201103L
  decltype (
#embed "embed-1.inc" limit (1)
  ) j;
  if (sizeof (j) != sizeof (int))
    abort ();
#endif
#if __has_embed ("embed-5.c" ds9000::element_type(short))
  short meow[] = {
    #embed "embed-5.c" ds9000::element_type(short)
  };
#elif __has_embed ("embed-5.c")
  const unsigned char meow_bytes[] = {
    #embed "embed-5.c"
  };
  short meow[sizeof (meow_bytes) / sizeof (short)] = {};
  for (int i = 0; i < (int) (sizeof (meow) / sizeof (short)); i++)
    meow[i] = (meow_bytes[i * 2] << 8) | meow_bytes[i * 2 + 1];
#else
  #error "cannot find embed-5.c resource"
#endif
  if (meow[0] != ('/' << 8) + '*')
    abort ();
#if __has_embed("././././embed-1.c" __limit__(1))
  if (
		#embed		"././././embed-1.c" /* This */ limit /* is */ ( /* a */ 1 /*comment*/) // .
      != '/')
    abort ();
#else
#error "__has_embed fail"
#endif
}