Newsgroups: comp.os.minix
Subject: Compiler Bug (NOT)
Organization: Syracuse University, Syracuse
From: mcconnel@hydra.syr.edu (Terry R. McConnell)
NNTP-Posting-Host: hydra-1.syr.edu
Message-ID: <369cff5c.0@news.syr.edu>
Date: 13 Jan 1999 15:17:32 -0500
X-Trace: 13 Jan 1999 15:17:32 -0500, hydra-1.syr.edu
Lines: 54
Path: news.adfa.oz.au!clarion.carno.net.au!news0.optus.net.au!news1.optus.net.au!optus!news1.mpx.com.au!nsw.nntp.telstra.net!intgwlon.nntp.telstra.net!newsfeed.mathworks.com!nntprelay.mathworks.com!woodstock.news.demon.net!demon!news.maxwell.syr.edu!news.syr.edu!hydra.syr.edu!mcconnel

I recently posted an article in which I speculated that a problem
encountered while porting the latest C-Kermit to Minix was due to a compiler
bug. 

It wasn't, but I think the explanation is instructive.

After posting, I looked at some of the kermit header files and found 
the following:

#ifdef MINIX
#ifdef putchar
#undef putchar
#endif /* putchar */
#define putchar(c) {putc(c,stdout);fflush(stdout);} 
#endif /* MINIX */

Apparently, whoever wrote this code surrounded the macro with brackets in
an attempt to handle the case that putchar occurs after an if, else, or
other construct that is expecting a block to follow. Unfortunately, it
doesn't work. There is a nice discussion of the underlying difficulty in
section 6.3 of C Traps and Pitfalls, by Andrew Koenig (1989, AT&T Bell
Laboratories.) Koenig says, "It is tempting, but surprisingly difficult,
to define macros that act like statements."

The following example illustrates the problem:

#include<stdlib.h>

#define inc2(x) {x++;x++;}  /* wrong */

int
main(void)
{
	int i=0;

	if(1)
		inc2(i);
	else
	        i= i+2;
	return i;
}

Here, inc2(i) expands to {i++;i++};. The extra semicolon is a (do-nothing)
statement that causes the else to be left dangling. One way to patch this
is do something like this (there are many other possibilities):

#define inc2(x) (x++ == x++)  

Consult Koenig's book for further discussion.
-- 
************************************************************************
Terry R. McConnell   Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150
trmcconn@syr.edu                            http://barnyard.syr.edu/~tmc 
************************************************************************
