1 /* Copyright (C) 2000 Free Software Foundation, Inc. */
2
3 /* { dg-do run } */
4 /* { dg-options "-std=c99 -pedantic-errors" } */
5
6 /* Test ## behavior and corner cases thoroughly. The macro expander
7 failed many of these during development. */
8
9 #ifndef __WCHAR_TYPE__
10 #define __WCHAR_TYPE__ int
11 #endif
12 typedef __WCHAR_TYPE__ wchar_t;
13
14 extern int strcmp (const char *, const char *);
15 extern int puts (const char *);
16 extern void abort (void);
17 #define err(str) do { puts(str); abort(); } while (0)
18
19 #define EMPTY
20 #define str(x) #x
21 #define xstr(x) str(x)
22 #define glue(x, y) x ## y
23 #define xglue(x, y) glue (x, y)
24 #define glue3(x, y, z) x ## y ## z
25 #define glue_var(x, ...) x ## __VA_ARGS__
26
27 #define __muldi3 __NDW(mul, 3 = 50)
28 #define __NDW(a,b) __ ## a ## di ## b
29 #define m3 NDW()
30 #define NDW(x) m3 ## x = 50
31 #define five 5
32 #define fifty int fif ## ty
33
34 /* Defines a function called glue, returning what it is passed. */
35 int glue (glue,) (int x)
36 {
37 return x;
38 }
39
main()40 int main ()
41 {
42 /* m3 and __muldi3 would sometimes cause an infinite loop. Ensure
43 we only expand fifty once. */
44 fifty = 50, m3, __muldi3;
45
46 /* General glue and macro expanding test. */
47 int five0 = xglue (glue (fi, ve), 0);
48
49 /* Tests only first and last tokens are pasted, and pasting to form
50 the != operator. Should expand to: if (five0 != 50). */
51 if (glue3 (fi, ve0 !,= glue (EMPTY 5, 0)))
52 err ("five0 != 50");
53
54 /* Test varags pasting, and pasting to form the >> operator. */
55 if (glue_var(50 >, > 1 != 25))
56 err ("Operator >> pasting");
57
58 /* The LHS should not attempt to expand twice, and thus becomes a
59 call to the function glue. */
60 if (glue (gl, ue) (12) != 12)
61 err ("Recursive macros");
62
63 /* Test placemarker pasting. The glued lines should all appear
64 neatly in the same column and below each other, though we don't
65 test that here. */
66 {
67 int glue3(a, b, ) = 1, glue3(a,,) = 1;
68 glue3(a, , b)++;
69 glue3(, a, b)++;
70 glue3(,a,)++;
71 glue3(,,a)++;
72 if (a != 3 || ab != 3 glue3(,,))
73 err ("Placemarker pasting");
74 }
75
76 /* Test that macros in arguments are not expanded. */
77 {
78 int glue (EMPTY,1) = 123, glue (T, EMPTY) = 123;
79 if (EMPTY1 != 123 || TEMPTY != 123)
80 err ("Pasted arguments macro expanding");
81 }
82
83 /* Test various paste combinations. */
84 {
85 const wchar_t* wc_array = glue(L, "wide string");
86 wchar_t wc = glue(L, 'w');
87 const char * hh = xstr(xglue(glue(%, :), glue(%, :)));
88 int array glue (<, :) 1 glue (:, >) = glue(<, %) 1 glue(%, >);
89 int x = 4;
90
91 if (array[0] != 1)
92 err ("Digraph pasting");
93
94 x glue (>>, =) 1; /* 2 */
95 x glue (<<, =) 1; /* 4 */
96 x glue (*, =) 2; /* 8 */
97 x glue (+, =) 100; /* 108 */
98 x glue (-, =) 50; /* 58 */
99 x glue (/, =) 2; /* 29 */
100 x glue (%, =) 20; /* 9 */
101 x glue (&, =) 254; /* 8 */
102 x glue (|, =) 16; /* 24 */
103 x glue (^, =) 18; /* 10 */
104
105 if (x != 10 || 0 glue (>, =) 1 glue (|, |) 1 glue (<, =) 0)
106 err ("Various operator pasting");
107 if (strcmp (hh, "%:%:"))
108 err ("Pasted digraph spelling");
109 if ((glue (., 1) glue (!, =) .1))
110 err ("Pasted numbers 1");
111 /* glue3 here will only work if we paste left-to-right. If a
112 future implementation does not do this, change the test. */
113 if (glue3 (1.0e, +, 1) != 10.0)
114 err ("Pasted numbers 2");
115 }
116
117 return 0;
118 }
119