[comp.lang.c] prototypes, old and new compilers

stuart@bms-at.UUCP (Stuart Gathman) (12/15/88)

To take advantage of prototypes but still remain compatible with older
compilers, we do the following:

    For ANSI compilers:

	int func(/**/ type, type /**/);

    For K&R compilers:

	int func(/* type, type */);

A convert program (e.g. sed) can easily turn '(/**/' into '(/*' and
vice versa.

This works well, but there are still some things on my wish list.

	1) many compilers take:

	      int func(/**/ type name1, type name2 /**/);

	   but we can't use this feature (to make header files more
	   self documenting) because of the ones that don't.

	2) Ansi requires '...' in a prototype to indicate varargs functions.  
	   Many compilers won't take '...' but use trailing comma instead.
	   
        3) The '/**/' is annoying when working with an ANSI compiler.

Does anyone have a parser that can recognize function prototypes and

	1) replace them with comments
		or
	2) strip the optional identifiers
		or
	3) add or remove the trailing '...'

By making some reasonable assumptions about coding style, it should work
without having to process typedefs or run the preprocessor.  I have started
such an animal, but don't have a lot of time to spend on it.  Has it been
done already?
-- 
Stuart D. Gathman	<stuart@bms-at.uucp>
			<..!{vrdxhq|daitc}!bms-at!stuart>

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (12/17/88)

In article <136@bms-at.UUCP> stuart@bms-at.UUCP (Stuart Gathman) writes:

| Does anyone have a parser that can recognize function prototypes and
| 
| 	1) replace them with comments
| 		or
| 	2) strip the optional identifiers
| 		or
| 	3) add or remove the trailing '...'

  I will share with you a little technique I use for allowing procedure
declarations to hold what I want without having to rewrite the source
code. Be warned, this addresses (1) and (2), and most of (3), but it's
really ugly. Also, some compilers which take prototypes with type info
don't accept elipsis, while others do. The comment shows where I change
this if need be.
________________________________________________________________

#define NoProto		1
#define TypesOnly	2
#define FullANSI	3

#ifndef	P
#define P NoProto
#endif

#if	P == NoProto
#define Param(x) ()
#endif
#if	P == TypesOnly
#define Param(x) x
#define Ptype(x, y) x
#define Pelipsis			/* is this one right for you? */
#endif
#if	P == FullANSI
#define Param(x) x
#define Ptype(x,y) x y
#define Pelipsis ,...
#endif

int someproc Param((Ptype(int *,x),Ptype(long,ypos)));
int myprintf Param((Ptype(char *, format) Pelipsis));
________________________________________________________________

This will doubtless start a macro war, in which ten people will tell me
there's a better way, and another ten will ignore the comment about
variable arguments and tell me Pelipsis has the wrong value for
TypesOnly.

Oh well, I hope this actually helps someone.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

pjh@mccc.UUCP (Pete Holsberg) (12/17/88)

On a somewhat-related topic:

	Some authors call the first line of a function definition a
declaration.  Some don't call it anything.  Is there an official name?

	Thanks,
	Pete
	

-- 
Pete Holsberg                   UUCP: {...!rutgers!}princeton!mccc!pjh
Mercer College			CompuServe: 70240,334
1200 Old Trenton Road           GEnie: PJHOLSBERG
Trenton, NJ 08690               Voice: 1-609-586-4800

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/17/88)

In article <494@mccc.UUCP> pjh@mccc.UUCP (Pete Holsberg) writes:
>	Some authors call the first line of a function definition a
>declaration.  Some don't call it anything.  Is there an official name?

"Function header" is what I usually hear it called.