[cfe-dev] SwitchStmt bug?

Richard Smith richard at metafoo.co.uk
Mon Mar 10 10:24:52 PDT 2014


On Mon, Mar 10, 2014 at 10:13 AM, Per Viberg <Per.Viberg at evidente.se> wrote:

>  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.
>

Consider this:

switch (x) {
  if (a)
    case 1: b;
  c;
}

An 'if' statement can only have one substatement, so the case label *must*
have 'b' as its child or we'd have no way to represent this code. And the
case label must *not* have 'c' as its child, or we'd be representing this
incorrectly.

Also, the relevant language standards say that labels have a single
statement as a child, and we try to make our representations faithful to
the relevant standards as far as is reasonably possible.


>
> .......................................................................................................................
> 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
>
> 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> 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
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140310/24d4afec/attachment.html>


More information about the cfe-dev mailing list