1 /*
2 * e_std.c
3 *
4 * 1998/08 made public kmatsui
5 * 2002/08 revised not to conflict with C99 Standard kmatsui
6 * 2003/11 added a few samples kmatsui
7 *
8 * Samples to test Standard C preprocessing.
9 * Preprocessor must diagnose all of these samples appropriately.
10 */
11
12
13 void e_19_3( void);
14 void e_25_6( void);
15 void e_27_6( void);
16 void e_31( void);
17
main(void)18 main( void)
19 {
20 e_19_3();
21 e_25_6();
22 e_27_6();
23 e_31();
24
25 return 0;
26 }
27
28
29 /* Illegal pp-token. */
30
31 /* 4.3: Empty character constant is an error. */
32 #if '' == 0 /* This line is invalid, maybe skipped. */
33 #endif /* This line maybe the second error. */
34
35
36 /* #line error. */
37
38 /* 7.4: string literal in #line directive shall be a character string
39 literal. */
40
41 #line 123 L"wide"
42
43
44 /* Out of range of integer pp-token in #if expression. */
45 /* Note: Tests of character constant overflow are in 32.5, 33.2, 35.2. */
46
47 /* 12.8: Preprocessing number perhaps out of range of unsigned long. */
48 #if 123456789012345678901
49 #endif
50
51
52 /* Illegal #if expressions. */
53
54 #define A 1
55 #define B 1
56
57 /* 14.1: String literal is not allowed in #if expression. */
58 #if "string"
59 #endif /* The second error ? */
60
61 /* 14.2: Operators =, +=, ++, etc. are not allowed in #if expression. */
62 #if A = B
63 #endif
64 #if A++ B
65 #endif
66 #if A --B
67 #endif
68 #if A.B
69 #endif
70
71 /* 14.3: Unterminated #if expression. */
72 #if 0 <
73 #endif
74 #if ( (A == B)
75 #endif
76
77 /* 14.4: Unbalanced parenthesis in #if defined operator. */
78 #if defined ( MACRO
79 #endif
80
81 /* 14.5: No argument. */
82 #if
83 #endif
84
85 /* 14.6: Macro expanding to 0 token in #if expression. */
86 #define ZERO_TOKEN
87 #if ZERO_TOKEN
88 #endif
89
90
91 /* There is no keyword in #if expression. */
92
93 /* 14.7: sizeof operator is disallowed. */
94 /* Evaluated as: 0 (0)
95 Constant expression syntax error. */
96 #if sizeof (int)
97 #endif
98
99 /* 14.8: type cast is disallowed. */
100 /* Evaluated as: (0)0x8000
101 Also a constant expression error. */
102 #if (int)0x8000 < 0
103 #endif
104
105
106 /* Out of range in #if expression (division by 0). */
107
108 /* 14.9: Divided by 0. */
109 #if 1 / 0
110 #endif
111
112
113 /* Overflow of constant expression in #if directive. */
114
115 /* 14.10: */
116 #include <limits.h>
117
118 #if LONG_MAX - LONG_MIN
119 #endif
120 #if LONG_MAX + 1
121 #endif
122 #if LONG_MIN - 1
123 #endif
124 #if LONG_MAX * 2
125 #endif
126
127
128 /* #ifdef, #ifndef syntax errors. */
129
130 /* 15.3: Not an identifier. */
131 #ifdef "string"
132 #endif
133 #ifdef 123
134 #endif
135
136 /* 15.4: Excessive token sequence. */
137 #ifdef A Junk
138 #endif
139
140 /* 15.5: No argument. */
141 #ifndef
142 #endif
143
144
145 /* Trailing junk of #else, #endif. */
146
147 /* 16.1: Trailing junk of #else. */
148 #define MACRO_0 0
149 #if MACRO_0
150 #else MACRO_0
151
152 /* 16.2: Trailing junk of #endif. */
153 #endif MACRO_0
154
155
156 /* Ill-formed group in a source file. */
157
158 #define MACRO_1 1
159
160 /* 17.1: Error of #endif without #if. */
161 #endif
162
163 /* 17.2: Error of #else without #if. */
164 #else
165
166 /* 17.3: Error of #else after #else. */
167 #if MACRO_1
168 #else /* line 168 */
169 #if 1
170 #else
171 #endif
172 #else
173 #endif
174
175 /* 17.4: Error of #elif after #else. */
176 #if MACRO_1 == 1
177 #else /* line 177 */
178 #elif MACRO_1 == 0
179 #endif
180
181 /* 17.5: Error of #endif without #if in an included file. */
182 #if 1
183 #include "unbal1.h"
184
185 /* 17.6: Error of unterminated #if section in an included file. */
186 #include "unbal2.h"
187 #endif
188
189 /* 17.7: Error of unterminated #if section. */
190 /* An error would be reported at end of file. */
191 #if MACRO_1 == 0 /* line 191 */
192 #else
193
194
195 /* #define syntax errors. */
196
197 /* 18.4: Not an identifier. */
198 #define "string"
199 #define 123
200
201 /* 18.5: No argument. */
202 #define
203
204 /* 18.6: Empty parameter list. */
205 #define math( op, a, ) op( (a), (b))
206
207 /* 18.7: Duplicate parameter names. */
208 #define math( op, a, a) op( (a), (b))
209
210 /* 18.8: Parameter is not an identifier. */
211 #define NUMARGS( 1, +, 2) (1 + 2)
212
213 /* 18.9: No space between macro name and replacement text. */
214 /*
215 C90 (Corrigendum 1) forbids this if and only the replacement text begins
216 with a non-basic-character.
217 C99 forbids this even when the replacement text begins with basic-
218 character.
219 */
220 /* From ISO 9899:1990 / Corrigendum 1. */
221 #define THIS$AND$THAT(a, b) ((a) + (b))
222 /* Note: the following definition is legal (object-like macro).
223 #define THIS $AND$THAT(a, b) ((a) + (b))
224 */
225
226
227 /* Redefinitions of macros. */
228
229 #define str( s) # s
230 #define xstr( s) str( s)
231
232 /* Excerpts from ISO C 6.8.3 "Examples". */
233
234 #define OBJ_LIKE (1-1)
235 #define FTN_LIKE(a) ( a )
236
e_19_3(void)237 void e_19_3( void)
238 {
239 /* The following redefinitions should be diagnosed. */
240
241 /* 19.3: */
242 #define OBJ_LIKE (0) /* different token sequence */
243
244 /* 19.4: */
245 #undef OBJ_LIKE
246 #define OBJ_LIKE (1-1)
247 #define OBJ_LIKE (1 - 1) /* different white space */
248
249 /* 19.5: */
250 #define FTN_LIKE(b) ( a ) /* different parameter usage */
251
252 /* 19.6: */
253 #undef FTN_LIKE
254 #define FTN_LIKE(a) ( a )
255 #define FTN_LIKE(b) ( b ) /* different parameter spelling */
256
257 /* 19.7: Not in ISO C "Examples" */
258 #define FTN_LIKE OBJ_LIKE
259 }
260
261
262 /* ## operator shall not occur at the beginning or at the end of
263 replacement list for either form of macro definition. */
264
265 /* 23.3: In object-like macro. */
266 #define con ## name
267 #define cat 12 ##
268
269 /* 23.4: In function-like macro. */
270 #define CON( a, b) ## a ## b
271 #define CAT( b, c) b ## c ##
272
273
274 /* Operand of # operator in function-like macro definition shall
275 be a parameter name. */
276
277 /* 24.6: */
278 #define FUNC( a) # b
279
280
281 /* Macro arguments are pre-expanded separately. */
282
283 /* 25.6: */
284 #define sub( x, y) (x - y)
285 #define head sub(
286 #define body(x,y) x,y
287 #define tail )
288 #define head_body_tail( a, b, c) a b c
289
e_25_6(void)290 void e_25_6( void)
291 {
292 /* "head" is once replaced to "sub(", then rescanning of "sub(" causes an
293 uncompleted macro call. Expansion of an argument should complete
294 within the argument. */
295 head_body_tail( head, body(a,b), tail);
296 }
297
298
299 /* Error of rescanning. */
300
301 /* 27.7: */
302 #define TWO_ARGS a,b
303 #define SUB( x, y) sub( x, y)
304
e_27_7(void)305 void e_27_7( void)
306 {
307 /* Too many arguments error while rescanning after once replaced to:
308 sub( a,b, 1); */
309 SUB( TWO_ARGS, 1);
310 }
311
312
313 /* #undef errors. */
314
315 /* 29.3: Not an identifier. */
316 #undef "string"
317 #undef 123
318
319 /* 29.4: Excessive token sequence. */
320 #undef MACRO_0 Junk
321
322 /* 29.5: No argument. */
323 #undef
324
325
326 /* Illegal macro calls. */
327
e_31(void)328 void e_31( void)
329 {
330 int x = 1, y = 2;
331
332 /* 31.1: Too many arguments error. */
333 sub( x, y, z);
334
335 /* 31.2: Too few arguments error. */
336 sub( x);
337 }
338
339
340 /* Macro call in control line should complete in the line. */
341
342 #define glue( a, b) a ## b
343
344 /* 31.3: Unterminated macro call. */
345 #include xstr( glue( header,
346 .h))
347
348
349 /* Range error of character constant. */
350
351 /* 32.5: Value of a numerical escape sequence in character constant should
352 be in the range of char. */
353 #if '\x123' == 0x123 /* Out of range. */
354 #endif
355
356
357 /* Out of range of numerical escape sequence in wide-char. */
358
359 /* 33.2: Value of a numerical escape sequence in wide-character constant
360 should be in the range of wchar_t. */
361 #if L'\xabcdef012' == 0xbcde012 /* Perhaps out of range. */
362 #endif
363
364
365 /* Out of range of character constant. */
366
367 /* In ASCII character set. */
368 /* 35.2: */
369 #if 'abcdefghi' == '\x61\x62\x63\x64\x65\x66\x67\x68\x69'
370 /* Perhaps out of range. */
371 #endif
372
373
374 /* Error of "unterminated #if section started at line 191" will be reported
375 at end of file. */
376
377