This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Patch: PR preprocessor/28227
- From: Tom Tromey <tromey at redhat dot com>
- To: Gcc Patch List <gcc-patches at gcc dot gnu dot org>
- Date: 29 Dec 2006 08:26:10 -0700
- Subject: Patch: PR preprocessor/28227
- Reply-to: tromey at redhat dot com
This patch fixes PR preprocessor/28227.
Both '#ifdef defined' and '#ifndef defined' are valid according to the
C standard. This patch adds an argument to lex_macro_node to disable
the check for 'defined' in these cases.
While writing this I noticed that we silently accept '#if defined defined',
which according to the standard is undefined. I wasn't sure what, if
anything, to do about this, so I left it as-is.
Bootstrapped & tested on x86 FC6. New test case included.
Ok?
Tom
:ADDPATCH preprocesssor:
libcpp
2006-12-29 Tom Tromey <tromey@redhat.com>
PR preprocessor/28227:
* directives.c (lex_macro_node): Added 'def_or_undef' argument.
(do_define): Update.
(do_undef): Update.
(do_ifdef): Update.
(do_ifndef): Update.
gcc/testsuite
2006-12-29 Tom Tromey <tromey@redhat.com>
PR preprocessor/28227:
* gcc.dg/cpp/pr28227.c: New file.
Index: libcpp/directives.c
===================================================================
--- libcpp/directives.c (revision 120244)
+++ libcpp/directives.c (working copy)
@@ -1,6 +1,7 @@
/* CPP Library. (Directive handling.)
Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
- 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+ 1999, 2000, 2001, 2002, 2003, 2004, 2005,
+ 2006 Free Software Foundation, Inc.
Contributed by Per Bothner, 1994-95.
Based on CCCP program by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -102,7 +103,7 @@
static unsigned int read_flag (cpp_reader *, unsigned int);
static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
static void do_diagnostic (cpp_reader *, int, int);
-static cpp_hashnode *lex_macro_node (cpp_reader *);
+static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
static void do_include_common (cpp_reader *, enum include_type);
static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
@@ -504,7 +512,7 @@
/* Checks for validity the macro name in #define, #undef, #ifdef and
#ifndef directives. */
static cpp_hashnode *
-lex_macro_node (cpp_reader *pfile)
+lex_macro_node (cpp_reader *pfile, bool def_or_undef)
{
const cpp_token *token = _cpp_lex_token (pfile);
@@ -519,7 +527,7 @@
{
cpp_hashnode *node = token->val.node;
- if (node == pfile->spec_nodes.n_defined)
+ if (def_or_undef && node == pfile->spec_nodes.n_defined)
cpp_error (pfile, CPP_DL_ERROR,
"\"defined\" cannot be used as a macro name");
else if (! (node->flags & NODE_POISONED))
@@ -542,7 +550,7 @@
static void
do_define (cpp_reader *pfile)
{
- cpp_hashnode *node = lex_macro_node (pfile);
+ cpp_hashnode *node = lex_macro_node (pfile, true);
if (node)
{
@@ -561,7 +569,7 @@
static void
do_undef (cpp_reader *pfile)
{
- cpp_hashnode *node = lex_macro_node (pfile);
+ cpp_hashnode *node = lex_macro_node (pfile, true);
if (node)
{
@@ -1607,7 +1613,7 @@
if (! pfile->state.skipping)
{
- const cpp_hashnode *node = lex_macro_node (pfile);
+ const cpp_hashnode *node = lex_macro_node (pfile, false);
if (node)
{
@@ -1629,7 +1635,7 @@
if (! pfile->state.skipping)
{
- node = lex_macro_node (pfile);
+ node = lex_macro_node (pfile, false);
if (node)
{
Index: gcc/testsuite/gcc.dg/cpp/pr28227.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/pr28227.c (revision 0)
+++ gcc/testsuite/gcc.dg/cpp/pr28227.c (revision 0)
@@ -0,0 +1,10 @@
+/* Copyright (C) 2006 Free Software Foundation, Inc. */
+/* PR preprocessor/28227 */
+
+/* { dg-do preprocess } */
+#ifdef defined
+#endif
+#ifndef defined
+#endif
+
+int x;