[cfe-dev] SwitchStmt bug?

Jordan Rose jordan_rose at apple.com
Mon Mar 10 09:20:25 PDT 2014


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 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/38648fc8/attachment.html>


More information about the cfe-dev mailing list