| | 1 | /* |
| | 2 | * ansi C++ 16.3.4(5) - tests of macro substitution rules (including |
| | 3 | * recursive substitutions), stringizing, and token pasting |
| | 4 | */ |
| | 5 | |
| | 6 | /* make sure __FILE__ and __LINE__ are provided */ |
| | 7 | file = __FILE__; |
| | 8 | line = __LINE__; |
| | 9 | |
| | 10 | /* check __DATE__ and __TIME__ */ |
| | 11 | //date = __DATE__; // these always change, so omit them |
| | 12 | //time = __TIME__; // for automated testing |
| | 13 | |
| | 14 | #define x 3 |
| | 15 | #define f(a) f(x * (a)) |
| | 16 | #undef x |
| | 17 | #define x 2 |
| | 18 | #define g f |
| | 19 | #define z z[0] |
| | 20 | #define h g(~ |
| | 21 | #define m(a) a(w) |
| | 22 | #define w 0,1 |
| | 23 | #define t(a) a |
| | 24 | |
| | 25 | t(t(g)(0) + t)(1); |
| | 26 | f(f(z)); |
| | 27 | f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); |
| | 28 | g(x+(3,4)-w) | h 5) & m |
| | 29 | (f)^m(m); |
| | 30 | |
| | 31 | "Result should be:"; |
| | 32 | "f(2 * (0)) + t(1);"; |
| | 33 | "f(2 * (f(2 * (z[0]))));"; |
| | 34 | "f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);"; |
| | 35 | "f(2 * (2+(3,4)-0,1)) | f(2 * (~5)) & f(2 * (0,1))^m(0,1);"; |
| | 36 | |
| | 37 | /* clean up */ |
| | 38 | #undef t |
| | 39 | #undef w |
| | 40 | #undef m |
| | 41 | #undef h |
| | 42 | #undef z |
| | 43 | #undef g |
| | 44 | #undef f |
| | 45 | #undef x |
| | 46 | |
| | 47 | |
| | 48 | /* |
| | 49 | * ansi C++ 16.3.4(7) |
| | 50 | */ |
| | 51 | |
| | 52 | /* VALID */ |
| | 53 | #define OBJ_LIKE (1-1) |
| | 54 | #define OBJ_LIKE1 /* white space */ (1-1) /* other */ |
| | 55 | #define FTN_LIKE(a) ( a ) |
| | 56 | #define FTN_LIKE( a )( /* note the white space */ \ |
| | 57 | a /* other stuff on this line |
| | 58 | */ ) |
| | 59 | |
| | 60 | /* INVALID */ |
| | 61 | #define OBJ_LIKE (0) /* different token sequence */ |
| | 62 | #define OBJ_LIKE (1 - 1) /* different white space */ |
| | 63 | #define FTN_LIKE(b) ( a ) /* different parameter usage */ |
| | 64 | #define FTN_LIKE(b) ( b ) /* different parameter spelling */ |
| | 65 | |
| | 66 | /* clean up */ |
| | 67 | #undef OBJ_LIKE |
| | 68 | #undef FTN_LIKE |
| | 69 | #undef OBJ_LIKE1 |
| | 70 | |
| | 71 | /* |
| | 72 | * stringizing |
| | 73 | */ |
| | 74 | |
| | 75 | #define str(s) # s |
| | 76 | #define xstr(s) str(s) |
| | 77 | #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \ |
| | 78 | x ## s, x ## t) |
| | 79 | #define INCFILE(n) vers ## n /* from previous #include example */ |
| | 80 | #define glue(a, b) a ## b |
| | 81 | #define xglue(a, b) glue(a, b) |
| | 82 | #define HIGHLOW "hello" |
| | 83 | #define LOW LOW ", world" |
| | 84 | |
| | 85 | debug(1, 2); |
| | 86 | fputs(str(strncmp("abc\0d", "abc", '\4')/* this goes away */ |
| | 87 | == 0) str(: @\n), s); |
| | 88 | #include xstr(INCFILE(2).h) |
| | 89 | glue(HIGH, LOW); >>> should be: "hello" |
| | 90 | xglue(HIGH, LOW); >>> should be: "hello" ", world" |
| | 91 | |
| | 92 | |
| | 93 | "should be:"; |
| | 94 | 'printf("x" "1" "= %d, x" "2" "= %s", x1, x2);' |
| | 95 | 'fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s);' |
| | 96 | '#include "vers2.h" (after macro replacement, before file access)' |
| | 97 | '"hello";' |
| | 98 | '"hello" ", world";' |
| | 99 | |
| | 100 | /* |
| | 101 | * include file syntax |
| | 102 | */ |
| | 103 | |
| | 104 | #undef INCFILE |
| | 105 | #define INCFILE vers2.h |
| | 106 | |
| | 107 | "should not expand macros on the following two lines"; |
| | 108 | #include "INCFILE" |
| | 109 | #include <INCFILE> |
| | 110 | |
| | 111 | #undef INCFILE |
| | 112 | #define INCFILE "vers2.h" |
| | 113 | "should expand macros on the following line"; |
| | 114 | #include INCFILE |
| | 115 | |
| | 116 | |