[comp.lang.c] ANSI <--> K&R conversion utilities - COMMING SOON

rfg@pink.ACA.MCC.COM (Ron Guilmette) (05/31/89)

In article <CLINE.89May29171059@sun.soe.clarkson.edu> cline@sun.soe.clarkson.edu (Marshall Cline) writes:
 >In all likelihood we'll all be living in the transition from K&R to ANSI-C
 >for a long while.  Like most of us (probably), I've written a macro package
 >that converts prototypes for ANSI/K&R compatibility.  However this is only
 >a partial fix.  What I'd really like is a real program that filters a K&R
 >module and spits out an ANSI module (and another filter for the vise versa
 >operation).  I can see a number of things this/these filter[s] would need
 >to do, including:
 >
 >(1) Convert between K&R & ANSI prototypes in function _DEFINITIONS_.  Ex:
 >	ANSI:	double myfunct(int i, int j) {...
 >	K&R:	double myfunct(i, j) {int i, j; ...
 >
 >(2) Convert between K&R & ANSI prototypes in function _DECLARATIONS_.  Ex:
 >	ANSI:	double myfunct(int i, int j);
 >	K&R:	double myfunct();

I have a program which does (1) and (2) above.  It is written in C (fairly
portable) and it seems to be quite fast (not that that matters much).  The
important thing is that it seems to do a good job for perhaps > 95% of all
declarations and definitions (based on a large test case, i.e. the GNU C
compiler).  The only cases where it will mess up is where there are MACRO
calls in the vicinity of the thing being protoized, or where some style (new
or old) of varargs stuff going on for a particular function.  Both cases are
rare in practice.  If you embed comments in your function headers, this
can also cause a failure, although many such cases are handled correctly.

One nice aspect to my tool is that it converts a whole *system* of code.
That is to say it can use information gleaned from a K&R style function
definition in one file to protoize a function declaration for the same
function in some different file (usually an include file).

Protoization is useful for conversion to either ANSI C or to C++.

My protoizer works in two steps.  First, prototype information must be
collected.  A fairly brute-force approach is used for this step.  I use a
modified version of the GNU C compiler to collect the prototype information.
Once this information has been collected for all relevant source files, the
"protoize" tool gobbles up this information and automatically edits prototypes
into all relevant source files (except system include files, or other files
that the user does not have write access to).

The protoizer has been tested on the GCC compiler itself, which is (I believe)
somewhere in the neighborhood of 150,000 lines of code.  It handled all proto-
typing correctly, and only got a little confused by the (occasional) varargs
functions.

Depending on the speed with which a legal handover of this code to the Free
Software Foundation is executed, I hope to see FSF distributing this code to
all interested users fairly soon.

 >(3) In the case of "int" functions, many ANSI prototypes would have to be
 >    added to the code before they are called (starting to get messy).  Ex:
 >    if "myfunct(x, y)" is called in the K&R code but isn't declared
 >    previously, it could be assumed to return an "int", and its parameter
 >    types could presumably be figured out, resulting in an ANSI prototype:
 >	int myfunct(foo_t param1, bar_t param2);

You can get GCC to warn you about implicit function declarations.  I suggest that
you do this, and then add in explicit (K&R) declarations where needed.  Later,
you can use my protoizer to get them automatically ANSI-fied.

 >(4) When converting from ANSI to K&R, the "automatic cast" facilities
 >    provided by the prototypes would have to be changed to "explicit casts".
 >    Ex: If "myfunct()" accepts a "long", and "i" is an "int", then the
 >    ANSI code "myfunct(i)" would have to be translated to "myfunct((long)i)"
 >    for the K&R compilers.

Who wants to go backwards?

 >(5) In the reverse of (4), explicit casts to parameter types might be
 >    removable when K&R code is translated to ANSI.  See example just above.

Leave them in.  They do no harm.

 >(6) The names of the <standard> header files could be changed/added/removed.

Wait until somebody has a <standard> set of header files before you do this.

 >(7) The variable argument schemes are different, so the code associated with
 >    <varargs.h> and <stdarg.h> conversions could be made.

Yep.  That's a problem.  It will probably have to be solved "by hand".

-- 
// Ron Guilmette  -  MCC  -  Experimental Systems Kit Project
// 3500 West Balcones Center Drive,  Austin, TX  78759  -  (512)338-3740
// ARPA: rfg@mcc.com
// UUCP: {rutgers,uunet,gatech,ames,pyramid}!cs.utexas.edu!pp!rfg

diamond@diamond.csl.sony.junet (Norman Diamond) (05/31/89)

In article <CLINE.89May29171059@sun.soe.clarkson.edu> cline@sun.soe.clarkson.edu (Marshall Cline) writes:

> >(4) When converting from ANSI to K&R, the "automatic cast" facilities
> >    provided by the prototypes would have to be changed to "explicit casts".
> >    Ex: If "myfunct()" accepts a "long", and "i" is an "int", then the
> >    ANSI code "myfunct(i)" would have to be translated to "myfunct((long)i)"
> >    for the K&R compilers.

In article <229@pink.ACA.MCC.COM> rfg@pink.aca.mcc.com.UUCP (Ron Guilmette) writes:

>Who wants to go backwards?

Anyone who has to use a K&R compiler, e.g. PCC.  Technically this
doesn't answer the question; this answers "Who HAS to go backwards?"
Anyway, Mr. Cline is right; the perverse transform is also useful.

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-implementing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?