From: Neil Booth Date: Tue, 4 Jul 2000 11:51:43 +0000 (+0000) Subject: macro1.c: Add more macro expansion tests. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=d2c1325af0ee17d224af50909f0dbcc70526b98d;p=gcc.git macro1.c: Add more macro expansion tests. * gcc.dg/cpp/macro1.c: Add more macro expansion tests. * gcc.dg/cpp/strify2.c: Same. From-SVN: r34867 --- diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1231daba1ab..2824980484e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2000-07-04 Neil Booth + + * gcc.dg/cpp/macro1.c: Add more macro expansion tests. + * gcc.dg/cpp/strify2.c: Same. + 2000-07-03 Zack Weinberg * testsuite/gcc.dg/cpp/19951025-1.c: Adjust regexps. diff --git a/gcc/testsuite/gcc.dg/cpp/macro1.c b/gcc/testsuite/gcc.dg/cpp/macro1.c index 60347ed4b60..e7235f577c2 100644 --- a/gcc/testsuite/gcc.dg/cpp/macro1.c +++ b/gcc/testsuite/gcc.dg/cpp/macro1.c @@ -2,21 +2,26 @@ /* { dg-do run } */ -/* Tests various macros are correctly expanded. */ +/* Tests various macro abuse is correctly expanded. */ extern int puts (const char *); extern void abort (void); -#define err(str) do { puts(str); abort(); } while (0) +extern int strcmp(const char *s1, const char *s2); +#define err(str) do { puts(str); abort(); } while (0) #define j(x, y) x + y #define k(x, y) j(x + 2, y + +#define glue(x, y) x ## y +#define xglue(x, y) glue(x, y) +/* Functions called when macros are left unexpanded. */ int q(int x) {return x + 40;} int B(int x) {return x + 20;} int foo(int x) {return x + 10;} int bar(int x, int y) {return x + y;} int baz(int x, int y) {return x + y;} int toupper(int x) {return x + 32;} +int M(int x) {return x * 2;} int main (int argc, char *argv[]) { @@ -48,25 +53,76 @@ int main (int argc, char *argv[]) if (apply(bar, baz) != 42) /* foo(foo(baz(22, 0))). */ err ("apply bar baz"); + /* Taken from glibc. */ #define __tobody(c, f) f (c) #define toupper(c) __tobody (c, toupper) if (toupper (10) != 42) /* toupper (10). */ err ("toupper"); + /* This tests that M gets expanded the right no. of times. Too many + times, and we get excess "2 +"s and the wrong sum. Derived from + nested stpcpy in dggettext.c. */ +#define M(x) 2 + M(x) +#define stpcpy(a) M(a) + if (stpcpy (stpcpy (9)) != 42) /* 2 + M (2 + M (9)) */ + err ("stpcpy"); + + /* Another test derived from nested stpcpy's of dggettext.c. Uses + macro A(x) and function B(x) as defined above. The problem was + the same - excess "1 +"s and the wrong sum. */ +#define B(x) 1 + B(x) +#define C(x) A(x) + if (C(B(0)) != 42) /* 1 + B (1 + B (0)) */ + err ("C"); + + /* More tests derived from gcc itself - the use of XEXP and COST. + These first two should both expand to the same thing. */ + { + int insn = 6, i = 2, b = 2; +#define XEXP(RTX, N) (RTX * N + 2) +#define PATTERN(INSN) XEXP(INSN, 3) + if (XEXP (PATTERN (insn), i) != 42) /* ((insn * 3 + 2) * i + 2) */ + err ("XEXP (PATTERN)"); + if (XEXP (XEXP (insn, 3), i) != 42) /* ((insn * 3 + 2) * i + 2) */ + err ("XEXP (XEXP)"); + +#define COST(X) XEXP (XEXP (X, 4), 4) + if (COST (b) != 42) /* ((b * 4 + 2) * 4 + 2) */ + err ("COST"); + } + + /* This tests macro recursion and expand-after-paste. */ +#define FORTYTWO "forty" +#define TWO TWO "-two" + if (strcmp (glue(FORTY, TWO), "forty")) + err ("glue"); + if (strcmp (xglue(FORTY, TWO), "forty-two")) + err ("xglue"); + + /* Test ability to call macro over multiple logical lines. */ + if (q + (42) != 42 + || q ( + 42) != 42 + || q (42 + ) != 42 + || q + ( + 42 + ) + != 42) + err ("q over multiple lines"); + + /* Corner case. Test that macro expansion is turned off for later + q, when not at start but at end of argument context, and supplied + with the '(' necessary for expansion. */ + if (q(1 + q)(1) != 42) /* 1 + q(1) */ + err ("Nested q"); + /* This looks like it has too many ')', but it hasn't. */ if (k(1, 4) 35) != 42) err ("k"); - /*#define B(x) Z B(x) -#define XEXP(RTX, N) RTX->fld[N].rtx -#define PATTERN(INSN) XEXP(INSN, 3) -#define COST(X) XEXP (XEXP (x, 0), 0) -#define M(a) OK M (a) -#define stpcpy(a) M(a) -#define C(x) A(x) -XEXP (PATTERN (insn), i); -XEXP (XEXP (insn, 3), i); -COST (b)*/ - + /* Phew! */ return 0; } diff --git a/gcc/testsuite/gcc.dg/cpp/strify2.c b/gcc/testsuite/gcc.dg/cpp/strify2.c index ddde976feae..18777bd9b7e 100644 --- a/gcc/testsuite/gcc.dg/cpp/strify2.c +++ b/gcc/testsuite/gcc.dg/cpp/strify2.c @@ -16,8 +16,9 @@ extern void abort (void); int main (int argc, char *argv[]) { - str (\); /* { dg-warning "valid string" "str(\)" } */ - str (\\\); /* { dg-warning "valid string" "str(\\\)" } */ + str (\); /* { dg-warning "valid string" "str(\\)" } */ + str (\\); /* OK. */ + str (\\\); /* { dg-warning "valid string" "str(\\\\\\)" } */ /* This also serves as a useful test of the value of __INCLUDE_LEVEL. */ if (strcmp (xstr (__INCLUDE_LEVEL__), "0")) @@ -26,6 +27,9 @@ int main (int argc, char *argv[]) if (strcmp(str (__INCLUDE_LEVEL__), "__INCLUDE_LEVEL__")) err ("macro name"); + if (strcmp(str(), "") || strcmp(str( ), "")) + err ("empty string"); + if (strcmp(str ("s\n"), "\"s\\n\"")) err ("quoted string");