Bug 57574

Summary: -std=c99 inline function incorrectly has external linkage with prior static declaration
Product: gcc Reporter: Szabolcs Nagy <nszabolcs>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Severity: normal    
Priority: P3    
Version: 4.8.0   
Target Milestone: 4.9.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Szabolcs Nagy 2013-06-09 23:45:17 UTC
gcc emits incorrect warning for inline function with internal linkage:

$ cat example.c
static int n;
static inline int f(void);
inline int f(void) {return n;}
int g(){return f();}

$ gcc-4.8 -std=c99 -c example.c 
example.c:3:28: warning: 'n' is static but used in inline function 'f' which is not static [enabled by default]
 inline int f(void) {return n;}
$ nm example.o
         U f
00000000 T g
00000000 b n

according to C99 6.2.2p4 the linkage of an identifier
is determined by the prior declaration
(internal in this case, inline does not change that)

referencing static objects from an inline definition
only violates the constraint in C99 6.7.4p3 if the
function has external linkage
(so the diagnostic is unjustified)

it seems from the warning that gcc thinks that f
is not static, this can break code:
the inline definition of f does not provide an
external definition and if the compiler choose
not to inline then the code has undefined f symbol
if the compiler thinks f has external linkage
(as demonstrated by nm above)
Comment 1 Joseph S. Myers 2013-11-29 01:30:45 UTC
Author: jsm28
Date: Fri Nov 29 01:30:42 2013
New Revision: 205506

URL: http://gcc.gnu.org/viewcvs?rev=205506&root=gcc&view=rev
	PR c/57574
	* c-decl.c (merge_decls): Clear DECL_EXTERNAL for a definition of
	an inline function following a static declaration.

	* gcc.dg/inline-35.c: New test.

Comment 2 Joseph S. Myers 2013-11-29 01:32:13 UTC
Fixed for 4.9.