This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch for bug 15360 (static decl followed by initialized extern)


This patch fixes bug 15360, incorrect errors for

static int a;
extern int a = 1;

(I don't like the warning for variables initialized and declared
extern, commented on in that bug report, being mandatory instead of
being controlled by some option included in -Wall, but that isn't
really a bug in itself.)

Bootstrapped with no regressions on i686-pc-linux-gnu.  Applied to
mainline.

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2004-07-25  Joseph S. Myers  <jsm@polyomino.org.uk>

	PR c/15360
	* c-decl.c (start_decl): Do not set DECL_EXTERNAL for initialized
	declarations until after calling pushdecl.
	(grokdeclarator): Set DECL_EXTERNAL for variables based on use of
	"extern" and not on whether the declaration is initialized.

testsuite:
2004-07-25  Joseph S. Myers  <jsm@polyomino.org.uk>

	PR c/15360
	* gcc.dg/pr15360-1.c: New test.

diff -rupN GCC.orig/gcc/c-decl.c GCC/gcc/c-decl.c
--- GCC.orig/gcc/c-decl.c	2004-07-21 23:57:58.000000000 +0000
+++ GCC/gcc/c-decl.c	2004-07-24 00:00:13.000000000 +0000
@@ -2761,7 +2761,6 @@ start_decl (tree declarator, tree declsp
 
   if (initialized)
     {
-      DECL_EXTERNAL (decl) = 0;
       if (current_scope == file_scope)
 	TREE_STATIC (decl) = 1;
 
@@ -2828,6 +2827,9 @@ start_decl (tree declarator, tree declsp
      TEM may equal DECL or it may be a previous decl of the same name.  */
   tem = pushdecl (decl);
 
+  if (initialized)
+    DECL_EXTERNAL (tem) = 0;
+
   return tem;
 }
 
@@ -4594,7 +4596,10 @@ grokdeclarator (tree declarator, tree de
 	if (inlinep)
 	  pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
 
-	DECL_EXTERNAL (decl) = extern_ref;
+	/* At file scope, an initialized extern declaration may follow
+	   a static declaration.  In that case, DECL_EXTERNAL will be
+	   reset later in start_decl.  */
+	DECL_EXTERNAL (decl) = !!(specbits & (1 << (int) RID_EXTERN));
 
 	/* At file scope, the presence of a `static' or `register' storage
 	   class specifier, or the absence of all storage class specifiers
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/pr15360-1.c GCC/gcc/testsuite/gcc.dg/pr15360-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/pr15360-1.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/pr15360-1.c	2004-07-22 14:56:43.000000000 +0000
@@ -0,0 +1,24 @@
+/* Static declarations followed by extern are OK even if the extern
+   declaration is initialized.  Bug 15360 from hozelda at
+   yahoo.com.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+static int a;
+static int a;
+extern int a;
+static int a;
+
+static int b;
+extern int b = 1; /* { dg-warning "initialized and declared" "extern init warning" } */
+static int b;
+static int b;
+
+static int c; /* { dg-error "previous declaration" "" } */
+int c; /* { dg-error "non-static" "correct error" } */
+
+static int d; /* { dg-error "previous declaration" "" } */
+int d = 1; /* { dg-error "non-static" "correct error" } */
+
+void foo (void) { extern int e = 1; } /* { dg-error "has both" "extern init in function" } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]