Survey of "duplicate case in switch" errors

Jeffrey E. F. Friedl jeff at unh.UUCP
Wed Jan 25 07:17:48 AEST 1989


In article <36376 at oliveb.olivetti.com>, chase at orc.olivetti.com 
describes odd error messages from the compilation of a small
program with one duplicate switch nested inside a non-errored switch.
> I get the following diagnostics from four different compilers:
> 
> SunOS 3.4, cc:
> "test.c", line 15: duplicate case in switch, 5
> "test.c", line 16: duplicate case in switch, 5
> "test.c", line 17: duplicate case in switch, 0
> 
> gcc 1.29:
> test.c: In function foo:
> test.c:13: duplicate case value
> 
> gcc 1.32: (with usual caveats about user installation)
> (nothing!)
> 
> SunOS 4.0, cc:
> "test.c", line 15: duplicate case in switch, 5
> "test.c", line 16: duplicate case in switch, 5
> "test.c", line 17: duplicate case in switch, 5
> 
> I am entertained -- error reporting by C compilers is often highly
> speculative, but this was extra fun.  Anyone care to add to my
> collection?  (Any vendors out there care to do it right?  GCC is
> batting .500, including reporting the real live offending line
> number.)
> 

A compiler that I'm working on does the same thing as the Sun's above.
My compiler, and I suppose the Sun compilers, are pcc descendents.
The problem that in the function in which the "duplicate..." error
is reported, they return without having "popped" the switch off the
switch-stack.

I fixed mine this way (your filenames/function names may vary):
In cgram.y, function swend(), near the end is a sequence much like:

	if (there is a duplicate case) {
		report_error("duplicate case in switch, %d", value);
		return;
	}

	dosomethinghere();
	swp = swbeg-1;	/* this "pops" this switch from the switch-stack */
    } /* end of function */
	
Just copy the "swp=swbeg-1" line to after the error call.  All will be happy.

As far as reporting the correct line number, pcc just didn't bother.
It seems to be an easy thing to do (add an appropriate field to the sw struct)
and, hey, I think I'll do just that....

	*jeff*

------------------------------------------------------------------------------
Jeffrey Eric Francis Friedl                             Box 2173 Babcock House
..!{uunet,decvax}!unh!jeff                          Durham New Hampshire 03824

I hope I'm not around Jan 18, 2038 at 10:14:08PM



More information about the Comp.lang.c mailing list