[tex-live] K&R C compilers.

Nelson H. F. Beebe beebe@math.utah.edu
Fri, 3 Jan 2003 08:54:11 -0700 (MST)


Olaf Weber <olaf@infovore.xs4all.nl> writes on 03 Jan 2003 12:57:46 +0100

>> Does anyone know whether it would be feasible to assume that all the
>> compilers used to compile TeX-live are ANSI C?  Or least that they
>> support the following:
>> 
>> - void
>> - prototypes
>> - stdarg.h (for variadic functions)

Please call it Standard C, not ANSI C; the ISO Standard has replaced
the ANSI one, and the C Standards committee recommends ``Standard C''
as the informal designation of ``International ISO/IEC Standard
9899:1999(E),  Programming languages -- C''.

I routinely build and test code with more than 60 C and C++ compilers
on up to 16 different UNIX platforms.  Today, the ONLY system that I
have access to that lacks a C89 compiler is HP-UX 10.01; the vendor
has one, but it costs extra, and the local management chose not to pay
for it.  I cannot build gcc successfully on that particular system, so
I've pretty much given up on using it for code development.

I have made a decision in my own coding projects to make all new
projects C89 (and C++98 and C99) conformant.  However, I will not
remove K&R support from older code, since this would take time that I
lack, and would serve no useful purpose.  Function prototypes in such
code look like this:

	extern void do_ISBN_file ARGS((const char *pathlist_,const
	char *name_));

and function declarations look like this:

	#if defined(HAVE_STDC)
	void
	do_ISBN_file(const char *pathlist, const char *name)
	#else /* K&R style */
	void
	do_ISBN_file(pathlist,name)
	const char *pathlist;
	const char *name;
	#endif
	{
	...
	}

Under pure C89, they would look like this:

	extern void do_ISBN_file (const char *pathlist_,const char *name_);

	void
	do_ISBN_file(const char *pathlist, const char *name)
	{
	...
	}

A lot of the Web2c code already has similar wrappers on function
declarations and definitions.

However, if a decision is made to move TeXlive coding to Standard C,
then the code should be made strictly C89 conformant, including correct
prototypes of all user-defined functions, explicit declaration of all
variables and functions (i.e., no assumed int types), proper
conversion casts in function returns (e.g., malloc()), and avoidance
of C++ keywords as user-defined entities.   It will then be possible
to compile it with C++ compilers too.  In every project where I've
done this, C++ compilations have turned up insidious errors that C
compilers on many different platforms, including non-UNIX ones like MS
DOS, DEC TOPS-20, and DEC VAX VMS, never caught, so the effort has
always been worthwhile.

The easiest way to check this is to compile the code under a strict
C++ compiler (g++ is by default NOT strict, although it has objects to
make it pickier).  The best one that I've found is SGI's CC.

As an experiment, I made a copy of the web2c-7.3.7 build tree on a
local SGI system, and tried compilation with "make CC=CC -i".  Here is
a sampling of the kind of errors reported:

CC -DHAVE_CONFIG_H  -I. -I. -I.. -I./..   -g  -c tie.c
cc-1312 CC: ERROR File = ../kpathsea/getopt.h, Line = 125
  More than one instance of overloaded function "getopt" has "C" linkage.

  extern KPSEDLL int getopt ();
                     ^
CC -g  -c ./alloca.c
cc-1020 CC: ERROR File = ./alloca.c, Line = 171
  The identifier "size" is undefined.

  alloca (size)
          ^

cc-1129 CC: ERROR File = ./alloca.c, Line = 172
  A left brace ("{") is expected at this point.

       unsigned size;
       ^

cc-1020 CC: ERROR File = ./alloca.c, Line = 198
  The identifier "free" is undefined.

          free ((pointer) hp);  /* Collect garbage.  */
          ^

cc-1020 CC: ERROR File = ./alloca.c, Line = 212
  The identifier "size" is undefined.

    if (size == 0)
        ^

cc-1136 CC: ERROR File = ./alloca.c, Line = 218
  Too many arguments in function call.

      register pointer newm = malloc (sizeof (header) + size);
                                      ^

cc-1020 CC: ERROR File = ./alloca.c, Line = 222
  The identifier "abort" is undefined.

        abort();
        ^

This is not horribly bad: Perhaps only a few hours, or a day or two,
would be needed to clean up the entire Web2C tree.  Tools like cproto
and gcc's protoize can be used to help automate this job.


-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- Center for Scientific Computing       FAX: +1 801 581 4148                  -
- University of Utah                    Internet e-mail: beebe@math.utah.edu  -
- Department of Mathematics, 110 LCB        beebe@acm.org  beebe@computer.org -
- 155 S 1400 E RM 233                       beebe@ieee.org                    -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe  -
-------------------------------------------------------------------------------