4
* 1998/08 made public kmatsui
5
* 2002/08 revised not to conflict with C99 Standard kmatsui
7
* Samples to test C++ Standard preprocessing.
8
* Preprocessor must process these samples as shown in each comments.
9
* Preprocessor must process also #error directive properly, which is not
10
* included here because the directive might cause translator to terminate.
18
#define sub( x, y) (x - y)
20
#define glue( a, b) a ## b
21
#define xglue( a, b) glue( a, b)
24
/* n_1.t: Conversion of trigraph sequences. */
26
/* 1.1: The following 9 sequences are valid trigraph sequences. */
27
/* [ ] \ ^ { } | ~ #; */
28
??( ??) ??/ ??' ??< ??> ??! ??- ??=;
30
/* 1.2: In directive line. */
32
??= define OR( a, b) a ??! b
35
/* 1.3: Any sequence other than above 9 is not a trigraph sequence. */
36
/* ?? ??? ??% ??^ ?#; */
40
/* n_2.t: Line splicing by <backslash><newline> sequence. */
42
/* 2.1: In a #define directive line, between the parameter list and the
45
#define FUNC( a, b, c) \
49
/* 2.2: In a #define directive line, among the parameter list and among the
59
/* 2.3: In a string literal. */
64
/* 2.4: <backslash><newline> in midst of an identifier. */
69
/* 2.5: <backslash><newline> by trigraph. */
75
/* n_3.t: Handling of comment. */
77
/* 3.1: A comment is converted to one space. */
81
/* 3.2: // is not a comment of C. */
82
#if 0 /* This feature is obsolete now. */
83
/* / / is not a comment of C */
84
// is not a comment of C
87
/* 3.3: Comment is parsed prior to the parsing of preprocessing directive. */
102
/* n_4.t: Special tokens. */
104
/* 4.1: Digraph spellings in directive line. */
106
%: define stringize( a) %: a
110
/* 4.2: Digraph spellings are retained in stringization. */
115
/* n_5.t: Spaces or tabs are allowed at any place in pp-directive line,
116
including between the top of a pp-directive line and '#', and between
117
the '#' and the directive. */
120
/* |**|[TAB]# |**|[TAB]define |**| MACRO_abcde[TAB]|**| abcde |**| */
121
/**/ # /**/ define /**/ MACRO_abcde /**/ abcde /**/
126
/* n_6.t: #include directive. */
128
/* 6.1: Header-name quoted by " and " as well as by < and > can include
130
/* Note: Standard headers can be included any times. */
134
/* 6.2: Macro is allowed in #include line. */
135
#define HEADER "header.h"
136
/* Before file inclusion: #include "header.h" */
141
/* 6.3: With macro nonsence but legal. */
143
#include ZERO_TOKEN HEADER ZERO_TOKEN
148
/* n_7.t: #line directive. */
150
/* 7.1: Line number and filename. */
155
/* 7.2: Filename argument is optional. */
160
/* 7.3: Argument with macro. */
162
#define LINE_AND_FILENAME 3456 "n_7.t"
163
#line LINE_AND_FILENAME
166
/* Restore to correct line number and filename. */
170
/* n_9.t: #pragma directive. */
172
/* 9.1: Any #pragma directive should be processed or ignored, should not
173
be diagnosed as an error. */
178
/* n_10.t: #if, #elif, #else and #endif pp-directive. */
181
/* Note: an undefined identifier in #if expression is replaced to 0. */
187
#elif MACRO_1 /* Valid block */
193
/* 10.2: Comments must be processed even if in skipped #if block. */
194
/* At least tokenization of string literal and character constant is necessary
195
to process comments, e.g. /* is not a comment mark in string literal.
199
"in literal /* is not a comment"
203
/* n_11.t: Operator "defined" in #if or #elif directive. */
207
#define MACRO_abc abc
215
#if defined (MACRO_abc)
221
/* 11.2: "defined" is an unary operator whose result is 1 or 0. */
222
#if defined MACRO_0 * 3 != 3
223
Bad handling of "defined" operator.
225
#if (!defined ZERO_TOKEN != 0) || (-defined ZERO_TOKEN != -1)
226
Bad grouping of "defined", !, - operator.
230
/* n_12.t: Integer preprocessing number token and type of #if expression. */
234
/* 12.1: Type long. */
235
#if LONG_MAX <= LONG_MIN
236
Bad evaluation of long.
238
#if LONG_MAX <= 1073741823 /* 0x3FFFFFFF */
239
Bad evaluation of long.
242
/* 12.2: Type unsigned long. */
243
#if ULONG_MAX / 2 < LONG_MAX
244
Bad evaluation of unsigned long.
247
/* 12.3: Octal number. */
249
Bad evaluation of octal number.
252
/* 12.4: Hexadecimal number. */
253
#if 0Xffff != 65535 || 0xFfFf != 65535
254
Bad evaluation of hexadecimal number.
257
/* 12.5: Suffix 'L' or 'l'. */
258
#if 0L != 0 || 0l != 0
259
Bad evaluation of 'L' suffix.
262
/* 12.6: Suffix 'U' or 'u'. */
263
#if 1U != 1 || 1u != 1
264
Bad evaluation of 'U' suffix.
267
/* 12.7: Negative integer. */
269
Bad evaluation of negative number.
273
/* n_13.t: Valid operators in #if expression. */
275
/* Valid operators are (precedence in this order) :
276
defined, (unary)+, (unary)-, ~, !,
290
/* 13.1: Bit shift. */
291
#if 1 << 2 != 4 || 8 >> 1 != 4
292
Bad arithmetic of <<, >> operators.
295
/* 13.2: Bitwise operators. */
296
#if (3 ^ 5) != 6 || (3 | 5) != 7 || (3 & 5) != 1
297
Bad arithmetic of ^, |, & operators.
300
/* 13.3: Result of ||, && operators is either of 1 or 0. */
301
#if (2 || 3) != 1 || (2 && 3) != 1 || (0 || 4) != 1 || (0 && 5) != 0
302
Bad arithmetic of ||, && operators.
305
/* 13.4: ?, : operator. */
307
Bad arithmetic of ?: operator.
311
/* n_13_5.t: Arithmetic conversion in #if expressions. */
313
/* 13.5: The usual arithmetic conversion is not performed on bit shift. */
315
Bad conversion of bit shift operands.
318
/* 13.6: Usual arithmetic conversions. */
319
#if -1 <= 0U /* -1 is converted to unsigned long. */
320
Bad arithmetic conversion.
324
Bad arithmetic conversion.
327
/* Second and third operands of conditional operator are converted to the
328
same type, thus -1 is converted to unsigned long. */
329
#if (1 ? -1 : 0U) <= 0
330
Bad arithmetic conversion.
334
/* n_13_7.t: Short-circuit evaluation of #if expression. */
336
/* 13.7: 10/0 or 10/MACRO_0 are never evaluated, "divide by zero" error
342
#if not_defined && 10 / not_defined
345
#if MACRO_0 && 10 / MACRO_0 > 1
348
#if MACRO_0 ? 10 / MACRO_0 : 0
351
#if MACRO_0 == 0 || 10 / MACRO_0 > 1
358
/* n_13_8.t: Grouping of sub-expressions in #if expression. */
360
/* 13.8: Unary operators are grouped from right to left. */
361
#if (- -1 != 1) || (!!9 != 1) || (-!+!9 != -1) || (~~1 != 1)
362
Bad grouping of -, +, !, ~ in #if expression.
365
/* 13.9: ?: operators are grouped from right to left. */
366
#if (1 ? 2 ? 3 ? 3 : 2 : 1 : 0) != 3
367
Bad grouping of ? : in #if expression.
370
/* 13.10: Other operators are grouped from left to right. */
371
#if (15 >> 2 >> 1 != 1) || (3 << 2 << 1 != 24)
372
Bad grouping of >>, << in #if expression.
375
/* 13.11: Test of precedence. */
376
#if 3*10/2 >> !0*2 >> !+!-9 != 1
377
Bad grouping of -, +, !, *, /, >> in #if expression.
380
/* 13.12: Overall test. Grouped as:
381
((((((+1 - -1 - ~~1 - -!0) & 6) | ((8 % 9) ^ (-2 * -2))) >> 1) == 7)
385
#if (((+1- -1-~~1- -!0&6|8%9^-2*-2)>>1)==7?7:0)!=7
386
Bad arithmetic of #if expression.
390
/* n_13_13.t: #if expression with macros. */
397
/* 13.13: With macros expanding to operators. */
399
#if (1 bitor 2) == 3 and 4 not_eq 5 or 0
400
/* #if (1 | 2) == 3 && 4 != 5 || 0 */
406
/* 13.14: With macros expanding to 0 token, nonsence but legal expression.*/
408
#if ZERO_TOKEN MACRO_1 ZERO_TOKEN > ZERO_TOKEN MACRO_0 ZERO_TOKEN
416
/* n_15.t: #ifdef, #ifndef directives. */
418
/* 15.1: #ifdef directive. */
426
/* 15.2: #ifndef directive. */
435
/* n_18.t: #define directive. */
437
/* Excerpts from ISO C 6.8.3 "Examples". */
438
#define OBJ_LIKE (1-1)
439
#define FTN_LIKE(a) ( a )
441
/* 18.1: Definition of an object-like macro. */
444
/* Macro defined to no token. */
448
/* 18.2: Definition of a function-like macro. */
452
/* 18.3: Spelling in string identical to parameter is not a parameter. */
454
#define STR( n1, n2) "n1:n2"
458
/* n_19.t: Valid re-definitions of macros. */
461
#define OBJ_LIKE /* white space */ (1-1) /* other */
464
#define FTN_LIKE( a )( /* note the white space */ \
465
a /* other stuff on this line
471
/* n_20.t: Definition of macro lexically identical to keyword. */
479
/* n_21.t: Tokenization (No preprocessing tokens are merged implicitly). */
487
/* n_22.t: Tokenization of preprocessing number. */
491
/* 22.1: 12E+EXP is a preprocessing number, EXP is not expanded. */
495
/* 22.2: .2e-EXP is also a preprocessing number. */
499
/* 22.3: + or - is allowed only following E or e, 12+EXP is not a
500
preprocessing number. */
501
/* Three tokens: 12 + 1; */
505
/* n_23.t: ## operator in macro definition. */
511
/* 23.2: Generate a preprocessing number. */
518
/* n_24.t: # operator in macro definition. */
524
/* 24.2: White spaces between tokens of operand are converted to one space.
527
str( ab /* comment */ +
530
/* 24.3: \ is inserted before \ and " in or surrounding literals and no
531
other character is inserted to anywhere. */
532
/* "'\"' + \"' \\\"\""; */
535
/* 24.4: Line splicing by <backslash><newline> is done prior to token
542
/* n_25.t: Macro arguments are pre-expanded (unless the argument is an
543
operand of # or ## operator) separately, that is, are macro-replaced
544
completely prior to rescanning. */
546
/* 25.1: "TWO_ARGS" is read as one argument to "sub", then expanded to
547
"a,b", then "x" is substituted by "a,b". */
551
/* 25.2: An argument pre-expanded to 0-token. */
555
/* 25.3: "glue( a, b)" is pre-expanded. */
557
xglue( glue( a, b), c);
559
/* 25.4: Operands of ## operator are not pre-expanded. */
560
/* MACRO_0MACRO_1; */
561
glue( MACRO_0, MACRO_1);
563
/* 25.5: Operand of # operator is not pre-expanded. */
568
/* n_26.t: The name once replaced is not furthur replaced. */
570
/* 26.1: Directly recursive macro definition. */
575
/* 26.2: Intermediately recursive macro definition. */
581
/* 26.3: Directly recursive function-like macro definition. */
583
#define f(a) a + f(a)
586
/* 26.4: Intermediately recursive function-like macro definition. */
588
#define g(a) a + h( a)
589
#define h(a) a + g( a)
592
/* 26.5: Rescanning encounters the non-replaced macro name. */
593
/* Z[0] + f( Z[0]); */
597
/* n_27.t: Rescanning of a macro expand any macro call in the replacement
598
text after substitution of parameters by pre-expanded-arguments. This
599
re-examination may involve the succeding sequences from the source
600
file (what a queer thing!). */
602
/* 27.1: Cascaded use of object-like macros. */
603
/* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8; */
604
#define NEST8 NEST7 + 8
605
#define NEST7 NEST6 + 7
606
#define NEST6 NEST5 + 6
607
#define NEST5 NEST4 + 5
608
#define NEST4 NEST3 + 4
609
#define NEST3 NEST2 + 3
610
#define NEST2 NEST1 + 2
614
/* 27.2: Cascaded use of function-like macros. */
615
/* (1) + (1 + 2) + 1 + 2 + 1 + 2 + 3 + 1 + 2 + 3 + 4; */
616
#define FUNC4( a, b) FUNC3( a, b) + NEST4
617
#define FUNC3( a, b) FUNC2( a, b) + NEST3
618
#define FUNC2( a, b) FUNC1( a, b) + NEST2
619
#define FUNC1( a, b) (a) + (b)
620
FUNC4( NEST1, NEST2);
622
/* 27.3: An identifier generated by ## operator is subject to expansion. */
627
#define math( op, a, b) op( (a), (b))
629
/* 27.4: 'sub' as an argument of math() is not pre-expanded, since '(' is
634
/* 27.5: Queer thing. */
639
/* n_28.t: __FILE__, __LINE__, __DATE__, __TIME__, __STDC__ and
640
__STDC_VERSION__ are predefined. */
664
/* In C99, the value of this macro is 199901L */
667
/* 28.7: __LINE__, __FILE__ in an included file. */
672
/* n_29.t: #undef directive. */
674
/* 29.1: Undefined macro is not a macro. */
680
/* 29.2: Undefining undefined name is not an error. */
684
/* n_30.t: Macro call. */
685
/* Note: Comma separate the arguments of function-like macro call,
686
but comma between matching inner parenthesis doesn't. This feature
687
is tested on so many places in this suite especially on *.c samples
688
which use assert() macro, that no separete item to test this feature
691
/* 30.1: A macro call may cross the lines. */
693
#define FUNC( a, b, c) a + b + c
704
/* n_32.t: Escape sequence in character constant in #if expression. */
706
/* 32.1: Character octal escape sequence. */
708
Bad evaluation of octal escape sequence.
711
/* 32.2: Character hexadecimal escape sequence. */
713
Bad evaluation of hexadecimal escape sequence.
717
/* n_37.t: Translation limits. */
719
/* 37.1: Number of parameters in macro: at least 31. */
720
#define glue31(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E) \
721
a##b##c##d##e##f##g##h##i##j##k##l##m##n##o##p##q##r##s##t##u##v##w##x##y##z##A##B##C##D##E
723
/* 37.2: Number of arguments of macro call: at least 31. */
724
/* ABCDEFGHIJKLMNOPQRSTUVWXYZabcde; */
725
glue31( A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R
726
, S, T, U, V, W, X, Y, Z, a, b, c, d, e);
728
/* 37.3: Significant initial characters in an internal identifier or a
729
macro name: at least 31. */
730
/* ABCDEFGHIJKLMNOPQRSTUVWXYZabcd_; */
731
ABCDEFGHIJKLMNOPQRSTUVWXYZabcd_;
733
/* 37.4: Nested conditional inclusion: at least 8 levels. */
761
/* 37.5: Nested source file inclusion: at least 8 levels. */
762
/* nest = 1; nest = 2; nest = 3; nest = 4;
763
nest = 5; nest = 6; nest = 7; nest = 8; */
767
/* 37.6: Parenthesized expression: at least 32 levels. */
769
#if 0 + (1 - (2 + (3 - (4 + (5 - (6 + (7 - (8 + (9 - (10 + (11 - (12 + \
770
(13 - (14 + (15 - (16 + (17 - (18 + (19 - (20 + (21 - (22 + (23 - \
771
(24 + (25 - (26 + (27 - (28 + (29 - (30 + (31 - (32 + 0)))))))))) \
772
)))))))))))))))))))))) == 0
776
/* 37.7: Characters in a string (after concatenation): at least 509. */
777
"123456789012345678901234567890123456789012345678901234567890123456789\
778
0123456789012345678901234567890123456789012345678901234567890123456789\
779
0123456789012345678901234567890123456789012345678901234567890123456789\
780
0123456789012345678901234567890123456789012345678901234567890123456789\
781
0123456789012345678901234567890123456789012345678901234567890123456789\
782
0123456789012345678901234567890123456789012345678901234567890123456789\
783
0123456789012345678901234567890123456789012345678901234567890123456789\
787
/* 37.8: Characters in a logical source line: at least 509. */
788
int a123456789012345678901234567890 = 123450; \
789
int b123456789012345678901234567890 = 123451; \
790
int c123456789012345678901234567890 = 123452; \
791
int d123456789012345678901234567890 = 123453; \
792
int e123456789012345678901234567890 = 123454; \
793
int f123456789012345678901234567890 = 123455; \
794
int A123456789012345678901234567890 = 123456; \
795
int B123456789012345678901234567890 = 123457; \
796
int C123456789012345678901234567890 = 123458; \
797
int D1234567890123456789012 = 123459;
799
/* 37.9: Macro definitions: at least 1024. */