[comp.lang.c] superfluous semicolons

ado@elsie.UUCP (Arthur David Olson) (06/29/89)

> Last week I spent an entire day tracking down one of those bugs you just
> don't see after staring at your code too long.  It was of the form:
> 	if (condition);
> 	    action;

For those with source, here are the changes to the 4.3BSD versions of
"cgram.y" and "lint.c" that catch both bodyless if's and bodyless else's.
One other challenge is dealt with here--code such as
			#include <math.h>

			double;
			function(x)
			{
				return fabs(x - 1);
			}
which misbehaves because of the incorrect semicolon.
-- 
	The RCMP is to spaceflight as the Canadian government is to freedom.
	Arthur David Olson    ado@alw.nih.gov    ADO is a trademark of Ampex.

*** 3.1/cgram.y	Thu Jun 29 10:41:33 1989
--- 3.2/cgram.y	Thu Jun 29 10:41:34 1989
***************
*** 52,57 ****
--- 57,67 ----
  
  %{
  	static int fake = 0;
+ #ifndef ASWAS
+ #ifdef LINT
+ 	extern char *	gripenull;
+ #endif /* LINT */
+ #endif /* !ASWAS */
  #ifndef FLEXNAMES
  	static char fakename[NCHNAM+1];
  #else
***************
*** 70,76 ****
  		;
  data_def:
  		   oattributes  SM
! 			={  $1->in.op = FREE; }
  		|  oattributes init_dcl_list  SM
  			={  $1->in.op = FREE; }
  		|  oattributes fdeclarator {
--- 80,99 ----
  		;
  data_def:
  		   oattributes  SM
! 			={
! #ifndef ASWAS
! 			    extern int	asm_esc;
! 
! 			    if ($1->in.type != STRTY &&
! 				$1->in.type != UNIONTY &&
! 				$1->in.type != ENUMTY &&
! 				(!asm_esc || curclass != SNULL ||
! 				$1->in.type != INT ||
! 				$1->fn.cdim != 0 ||
! 				$1->fn.csiz != INT))
! 					werror("null effect");
! #endif /* !ASWAS */
! 			$1->in.op = FREE; }
  		|  oattributes init_dcl_list  SM
  			={  $1->in.op = FREE; }
  		|  oattributes fdeclarator {
***************
*** 401,406 ****
--- 424,434 ----
  			    if( psavbc > &asavbc[BCSZ-2] ) cerror( "nesting too deep" );
  			    *psavbc++ = regvar;
  			    *psavbc++ = autooff;
+ #ifndef ASWAS
+ #ifdef LINT
+ 			    gripenull = 0;
+ #endif /* LINT */
+ #endif /* !ASWAS */
  			    }
  		;
  
***************
*** 409,414 ****
--- 437,447 ----
  		|  compoundstmt
  		|  ifprefix statement
  			={ deflab($1);
+ #ifndef ASWAS
+ #ifdef LINT
+ 			   gripenull = 0;
+ #endif /* LINT */
+ #endif /* !ASWAS */
  			   reached = 1;
  			   }
  		|  ifelprefix statement
***************
*** 416,421 ****
--- 449,459 ----
  				deflab( $1 );
  				reached = 1;
  				}
+ #ifndef ASWAS
+ #ifdef LINT
+ 			    gripenull = 0;
+ #endif /* LINT */
+ #endif /* !ASWAS */
  			    }
  		|  whprefix statement
  			={  branch(  contlab );
***************
*** 497,502 ****
--- 535,548 ----
  			    goto rch;
  			    }
  		|   SM
+ /* #ifndef ASWAS */
+ 			={
+ #ifdef LINT
+ 			    if (hflag && gripenull != 0)
+ 				werror(gripenull);
+ #endif
+ 			 }
+ /* #endif !ASWAS */
  		|  error  SM
  		|  error RC
  		|  label statement
***************
*** 530,535 ****
--- 576,586 ----
  ifprefix:	IF LP e RP
  			={  ecomp( buildtree( CBRANCH, $3, bcon( $$=getlab()) ) ) ;
  			    reached = 1;
+ #ifndef ASWAS
+ #ifdef LINT
+ 			    gripenull = "bodyless if";
+ #endif /* !LINT */
+ #endif /* !ASWAS */
  			    }
  		;
  ifelprefix:	  ifprefix statement ELSE
***************
*** 537,542 ****
--- 588,598 ----
  			    else $$ = NOLAB;
  			    deflab( $1 );
  			    reached = 1;
+ #ifndef ASWAS
+ #ifdef LINT
+ 			    gripenull = "bodyless else";
+ #endif /* LINT */
+ #endif /* !ASWAS */
  			    }
  		;
  
*** 3.1/lint.c	Thu Jun 29 10:54:59 1989
--- 3.1.1.1/lint.c	Thu Jun 29 10:55:00 1989
***************
*** 95,100 ****
--- 95,103 ----
  		}
  	}
  
+ #ifndef ASWAS
+ char *	gripenull;
+ #endif /* !defined ASWAS */
  ecode( p ) NODE *p; {
  	/* compile code for p */
  
***************
*** 101,106 ****
--- 104,112 ----
  	fwalk( p, contx, EFF );
  	lnp = lnames;
  	lprt( p, EFF, 0 );
+ #ifndef ASWAS
+ 	gripenull = 0;
+ #endif /* !defined ASWAS */
  	}
  
  ejobcode( flag ){