[comp.lang.c] Survey of "duplicate case in switch" errors

chase@orc.olivetti.com (01/21/89)

For the following file (test.c):
----------------
foo(i,j,k)   int i,j,k;
{
  int r = 0;
  switch (i)
    {
    case 1: r = i;
    case 2: switch (j)
	{
	case 3: r = j;
	case 4: switch (k)
	    {
	    case 5: break;
	    case 5: r = k; /* line 13, for reference */
	    case 6: break;
	    }
	}
    }
  return r;
}
----------------
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.)

David

dbh@hpesrgd.HP.COM (Dave Hollenbeck) (01/24/89)

HP-UX version 2.0 on a 9000/825 says this:

cc: "test.c", line 13: error 1526: Duplicate case constant (5) in switch.

Looks right to me, although sometimes I wish I *could* have duplicates.

jeff@unh.UUCP (Jeffrey E. F. Friedl) (01/25/89)

In article <36376@oliveb.olivetti.com>, chase@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

frank@zen.co.uk (Frank Wales) (01/25/89)

In article <36376@oliveb.olivetti.com> chase@orc.olivetti.com () writes:
>For the following file (test.c):
>----------------
>foo(i,j,k)   int i,j,k;
>{
>[...]
>	case 4: switch (k)
>	    {
>	    case 5: break;
>	    case 5: r = k; /* line 13, for reference */
>	    case 6: break;
>	    }
>[...]
>}
>----------------
>I get the following diagnostics from four different compilers:
>[...]
>I am entertained -- error reporting by C compilers is often highly
>speculative, but this was extra fun.  Anyone care to add to my
>collection? 

HP's contribution to the fray...

HP-9000/350SRX, HP-UX 6.2 (pcc-derived, I think) gives:
"test.c", line 16: duplicate case in switch, 5
"test.c", line 17: duplicate case in switch, 5
"test.c", line 18: duplicate case in switch, 0

This is identical to SunOS 3.4; interesting, huh?  :-)

HP-9000/840, HP-UX 2.1 (HP-PA, written entirely by HP):
cc: "test.c", line 13: error 1526: Duplicate case constant (5) in switch.

which is what I'd expect from the Precision Architecture compilers (this
is the C compiler that never says "syntax error").

>(Any vendors out there care to do it right?  GCC is
>batting .500, including reporting the real live offending line
>number.)

Ditto HP.  Has anyone tried feeding this to lint?  On series 800, where
the lint is still pcc-derived, you get the wrong triple error message as
reported above, just in case you thought you knew what was going on.
--
Frank Wales, Systems Manager,        [frank@zen.co.uk<->mcvax!zen.co.uk!frank]
Zengrange Ltd., Greenfield Rd., Leeds, ENGLAND, LS9 8DB. (+44) 532 489048 x217 

bet@dukeac.UUCP (Bennett Todd) (02/08/89)

>HP-UX version 2.0 on a 9000/825 says this:
>
>cc: "test.c", line 13: error 1526: Duplicate case constant (5) in switch.
>
>Looks right to me, although sometimes I wish I *could* have duplicates.


Me too! What a great idea, duplicate case labels. It would make programming
non-deterministic finite state machines a piece of cake! Maybe we could get
X3J11 to add it in:-).

-Bennett