[cc65] Problems with va_copy

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2005-12-11 14:07:49
To work around problems with some compilers having snprintf and others not
having it, I've added my own implementation some time ago. To do this, I
needed a facility to copy a va_list, so I added a va_copy.h file which
includes this facility for compilers that don't have it.

va_copy is C99, which lead to a portability problem: C99 compilers have
va_copy, and user programs are not allowed to define such an identifier. C89
compilers do not have va_copy, and user programs are allowed to have such an
identifier. Unfortunately, many compilers do not really state which type they
are. In addition to that, distributors have often their own idea, what the
compiler should support and what not, so for example there are several older
versions of gcc in the wild that support va_copy. As a result, I got more and
more request to add #ifs for this and that operating system/compiler version.

It is my philosophy, that if you have to add too many #ifdefs to a file,
you're doing something wrong. Portable code does not need #ifdefs (this is
contrary to popular belief, which says the more #ifdefs you have, the more
portable your code is).

To solve the problem, I will try something different and resort to standard
compliance: C99 compilers are required to define the __STDC_VERSION__ symbol
to 199901, while C89 compilers do not have such a symbol (they would
theoretically be allowed to define it, but this is highly unlikely). This
means that, in the former case, the compiler does already have va_copy, while
in the latter, it is not allowed to define such a symbol, so I can use this
name for my own version. This leaves the problem to find a working
implementation for va_copy (src = dest does not always work), but it removes
the problem of knowing if va_copy clashes with something, the compiler already
has.

The advantage of this approach is that the problem is now the problem of the
compiler. If it claims to be C99 conformant, it must have va_copy, if it
doesn't claim to be a C99 compiler it is not allowed to have va_copy. If
neither is true, the compiler is broken.

In the case of GNU C, the compiler does not claim to adhere to the C89 or C99
standard, if you don't force it to do so. So I've added -std=c89 to the
makefiles. This required minor changes in three places where fileno() was
used, which is no longer available in C89 mode (because it comes from
stdio.h).

If you're compiling cc65 yourself, I would request that to test this change
and tell me if it works. Older versions of gcc may need to have the -std=c89
command line parameter changed to -ansi or something similar. Apart from this,
it should at least compile. Otherwise, the compiler has a problem with C
standard compliance.

I'm not sure if this is really the perfect solution, but the old one seemed to
be even worse.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sun Dec 11 14:08:00 2005

This archive was generated by hypermail 2.1.8 : 2005-12-11 14:08:02 CET