[3.2] 4 cpplib patches
Neil Booth
neil@daikokuya.co.uk
Thu Aug 15 12:42:00 GMT 2002
This is a backport of four cpplib bugfixes that have gone into HEAD
recently.
Neil.
PR preprocessor/7358
PR preprocessor/7357
PR preprocessor/7526
* cppfiles.c (stack_include_file): Ignore main file if
appropriate. Correct test of whether a dependency should
be output.
* cppinit.c (init_dependency_output): Ignore main file
for SUNPRO_DEPENDENCIES.
(struct lang_flags): Rename trigraphs std.
(set_lang): Update.
* cpplib.c (run_directive): Kludge so _Pragma dependency works.
* cpplib.h (struct cpp_options): New members.
* cppmacro.c (collect_args): Flag whether to swallow a possible
future comma pasted with varargs.
(replace_args): Use this flag.
* doc/cpp.texi: Update varargs extension documentation.
* doc/cppenv.texi: Update.
testsuite:
* gcc.dg/cpp/_Pragma3.c, gcc.dg/cpp/vararg3.c, gcc.dg/cpp/vararg4.c:
New tests.
Index: cppfiles.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppfiles.c,v
retrieving revision 1.144
diff -u -p -r1.144 cppfiles.c
--- cppfiles.c 18 Jan 2002 13:40:28 -0000 1.144
+++ cppfiles.c 15 Aug 2002 17:34:30 -0000
@@ -310,9 +310,12 @@ stack_include_file (pfile, inc)
sysp = MAX ((pfile->map ? pfile->map->sysp : 0),
(inc->foundhere ? inc->foundhere->sysp : 0));
- /* For -M, add the file to the dependencies on its first inclusion. */
- if (CPP_OPTION (pfile, print_deps) > sysp && !inc->include_count)
- deps_add_dep (pfile->deps, inc->name);
+ /* Add the file to the dependencies on its first inclusion. */
+ if (CPP_OPTION (pfile, print_deps) > !!sysp && !inc->include_count)
+ {
+ if (pfile->buffer || CPP_OPTION (pfile, deps_ignore_main_file) == 0)
+ deps_add_dep (pfile->deps, inc->name);
+ }
/* Not in cache? */
if (! inc->buffer)
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.202.2.5
diff -u -p -r1.202.2.5 cppinit.c
--- cppinit.c 24 May 2002 09:26:48 -0000 1.202.2.5
+++ cppinit.c 15 Aug 2002 17:34:31 -0000
@@ -396,7 +396,7 @@ struct lang_flags
char objc;
char cplusplus;
char extended_numbers;
- char trigraphs;
+ char std;
char dollars_in_ident;
char cplusplus_comments;
char digraphs;
@@ -404,7 +404,7 @@ struct lang_flags
/* ??? Enable $ in identifiers in assembly? */
static const struct lang_flags lang_defaults[] =
-{ /* c99 objc c++ xnum trig dollar c++comm digr */
+{ /* c99 objc c++ xnum std dollar c++comm digr */
/* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
/* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
/* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
@@ -431,7 +431,8 @@ set_lang (pfile, lang)
CPP_OPTION (pfile, objc) = l->objc;
CPP_OPTION (pfile, cplusplus) = l->cplusplus;
CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
- CPP_OPTION (pfile, trigraphs) = l->trigraphs;
+ CPP_OPTION (pfile, std) = l->std;
+ CPP_OPTION (pfile, trigraphs) = l->std;
CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
CPP_OPTION (pfile, digraphs) = l->digraphs;
@@ -1880,7 +1881,10 @@ init_dependency_output (pfile)
{
spec = getenv ("SUNPRO_DEPENDENCIES");
if (spec)
- CPP_OPTION (pfile, print_deps) = 2;
+ {
+ CPP_OPTION (pfile, print_deps) = 2;
+ CPP_OPTION (pfile, deps_ignore_main_file) = 1;
+ }
else
return;
}
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
retrieving revision 1.290.2.1
diff -u -p -r1.290.2.1 cpplib.c
--- cpplib.c 14 Mar 2002 19:24:56 -0000 1.290.2.1
+++ cpplib.c 15 Aug 2002 17:34:31 -0000
@@ -406,12 +406,17 @@ run_directive (pfile, dir_no, buf, count
{
cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
/* from_stage3 */ true, 1);
+ /* Disgusting hack. */
+ if (dir_no == T_PRAGMA)
+ pfile->buffer->inc = pfile->buffer->prev->inc;
start_directive (pfile);
/* We don't want a leading # to be interpreted as a directive. */
pfile->buffer->saved_flags = 0;
pfile->directive = &dtable[dir_no];
(void) (*pfile->directive->handler) (pfile);
end_directive (pfile, 1);
+ if (dir_no == T_PRAGMA)
+ pfile->buffer->inc = NULL;
_cpp_pop_buffer (pfile);
}
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.201.2.1
diff -u -p -r1.201.2.1 cpplib.h
--- cpplib.h 14 Mar 2002 19:24:57 -0000 1.201.2.1
+++ cpplib.h 15 Aug 2002 17:34:32 -0000
@@ -279,6 +279,9 @@ struct cpp_options
/* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
unsigned char print_deps_append;
+ /* If true, no dependency is generated on the main file. */
+ unsigned char deps_ignore_main_file;
+
/* Nonzero means print names of header files (-H). */
unsigned char print_include_names;
@@ -334,6 +337,9 @@ struct cpp_options
/* Nonzero for the 1999 C Standard, including corrigenda and amendments. */
unsigned char c99;
+
+ /* Nonzero if conforming to some particular standard. */
+ unsigned char std;
/* Nonzero means give all the error messages the ANSI standard requires. */
unsigned char pedantic;
Index: cppmacro.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmacro.c,v
retrieving revision 1.91.6.6
diff -u -p -r1.91.6.6 cppmacro.c
--- cppmacro.c 29 May 2002 19:26:00 -0000 1.91.6.6
+++ cppmacro.c 15 Aug 2002 17:34:32 -0000
@@ -613,7 +613,20 @@ collect_args (pfile, node)
}
if (!error)
- return base_buff;
+ {
+ /* GCC has special semantics for , ## b where b is a varargs
+ parameter: we remove the comma if b was omitted entirely.
+ If b was merely an empty argument, the comma is retained.
+ If the macro takes just one (varargs) parameter, then we
+ retain the comma only if we are standards conforming.
+
+ If FIRST is NULL replace_args () swallows the comma. */
+ if (macro->variadic && (argc < macro->paramc
+ || (argc == 1 && args[0].count == 0
+ && !CPP_OPTION (pfile, std))))
+ args[macro->paramc - 1].first = NULL;
+ return base_buff;
+ }
_cpp_release_buff (pfile, base_buff);
return NULL;
@@ -799,15 +812,13 @@ replace_args (pfile, node, args)
count = arg->count, from = arg->first;
if (dest != first)
{
- /* GCC has special semantics for , ## b where b is a
- varargs parameter: the comma disappears if b was
- given no actual arguments (not merely if b is an
- empty argument); otherwise the paste flag is removed. */
if (dest[-1]->type == CPP_COMMA
&& macro->variadic
&& src->val.arg_no == macro->paramc)
{
- if (count == 0)
+ /* Swallow a pasted comma if from == NULL, otherwise
+ drop the paste flag. */
+ if (from == NULL)
dest--;
else
paste_flag = dest - 1;
Index: doc/cpp.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/cpp.texi,v
retrieving revision 1.24.2.3
diff -u -p -r1.24.2.3 cpp.texi
--- doc/cpp.texi 14 Mar 2002 19:24:59 -0000 1.24.2.3
+++ doc/cpp.texi 15 Aug 2002 17:34:34 -0000
@@ -1676,6 +1676,15 @@ eprintf ("success!\n")
@expansion{} fprintf(stderr, "success!\n");
@end example
+@noindent
+The above explanation is ambiguous about the case where the only macro
+parameter is a variable arguments parameter, as it is meaningless to
+try to distinguish whether no argument at all is an empty argument or
+a missing argument. In this case the C99 standard is clear that the
+comma must remain, however the existing GCC extension used to swallow
+the comma. So CPP retains the comma when conforming to a specific C
+standard, and drops it otherwise.
+
C99 mandates that the only place the identifier @code{@w{__VA_ARGS__}}
can appear is in the replacement list of a variadic macro. It may not
be used as a macro name, macro argument name, or within a different type
Index: doc/cppenv.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/cppenv.texi,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 cppenv.texi
--- doc/cppenv.texi 11 Mar 2002 19:59:58 -0000 1.1.2.1
+++ doc/cppenv.texi 15 Aug 2002 17:34:34 -0000
@@ -66,7 +66,8 @@ with an optional @option{-MT} switch too
This variable is the same as the environment variable
@env{DEPENDENCIES_OUTPUT} (@pxref{DEPENDENCIES_OUTPUT}), except that
system header files are not ignored, so it implies @option{-M} rather
-than @option{-MM}.
+than @option{-MM}. However, the dependence on the main input file is
+omitted.
@ifset cppmanual
@xref{Invocation}.
@end ifset
Index: testsuite/gcc.dg/cpp/_Pragma3.c
===================================================================
RCS file: testsuite/gcc.dg/cpp/_Pragma3.c
diff -N testsuite/gcc.dg/cpp/_Pragma3.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/_Pragma3.c 15 Aug 2002 17:34:36 -0000
@@ -0,0 +1,11 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc. */
+
+/* { dg-do preprocess } */
+
+/* Pragma buffers have a NULL "inc" member, which we would dereference
+ when getting a file's date and time.
+
+ Based on PR 7526. 14 Aug 2002. */
+
+#define GCC_PRAGMA(x) _Pragma (#x)
+GCC_PRAGMA(GCC dependency "mi1c.h")
Index: testsuite/gcc.dg/cpp/vararg3.c
===================================================================
RCS file: testsuite/gcc.dg/cpp/vararg3.c
diff -N testsuite/gcc.dg/cpp/vararg3.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/vararg3.c 15 Aug 2002 17:34:36 -0000
@@ -0,0 +1,17 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc. */
+
+/* { dg-do preprocess } */
+/* { dg-options "-std=c99" } */
+
+/* Source: Neil Booth, 6 Aug 2002.
+
+ Tests that we DTRT with varargs commas for a single-parameter macro
+ when in standards-conforming mode. */
+
+#define f(...) , ## __VA_ARGS__
+
+/* The comma from f's expansion should be retained (standards
+ conforming mode only). Tests that it isn't in non-standards mode
+ include macro8.c and vararg1.c. */
+#if 2 f() 3 /* { dg-bogus "missing binary operator" } */
+#endif
Index: testsuite/gcc.dg/cpp/vararg4.c
===================================================================
RCS file: testsuite/gcc.dg/cpp/vararg4.c
diff -N testsuite/gcc.dg/cpp/vararg4.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/vararg4.c 15 Aug 2002 17:34:36 -0000
@@ -0,0 +1,14 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc. */
+
+/* { dg-do preprocess } */
+/* { dg-options -std=gnu99 } */
+
+/* Source: Neil Booth, 6 Aug 2002.
+
+ Tests that we DTRT with varargs commas. */
+
+#define g(a, ...) a , ## __VA_ARGS__
+
+/* The comma from g's expansion should be retained. */
+#if g (2, ) 3 /* { dg-bogus "missing binary operator" } */
+#endif
More information about the Gcc-patches
mailing list