[gnu.gcc.bug] gcc prototype enforcer

geoff@warwick.UUCP (Geoff Rimmer) (02/18/89)

Earlier this year, Paul Hudson summarised to thsi newsgroup, the
responses he got to his request for desired warnings for gcc. 

One thing that I didn't see mentioned was a warning that would
complain like hell if you use *ANY* function (including C library
functions) without giving it a function prototype beforehand.  Our
current version (gcc v1.32) will complain (with -Wall) if no mention
whatsoever is made of the function, and rightly so.  However, if the
function is declared in the old style, eg. (from /usr/include/stdio.h)

	extern char *fgets();

gcc doesn't say a thing.

The reason why I would like gcc to shout when it sees a situation like
this, is because I was writing a program and used 'fgets'.  Unfortunately,
I accidentally swapped the first 2 parameters around, that is to say I put 

	while (fgets(BUFSIZ,buffer,fpr)) {...}

rather than

	while (fgets(buffer,BUFSIZ,fpr)) {...}


Now, I had included <stdio.h>, so gcc knew about fgets.  However in my
protos.h file (where I keep a list of library functions), I had mistakenly
omitted fgets().  So, the program compiled fine, but I got a segmentation
fault when I ran it. 

If gcc would print a warning whenever it sees a function that has not
been declared as a *prototype*, I would have added the missing 

	extern fgets (char *, int, FILE *);

and then on re-compilation, it would have been able to spot that the
arguments were not correctly ordered. 

Presumably when we get full ANSI C, all the standard header files will
have the prototypes and so this situation will become less common.
However, it would still be very useful for converting old-style C
programs to ANSI C - making sure that EVERY function you write has its
own prototype.

If it would be possible, I'd like to see this feature in the next
version of gcc.

Thanks for listening!

geoff M-b M-c

	------------------------------------------------------------
	Geoff Rimmer, Computer Science, Warwick University, England.
			geoff@uk.ac.warwick.emerald

	"Filthy, if this is supposed to be some sort of joke..."
	"Then it'll be the first on the show this week!!"
	"Shut up, Eddie.  The show's only just begun, and we've had
	 some super laughs already."

			- Filthy, Rich and Catflap, 1986.
	------------------------------------------------------------

geoff@warwick.UUCP (Geoff Rimmer) (02/18/89)

In article <1261@ubu.warwick.UUCP> I wrote:
>If gcc would print a warning whenever it sees a function that has not
>been declared as a *prototype*, I would have added the missing 
>
>	extern fgets (char *, int, FILE *);
>

Sorry, that should be 

	extern char *fgets (char *, int, FILE *);

Geoff

mdt@YAHI.STANFORD.EDU (Michael Tiemann) (02/20/89)

> whatsoever is made of the function, and rightly so.  However, if the
> function is declared in the old style, eg. (from /usr/include/stdio.h)
> 
> 	extern char *fgets();
> 
> gcc doesn't say a thing.

The GNU C++ compiler has a flag -fstrict-prototype

When this option is enabled, it treats

	extern char *fgets ();

as

	extern char *fgets (void);

and this will complain as desired.  Perhaps it would be a good idea to
put that code into GNU CC.

Michael

paul@UUNET.UU.NET (Paul Hudson) (02/20/89)

I added this in a provate version last year some time. I'll add it again.
I'm about half-way through adding the warnings, so I may soon deliver, instead
of just promising!.

Paul Hudson 

Snail mail: Monotype ADG	Email:	...!ukc!acorn!moncam!paul
	    Science Park,		paul@moncam.co.uk
	    Milton Road,	"Sun Microsysytems:
	    Cambridge,		 The Company is Arrogant (TM)"
	    CB4 4FQ