[cfe-dev] SwitchStmt bug?

Per Viberg Per.Viberg at evidente.se
Mon Mar 10 10:13:08 PDT 2014


thanks guys for the quick reply,

yeah, I suspected something like that. The only thing I was surprised about was that there is a Stmt at all inside a switch "label". I mean, if they are just like goto labels, then no statement at all would seem more intuitive.

.......................................................................................................................
Per Viberg Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden
Phone:    +46 (0)8 402 79 00
Mobile:    +46 (0)70 912 42 52
E-mail:     Per.Viberg at evidente.se<mailto:Per.Viberg at evidente.se>

www.evidente.se<http://www.evidente.se>
This e-mail, which might contain confidential information, is addressed to the above stated person/company. If you are not the correct addressee, employee or in any other way the person concerned, please notify the sender immediately. At the same time, please delete this e-mail and destroy any prints. Thank You.
________________________________
Från: Jordan Rose [jordan_rose at apple.com]
Skickat: den 10 mars 2014 17:20
Till: Per Viberg
Cc: cfe-dev Developers; Tom Honermann
Ämne: Re: [cfe-dev] SwitchStmt bug?


On Mar 10, 2014, at 7:49 , Tom Honermann <thonermann at coverity.com<mailto:thonermann at coverity.com>> wrote:

On 03/10/2014 10:23 AM, Per Viberg wrote:

Hi all,

I was inspecting the AST dump of this switch:

void test_switch(int t) {
  switch(t)
  {
  case 1:
    y = 11;
  case 2:
    y = 9;
    x = 12;
    break;
  default:
    break;
  }
}

and I got something I didn't expect. The second CaseStmt does only
contain the first statement that is under it's label. The "x=12;" gets
its own Stmt outside the CaseStmt but inside the CompoundStmt inside the
SwitchStmt. I would anticipate that all statements under a label either
are: inside the CaseStmt or: outside but inside the CompoundStmt. Any
reason for this?.

This is expected.  Case statements have just one sub statement.

More generally, case statements are just labels, and have the same basic rules as regular old goto labels. Having case statements "just" be labels is what allows things like Duff's Device<http://en.wikipedia.org/wiki/Duff's_device> in C:

switch(count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while(--n > 0);
}

This is the opposite situation, really, because here we need cases inside the loop that's inside the switch. But once you're supporting this, the representation as a simple label is just easier to work with.

Jordan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140310/c206361f/attachment.html>


More information about the cfe-dev mailing list