annotate gcc/testsuite/gcc.dg/cpp/paste2.c @ 131:84e7813d76e9

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