Bug 17205 - [4.0 Regression] Strange behaviour when function prototypes appear inside other functions
Summary: [4.0 Regression] Strange behaviour when function prototypes appear inside oth...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, wrong-code
Depends on:
Blocks:
 
Reported: 2004-08-27 06:14 UTC by Anton Blanchard
Modified: 2004-09-13 14:15 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-08-27 06:16:44


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anton Blanchard 2004-08-27 06:14:44 UTC
gcc version 3.5.0 20040827 (experimental)

A 2.6 kernel compilation encountered a link failure. The following sequence
shows how things fail if a function protoype appears inside another function:

#define BROKE

#ifndef BROKE
static void foo(void);
#endif

int main()
{
#ifdef BROKE
        static void foo(void);
#endif
        foo();
}

static void foo(void)
{
        printf("bar\n");
}

define BROKE and foo disappears completely, undefine it and it works as
expected. To be honest Im not even sure this is valid C.
Comment 1 Andrew Pinski 2004-08-27 06:16:44 UTC
This is not valid C.
We should be warning or erroring out.
Comment 2 Joseph S. Myers 2004-08-27 09:44:40 UTC
Subject: Re:  Strange behaviour when function prototypes appear
 inside other functions

On Fri, 27 Aug 2004, pinskia at gcc dot gnu dot org wrote:

> This is not valid C.
> We should be warning or erroring out.

To be precise, it's compile-time undefined for block-scope function 
declarations to have any storage-class specifier other than extern.  I 
don't object to removing this undocumented extension, but this bug report 
shows it does have users.

The problem appears with a compiler from before my patch for bug 13801 
went in, so it is not a problem caused by that patch.  However, an 
appropriate fix if we keep the extension would probably be along the lines 
I suggested for a possible future refinement to that patch: add an 
internal scope between the external scope and the file scope, which stores 
hidden declarations of objects and functions with internal linkage used 
for type checking the same way as the external scope, then merge the 
handling of static function declarations at block scope with the handling 
of external and file scope declarations.  (So everything with external 
linkage goes in the external scope, in addition to its proper scope, and 
everything with internal linkage in a given translation unit goes in the 
internal scope, in addition to its proper scope, static function 
declarations at block scope are defined as having internal linkage, and 
the internal scope, just like the external scope, accumulates composite 
type information from all declarations, while the file scope and scopes 
below that have the type information from visible declarations only.)

I might look at doing this, but not immediately given the problem isn't 
caused by my patch.

Comment 3 Geoff Keating 2004-08-27 13:51:08 UTC
This is probably related to 12738.
Comment 4 GCC Commits 2004-08-30 22:19:35 UTC
Subject: Bug 17205

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	geoffk@gcc.gnu.org	2004-08-30 22:19:24

Modified files:
	gcc            : ChangeLog c-decl.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.c-torture/compile: 920625-2.c 
	gcc/testsuite/gcc.c-torture/execute: 20010605-1.c nest-stdar-1.c 
	gcc/testsuite/gcc.dg: 20011130-1.c 20021014-1.c 20030331-2.c 
Added files:
	gcc/testsuite/gcc.dg: funcdef-storage-1.c 

Log message:
	2004-08-30  Geoffrey Keating  <geoffk@apple.com>
	
	PR 12738
	PR 17205
	Radar 3460526
	Radar 3775729
	* c-decl.c (grokdeclarator): Produce error for 'static' on local
	function declaration.
	
	Index: testsuite/ChangeLog
	2004-08-30  Geoffrey Keating  <geoffk@apple.com>
	
	* gcc.dg/funcdef-storage-1.c: New.
	* gcc.c-torture/compile/920625-2.c: Don't use 'static' to declare
	an extern function in local scope.
	* gcc.c-torture/execute/20010605-1.c: Don't use 'static inline' to
	declare an 'auto inline' function in local scope.
	* gcc.dg/20011130-1.c: Likewise.
	* gcc.c-torture/execute/nest-stdar-1.c: Don't use 'static' to declare
	an 'auto' function in local scope.
	* gcc.dg/20021014-1.c: Likewise.
	* gcc.dg/20030331-2.c: Likewise.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.5161&r2=2.5162
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-decl.c.diff?cvsroot=gcc&r1=1.565&r2=1.566
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4211&r2=1.4212
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/compile/920625-2.c.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20010605-1.c.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/nest-stdar-1.c.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/funcdef-storage-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20011130-1.c.diff?cvsroot=gcc&r1=1.1&r2=1.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20021014-1.c.diff?cvsroot=gcc&r1=1.10&r2=1.11
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030331-2.c.diff?cvsroot=gcc&r1=1.3&r2=1.4

Comment 5 Geoff Keating 2004-08-30 22:23:33 UTC
Fixed, by declaring this code invalid and producing an error message on it.  The kernel will need to be 
changed to Not Do That, by moving the prototype for the 'static' function to file scope.
Comment 6 GCC Commits 2004-09-01 22:12:26 UTC
Subject: Bug 17205

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	apple-ppc-branch
Changes by:	geoffk@gcc.gnu.org	2004-09-01 22:12:16

Modified files:
	gcc            : c-decl.c 
	gcc/testsuite/gcc.c-torture/compile: 920625-2.c 
	gcc/testsuite/gcc.c-torture/execute: 20010605-1.c nest-stdar-1.c 
	gcc/testsuite/gcc.dg: 20011130-1.c 20021014-1.c 20030331-2.c 
Added files:
	gcc/testsuite/gcc.dg: funcdef-storage-1.c 

Log message:
	PR 12738
	PR 17205
	Radar 3460526
	Radar 3775729
	* c-decl.c (grokdeclarator): Produce error for 'static' on local
	function declaration.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-decl.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.334.2.87.2.19&r2=1.334.2.87.2.20
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/compile/920625-2.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.2&r2=1.2.116.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20010605-1.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.2&r2=1.2.92.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/nest-stdar-1.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.2&r2=1.2.116.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/funcdef-storage-1.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=NONE&r2=1.2.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20011130-1.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.1&r2=1.1.88.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20021014-1.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.1.8.9&r2=1.1.8.9.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030331-2.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.2.4.1.8.1&r2=1.2.4.1.8.2