Undefined static functions

Mark Mitchell mark@codesourcery.com
Tue Aug 31 23:55:00 GMT 2004


Joseph S. Myers wrote:

>C requires a diagnostic if a static function is used but not defined in a 
>translation unit.  The present implementation of this is a pedwarn in 
>check_global_declarations in toplev.c.
>
>(a) Which language front ends use this pedwarn, rather than avoiding 
>getting that far with a translation unit using an undefined static 
>function?
>
>(b) Is there any reason this shouldn't be a hard error rather than just a 
>pedwarn?
>
>(c) This diagnostic is broken for C with -funit-at-a-time (implied by 
>-O2), ": Search converges between 2004-07-08-trunk (#482) and 
>2004-07-09-trunk (#483).".  Test (should be diagnosed, now isn't at -O2):
>
>static void f0(void);
>void g0(void) { f0(); }
>
>I would guess TREE_SYMBOL_REFERENCED is being tested too early for 
>unit-at-a-time mode; certainly it's the wrong way to implement this 
>diagnostic.
>
>(d) It also has always been broken with regard to calls that are optimised 
>away; C90 and C99 are explicit that it is any use outside sizeof (sizeof 
>whose result is an integer constant, in C99) that is erroneous.  For 
>example,
>
>static void f1(void);
>void g1(void) { if (0) { f1(); } }
>
>should be diagnosed.  Is there any reason this shouldn't be a hard error 
>as well?
>
>I propose to fix this C90 issue (which was missing from my original C90 
>project proposal) in the front end, and there is nothing tricky about so 
>doing (flag all used static function decls, ignoring uses inside 
>__alignof__ and keeping lists inside sizeof/typeof so they can be flagged 
>afterwards if the result turns out nonconstant).  It would however be 
>convenient to be able to get rid of the checks in toplev.c (or make them 
>internal_error if no errors have been given by the front end) by having 
>all front ends give proper errors, to avoid the need to do anything 
>special to avoid duplicate error messages.
>
>(e) Consider the following C++ variation on the above program:
>
>inline static void f1(void);
>void g1(void) { if (0) { f1(); } }
>
>Does "no diagnostic required" in C++03 [basic.def.odr] paragraph 3 apply 
>to the whole paragraph, or only to the sentence it appears in?  That is, 
>is "An inline function shall be defined in every translation unit in which 
>it is used." a diagnosable rule?  
>
Yes.  (And there's nothing wrong with issuing diagnostics that are not 
strictly required.)

>If so, this program should be diagnosed 
>by the C++ compiler (and the existing diagnostic for inline functions used 
>but not defined should be a pedwarn or error not a warning).
>  
>
I agree.

-- 
Mark Mitchell
CodeSourcery, LLC
(916) 791-8304
mark@codesourcery.com




More information about the Gcc mailing list