aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorNeil Booth <neilb@earthling.net>2000-11-01 20:21:13 +0000
committerNeil Booth <neil@gcc.gnu.org>2000-11-01 20:21:13 +0000
commita0a9606d4b5b7545b6dcabaa9a8a915ab96bd166 (patch)
treead692a7f6210d539ee8ff1304361b204914f4b9e /gcc
parentfc05d51600acbf3cc72646e985db62b45b6f377f (diff)
downloadgcc-a0a9606d4b5b7545b6dcabaa9a8a915ab96bd166.zip
gcc-a0a9606d4b5b7545b6dcabaa9a8a915ab96bd166.tar.gz
gcc-a0a9606d4b5b7545b6dcabaa9a8a915ab96bd166.tar.bz2
cpplib.c (run_directive): Use correct line number for output of _Pragma.
* cpplib.c (run_directive): Use correct line number for output of _Pragma. Remember any in-progress directive. * gcc.dg/cpp/vararg2.c, gcc.dg/cpp/_Pragm1.c: New tests. From-SVN: r37194
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog7
-rw-r--r--gcc/cpplib.c15
-rw-r--r--gcc/testsuite/gcc.dg/cpp/_Pragma1.c41
-rw-r--r--gcc/testsuite/gcc.dg/cpp/vararg2.c131
4 files changed, 190 insertions, 4 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f463051..e04fef7 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2000-11-01 Neil Booth <neilb@earthling.net>
+
+ * cpplib.c (run_directive): Use correct line number for output
+ of _Pragma. Remember any in-progress directive.
+
+ * gcc.dg/cpp/vararg2.c, gcc.dg/cpp/_Pragm1.c: New tests.
+
2000-11-01 Richard Henderson <rth@redhat.com>
* stmt.c (expand_start_null_loop): Set continue_label.
diff --git a/gcc/cpplib.c b/gcc/cpplib.c
index cd14ba4..738b1ba 100644
--- a/gcc/cpplib.c
+++ b/gcc/cpplib.c
@@ -349,9 +349,12 @@ run_directive (pfile, dir_no, buf, count, name)
size_t count;
const char *name;
{
+ unsigned int output_line = pfile->lexer_pos.output_line;
+
if (cpp_push_buffer (pfile, (const U_CHAR *) buf, count) != NULL)
{
- const struct directive *dir = &dtable[dir_no];
+ const struct directive *dir = &dtable[dir_no], *orig_dir;
+ unsigned char orig_in_directive;
if (name)
CPP_BUFFER (pfile)->nominal_fname = name;
@@ -360,7 +363,11 @@ run_directive (pfile, dir_no, buf, count, name)
/* A kludge to avoid line markers for _Pragma. */
if (dir_no == T_PRAGMA)
- pfile->lexer_pos.output_line = CPP_BUFFER (pfile)->prev->lineno;
+ pfile->lexer_pos.output_line = output_line;
+
+ /* Save any in-process directive; _Pragma can appear in one. */
+ orig_dir = pfile->directive;
+ orig_in_directive = pfile->state.in_directive;
/* For _Pragma, the text is passed through preprocessing stage 3
only, i.e. no trigraphs, no escaped newline removal, and no
@@ -371,8 +378,8 @@ run_directive (pfile, dir_no, buf, count, name)
pfile->state.prevent_expansion++;
(void) (*dir->handler) (pfile);
pfile->state.prevent_expansion--;
- pfile->directive = 0;
- pfile->state.in_directive = 0;
+ pfile->directive = orig_dir;
+ pfile->state.in_directive = orig_in_directive;
skip_rest_of_line (pfile);
if (pfile->buffer->cur != pfile->buffer->rlimit)
diff --git a/gcc/testsuite/gcc.dg/cpp/_Pragma1.c b/gcc/testsuite/gcc.dg/cpp/_Pragma1.c
new file mode 100644
index 0000000..8759b0a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/_Pragma1.c
@@ -0,0 +1,41 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc. */
+
+/* { dg-do preprocess } */
+
+/* Tests the _Pragma operator. Contributed by Neil Booth 1 Nov 2000. */
+
+/* Within the preprocessor, the easy pragma to test is "poison". */
+
+#pragma GCC poison p1
+p1 /* { dg-error "poisoned" } */
+
+/* Standard use of _Pragma. */
+_Pragma ("GCC poison p2")
+p2 /* { dg-error "poisoned" } */
+
+/* I can see no reason it cannot appear in a directive. Check we
+ enter the conditional. Putting the _Pragma at EOL also catches
+ nasties like not saving current lexer state properly. Also test
+ that wide strings are OK. */
+#if 1 _Pragma (L"GCC poison p3")
+p3 /* { dg-error "poisoned" } */
+#endif
+
+#define M1 _Pragma ("GCC poison p4")
+p4 /* No problem; not yet poisoned. */
+#define M2(x) _Pragma (#x)
+
+/* Now test macro expansion with embedded _Pragmas. */
+M1 p4 /* { dg-error "poisoned" } */
+M2 (GCC poison p5) p5 /* { dg-error "poisoned" } */
+
+/* Look, ma! These things even nest. */
+_Pragma ("_Pragma (\"GCC poison p6\") GCC poison p7")
+p6 /* { dg-error "poisoned" } */
+p7 /* { dg-error "poisoned" } */
+
+/* Check we ignore them in false conditionals. */
+#if 0
+_Pragma ("GCC poison p8")
+#endif
+p8 /* No problem. */
diff --git a/gcc/testsuite/gcc.dg/cpp/vararg2.c b/gcc/testsuite/gcc.dg/cpp/vararg2.c
new file mode 100644
index 0000000..2b2b033
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/vararg2.c
@@ -0,0 +1,131 @@
+/* { dg-do preprocess } */
+/* { dg-options "-std=gnu99" } */
+
+/* Jamie's varargs macros from hell. Not for the faint of heart.
+ Great tests that C99 and GNU varargs give identical results.
+ Adapted to the testsuite by Neil Booth, 1 Nov 2000. */
+
+/* Permission is granted to use, copy, modify and distribute this file
+ freely for any purpose whatsoever. This file is free software, and
+ there's no warranty.
+
+ -- Jamie Lokier <jamie.lokier@cern.ch>, 25/Sep/2000. */
+
+#define dup3(x) x,x,x
+
+/* Count elements in a list (0 to 10 max). */
+#define gnu_count(y...) _gnu_count1 ( , ##y)
+#define _gnu_count1(y...) _gnu_count2 (y,10,9,8,7,6,5,4,3,2,1,0)
+#define _gnu_count2(_,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,n,ys...) n
+
+/* Tail of a list. */
+#define gnu_tail(y...) _gnu_tail (y)
+#define _gnu_tail(x,y...) y
+
+/* Repeat N times. */
+#define gnu_repeat(n, x) gnu_tail (_gnu_repeat (n, x))
+#define _gnu_repeat(n, x) _gnu_repeat_##n (x)
+#define _gnu_repeat_0(x)
+#define _gnu_repeat_1(x) ,x
+#define _gnu_repeat_2(x) ,x,x
+#define _gnu_repeat_3(x) ,x,x,x
+#define _gnu_repeat_4(x) ,x,x,x,x
+#define _gnu_repeat_5(x) ,x,x,x,x,x
+
+#define _gnu_keep(xs...) xs
+#define _gnu_discard(xs...)
+#define _gnu_split_r(n,xs...) _gnu_split_rd (n,_gnu_keep,_gnu_discard xs)
+#define _gnu_split_d(n,xs...) _gnu_split_rd (n,_gnu_discard,_gnu_keep xs)
+#define _gnu_split_rd(n,xs...) _gnu_split_##n (xs)
+#define _gnu_split_0(a,b,xs...) a() b(xs)
+#define _gnu_split_1(a,b,x0,xs...) a(x0) b(xs)
+#define _gnu_split_2(a,b,x0,x1,xs...) a(x0,x1) b(xs)
+#define _gnu_split_3(a,b,x0,x1,x2,xs...) a(x0,x1,x2) b(xs)
+#define _gnu_split_4(a,b,x0,x1,x2,x3,xs...) a(x0,x1,x2,x3) b(xs)
+#define _gnu_split_5(a,b,x0,x1,x2,x3,x4,xs...) a(x0,x1,x2,x3,x4) b(xs)
+
+/* List manipulations. Surprise: index zero is the rightmost element. */
+#define gnu_take(n, xs...) \
+ _gnu_split_d (_gnu_count1 ( , ## xs), _gnu_repeat (n, _gnu_error) , ## xs)
+#define gnu_drop(n, xs...) \
+ _gnu_split_d (n,,_gnu_split_r (_gnu_count1 ( , ## xs), _gnu_repeat (n, _gnu_error) , ## xs))
+#define gnu_index(pos, xs...) gnu_take (1, gnu_drop (pos , ## xs))
+
+/* C99 __VA_ARGS__ versions */
+#define c99_count(...) _c99_count1 ( , ##__VA_ARGS__)/* If only ## worked.*/
+#define _c99_count1(...) _c99_count2 (__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0)
+#define _c99_count2(_,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,n,...) n
+
+#define c99_tail(...) _c99_tail (__VA_ARGS__)
+#define _c99_tail(x,...) __VA_ARGS__
+
+/* Repeat N times. */
+#define c99_repeat(n, x) c99_tail (_c99_repeat (n, x))
+#define _c99_repeat(n, x) _c99_repeat_##n (x)
+#define _c99_repeat_0(x)
+#define _c99_repeat_1(x) ,x
+#define _c99_repeat_2(x) ,x,x
+#define _c99_repeat_3(x) ,x,x,x
+#define _c99_repeat_4(x) ,x,x,x,x
+#define _c99_repeat_5(x) ,x,x,x,x,x
+
+#define _c99_keep(...) __VA_ARGS__
+#define _c99_discard(...)
+#define _c99_split_r(n,...) _c99_split_rd(n,_c99_keep,_c99_discard __VA_ARGS__)
+#define _c99_split_d(n,...) _c99_split_rd(n,_c99_discard,_c99_keep __VA_ARGS__)
+#define _c99_split_rd(n,...) _c99_split_##n (__VA_ARGS__)
+#define _c99_split_0(a,b,...) a() b(__VA_ARGS__)
+#define _c99_split_1(a,b,x0,...) a(x0) b(__VA_ARGS__)
+#define _c99_split_2(a,b,x0,x1,...) a(x0,x1) b(__VA_ARGS__)
+#define _c99_split_3(a,b,x0,x1,x2,...) a(x0,x1,x2) b(__VA_ARGS__)
+#define _c99_split_4(a,b,x0,x1,x2,x3,...) a(x0,x1,x2,x3) b(__VA_ARGS__)
+#define _c99_split_5(a,b,x0,x1,x2,x3,x4,...) a(x0,x1,x2,x3,x4) b(__VA_ARGS__)
+
+/* List manipulations. Surprise: index zero is the rightmost element. */
+#define c99_take(n, ...) \
+ _c99_split_d (_c99_count1 ( , ## __VA_ARGS__), _c99_repeat (n, _c99_error) , ## __VA_ARGS__)
+#define c99_drop(n, ...) \
+ _c99_split_d (n,,_c99_split_r (_c99_count1 ( , ## __VA_ARGS__), _c99_repeat (n, _c99_error) , ## __VA_ARGS__))
+#define c99_index(pos, ...) c99_take (1, c99_drop (pos , ## __VA_ARGS__))
+
+/************** Expansions **************/
+
+/* Correct answers are 0, 0, 1, 2, 10. */
+#if _gnu_count1 () != 0 || gnu_count () != 0 || gnu_count (A) != 1 \
+ || gnu_count (,) != 2 || gnu_count (A, B, C, D, E, F, G, H, I, J) != 10
+#error gnu_count
+#endif
+
+/* Correct answers are empty, 'x'. */
+#if gnu_repeat (0, 'x') gnu_repeat (1, 'x') != 'x'
+#error gnu_repeat
+#endif
+
+/* Correct answers are "e", "b", "a", empty. */
+#if gnu_index (0, 'a', 'b', 'c', 'd', 'e') != 'e' \
+ || gnu_index (3, 'a', 'b', 'c', 'd', 'e') != 'b' \
+ || gnu_index (4, 'a', 'b', 'c', 'd', 'e') != 'a' \
+ gnu_index (5, 'a', 'b', 'c', 'd', 'e')
+#error gnu_index
+#endif
+
+/************* C99 tests *************/
+
+/* The answers are 0, 0, 1, 2, 10 as for the non-C99 version. */
+#if _c99_count1 () != 0 || c99_count () != 0 || c99_count (A) != 1 \
+ || c99_count (,) != 2 || c99_count (A, B, C, D, E, F, G, H, I, J) != 10
+#error c99_count
+#endif
+
+/* Correct answers are empty, 'x'. */
+#if c99_repeat (0, 'x') c99_repeat (1, 'x') != 'x'
+#error c99_repeat
+#endif
+
+/* Correct answers are "e", "b", "a", empty. */
+#if c99_index (0, 'a', 'b', 'c', 'd', 'e') != 'e' \
+ || c99_index (3, 'a', 'b', 'c', 'd', 'e') != 'b' \
+ || c99_index (4, 'a', 'b', 'c', 'd', 'e') != 'a' \
+ c99_index (5, 'a', 'b', 'c', 'd', 'e')
+#error gnu_index
+#endif