OSDN Git Service

Index: gcc/ChangeLog
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / cpp / macro1.c
1 /* Copyright (C) 2000 Free Software Foundation, Inc.  */
2
3 /* { dg-do run } */
4
5 /* Tests various macro abuse is correctly expanded.  */
6
7 extern int puts (const char *);
8 extern void abort (void);
9 extern int strcmp(const char *s1, const char *s2);
10
11 #define err(str) do { puts(str); abort(); } while (0)
12 #define j(x, y) x + y
13 #define k(x, y) j(x + 2, y +
14 #define glue(x, y) x ## y
15 #define xglue(x, y) glue(x, y)
16
17 /* Functions called when macros are left unexpanded.  */
18 int q(int x)            {return x + 40;}
19 int B(int x)            {return x + 20;}
20 int foo(int x)          {return x + 10;}
21 int bar(int x, int y)   {return x + y;}
22 int baz(int x, int y)   {return x + y;}
23 int toupper(int x)      {return x + 32;}
24 int M(int x)            {return x * 2;}
25
26 int main (int argc, char *argv[])
27 {
28 #define q(x) x
29   if (q(q)(2) != 42)
30     err ("q");
31
32 #define A(x) B(x)
33   if (A(A(2)) != 42)
34     err ("A");
35
36 #define E(x) A x
37 #define F (22)
38   if (E(F) != 42)
39     err ("E(F)");
40
41 #define COMMA ,
42 #define NASTY(a) j(a 37)
43   if (NASTY (5 COMMA) != 42)
44     err ("NASTY");
45
46 #define bar(x, y) foo(x(y, 0))
47 #define apply(x, y) foo(x(y, 22))
48 #define bam bar
49   if (bar(bar, 32) != 42)       /* foo(bar(32, 0)).  */
50     err ("bar bar");
51   if (bar(bam, 32) != 42)       /* Same.  */
52     err ("bar bam");
53   if (apply(bar, baz) != 42)    /* foo(foo(baz(22, 0))).  */
54     err ("apply bar baz");
55
56   /* Taken from glibc.  */
57 #define __tobody(c, f) f (c)
58 #define toupper(c) __tobody (c, toupper)
59   if (toupper (10) != 42)       /* toupper (10). */
60     err ("toupper");
61
62   /* This tests that M gets expanded the right no. of times.  Too many
63      times, and we get excess "2 +"s and the wrong sum.  Derived from
64      nested stpcpy in dggettext.c.  */
65 #define M(x) 2 + M(x)
66 #define stpcpy(a) M(a)
67   if (stpcpy (stpcpy (9)) != 42) /*  2 + M (2 + M (9)) */
68     err ("stpcpy");
69
70   /* Another test derived from nested stpcpy's of dggettext.c.  Uses
71      macro A(x) and function B(x) as defined above.  The problem was
72      the same - excess "1 +"s and the wrong sum.  */
73 #define B(x) 1 + B(x)
74 #define C(x) A(x)
75   if (C(B(0)) != 42)            /* 1 + B (1 + B (0)) */
76     err ("C");
77
78   /* More tests derived from gcc itself - the use of XEXP and COST.
79      These first two should both expand to the same thing.  */
80   {
81     int insn = 6, i = 2, b = 2;
82 #define XEXP(RTX, N)  (RTX * N + 2)
83 #define PATTERN(INSN) XEXP(INSN, 3)
84     if (XEXP (PATTERN (insn), i) != 42) /* ((insn * 3 + 2) * i + 2) */
85       err ("XEXP (PATTERN)");
86     if (XEXP (XEXP (insn, 3), i) != 42) /* ((insn * 3 + 2) * i + 2) */
87       err ("XEXP (XEXP)");
88
89 #define COST(X) XEXP (XEXP (X, 4), 4)
90     if (COST (b) != 42)         /* ((b * 4 + 2) * 4 + 2) */
91       err ("COST");
92   }
93
94   /* This tests macro recursion and expand-after-paste.  */
95 #define FORTYTWO "forty"
96 #define TWO TWO "-two"
97   if (strcmp (glue(FORTY, TWO), "forty"))
98     err ("glue");
99   if (strcmp (xglue(FORTY, TWO), "forty-two"))
100     err ("xglue");
101
102   /* Test ability to call macro over multiple logical lines.  */
103   if (q
104       (42) != 42
105       || q (
106          42) != 42
107       || q (42
108             ) != 42
109       || q
110       (
111        42
112        )
113       != 42)
114     err ("q over multiple lines");
115
116   /* Corner case.  Test that macro expansion is turned off for later
117      q, when not at start but at end of argument context, and supplied
118      with the '(' necessary for expansion.  */
119   if (q(1 + q)(1) != 42)        /* 1 + q(1) */
120     err ("Nested q");
121
122   /* This looks like it has too many ')', but it hasn't.  */
123   if (k(1, 4) 35) != 42)
124     err ("k");
125
126   /* Phew! */
127   return 0;
128 }