[cfe-dev] SwitchStmt bug?

Tom Honermann thonermann at coverity.com
Mon Mar 10 07:49:59 PDT 2014


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.

The statements can be grouped with the case statement through the use of 
a compound statement:

$ cat t.c
int x;
int y;
void test_switch(int t) {
    switch(t)
    {
    case 1:
      y = 11;
    case 2: {   // Start compound statement
      y = 9;
      x = 12;
      break;
    }           // End compound statement
    default:
      break;
    }
}

$ clang -Xclang -ast-dump -c t.c
...
     `-SwitchStmt 0x5a03748 <line:4:4, line:15:4>
       |-<<<NULL>>>
       |-ImplicitCastExpr 0x5a03730 <line:4:11> 'int' <LValueToRValue>
       | `-DeclRefExpr 0x5a03708 <col:11> 'int' lvalue ParmVar 0x5a035a0 
't' 'int'
       `-CompoundStmt 0x5a4ab58 <line:5:4, line:15:4>
         |-CaseStmt 0x5a03798 <line:6:4, line:7:10>
         | |-IntegerLiteral 0x5a03778 <line:6:9> 'int' 1
         | |-<<<NULL>>>
         | `-BinaryOperator 0x5a03818 <line:7:6, col:10> 'int' '='
         |   |-DeclRefExpr 0x5a037d0 <col:6> 'int' lvalue Var 0x5a03530 
'y' 'int'
         |   `-IntegerLiteral 0x5a037f8 <col:10> 'int' 11
         |-CaseStmt 0x5a03860 <line:8:4, line:12:4>
         | |-IntegerLiteral 0x5a03840 <line:8:9> 'int' 2
         | |-<<<NULL>>>
         | `-CompoundStmt 0x5a4aaf8 <col:12, line:12:4>
         |   |-BinaryOperator 0x5a4aa50 <line:9:6, col:10> 'int' '='
         |   | |-DeclRefExpr 0x5a03898 <col:6> 'int' lvalue Var 
0x5a03530 'y' 'int'
         |   | `-IntegerLiteral 0x5a038c0 <col:10> 'int' 9
         |   |-BinaryOperator 0x5a4aac0 <line:10:6, col:10> 'int' '='
         |   | |-DeclRefExpr 0x5a4aa78 <col:6> 'int' lvalue Var 
0x5a034c0 'x' 'int'
         |   | `-IntegerLiteral 0x5a4aaa0 <col:10> 'int' 12
         |   `-BreakStmt 0x5a4aae8 <line:11:6>
         `-DefaultStmt 0x5a4ab38 <line:13:4, line:14:6>
           `-BreakStmt 0x5a4ab28 <col:6>

Tom.



More information about the cfe-dev mailing list