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] Fix PR19315.


Hi,

As noted in the PR, GCC C currently has an "undocumented extension" that
silently makes zero-sized static objects into 'extern' as well as
'static’.  The warning that would normally be emitted is suppressed without
 ‘-pedantic’.

This means that they don't get allocated (appearing in the object as an
undefined extern).  This is surprising behaviour and can cause assembly-
time fails on at least one target, since such arrays can still be
referenced in the generated code.  For most targets, presumably there
would be a link-time fail - also a 'surprising' result for the end user.

Effectively,  ISTM we accept wrong code silently without the “-pedantic” flag.

It would seem that if there was a compelling use-case for this, then we
should expose it via a flag, rather than making it the default.

The patch removes the code marking these objects as extern.

Two tests in the test-suite needed change, neither of which appears to
be intentionally testing this "extension".  The amended graphite test
generates identical code.

Bootstrapped on x86_64-linux-gnu (all langs), x86_64-apple-darwin.

OK for trunk?

iain

2018-08-14  Iain Sandoe <iain@sandoe.co.uk>

gcc/c:

	PR c/19315
	* c-decl.c (finish_decl): Don't add the 'extern' storage class to
	objects of unknown size.

gcc/testsuite:

	PR c/19315
	gcc.dg/graphite/pr82451.c: Make array 'a' an extern.
	gcc.dg/redecl-10.c: Expect warnings for the static vars with unknown
	size.
---
 gcc/c/c-decl.c                          | 8 --------
 gcc/testsuite/gcc.dg/graphite/pr82451.c | 2 +-
 gcc/testsuite/gcc.dg/redecl-10.c        | 4 ++--
 3 files changed, 3 insertions(+), 11 deletions(-)

diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index ed1dd28cac..da42add073 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -4969,14 +4969,6 @@ finish_decl (tree decl, location_t init_loc, tree init,
 	case 2:
 	  if (do_default)
 	    error ("array size missing in %q+D", decl);
-	  /* If a `static' var's size isn't known,
-	     make it extern as well as static, so it does not get
-	     allocated.
-	     If it is not `static', then do not mark extern;
-	     finish_incomplete_decl will give it a default size
-	     and it will get allocated.  */
-	  else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
-	    DECL_EXTERNAL (decl) = 1;
 	  break;
 
 	case 3:
diff --git a/gcc/testsuite/gcc.dg/graphite/pr82451.c b/gcc/testsuite/gcc.dg/graphite/pr82451.c
index 802b931fdd..b2c439bb56 100644
--- a/gcc/testsuite/gcc.dg/graphite/pr82451.c
+++ b/gcc/testsuite/gcc.dg/graphite/pr82451.c
@@ -1,7 +1,7 @@
 /* { dg-do compile } */
 /* { dg-options "-O -floop-parallelize-all" } */
 
-static int a[];
+extern int a[];
 int b[1];
 int c;
 static void
diff --git a/gcc/testsuite/gcc.dg/redecl-10.c b/gcc/testsuite/gcc.dg/redecl-10.c
index 525961e7e3..0864311f64 100644
--- a/gcc/testsuite/gcc.dg/redecl-10.c
+++ b/gcc/testsuite/gcc.dg/redecl-10.c
@@ -5,7 +5,7 @@
 /* { dg-do compile } */
 /* { dg-options "-g" } */
 
-static int w[];
+static int w[]; /* { dg-warning "array 'w' assumed to have one element" } */
 void
 f (void)
 {
@@ -19,7 +19,7 @@ g (void)
   extern int x[] = { 3, 4, 5 }; /* { dg-error "has both" } */
 }
 
-static int y[];
+static int y[]; /* { dg-warning "array 'y' assumed to have one element" } */
 void
 h (void)
 {
-- 
2.17.1



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