Here are some patches concerning CIL's transformation of switch statements.

One patch, switch_default_ordering.patch, fixes an actual bug (although I suppose it never comes up in practice, given that it hasn't been found already) in CIL's transformation of switch statements, due to assumptions on the order of 'case' and 'default' labels. The patch includes a test case that exhibits the bug.

I'm guessing this bug hasn't been found because no one ever puts 'case' labels on the same block as a 'default' label, because that wouldn't do anything. Despite the fact that this apparently never arises in practice, my sense of cleanliness urged me to want to remove 'case' labels that occur alongside a 'default' label. This is remove_case_when_default.patch . Doing this also has the benefit of removing unnecessary 'if' statements from the transformed switch statements.

I noticed that this only removed 'case' labels *before* a 'default', but not after a 'default'. It turns out there's a strange quirk in the parser that makes it so that 'default' labels are always followed by empty statements. This isn't quite a bug (because the empty statement just falls through to the next statement), but it means that the parser accepts
  switch (x) { default: }
while I think is not valid C. Also, fixing this makes the previous patch more effective, so I made this (trivial) change in default_parse.patch .

Finally, since I was playing around with switch statements anyway, I did one further thing. You may have seen my previous posts about wanting to retain '&&'s and '||'s wherever possible. Well, that is possible for switch statements if there are multiple 'case' labels on a single block. The patch make_switch_cases_ors.patch makes it so that
  case 0: case 1: ...
becomes
  if (x == 0 || x == 1) { goto ...; } else ...
instead of
  if (x == 0) { goto ...; } else if (x == 1) { goto ...; } else ...
And, while I was making that change, I made one other change: I removed the 'if (0)' that gets inserted in the very center of the switch statement, as long as there is a 'default' label in the switch statement. (This 'if (0)' is needed if and only if there is no 'default' label.)

I think the first three patches are independent of one another, but the last one, make_switch_cases_ors, depends on the second one, remove_case_when_default.

I hope someone out there appreciates my delving into this apparently trivial matter and cleaning things up a bit.

Elnatan

Attachment: switch_default_ordering.patch
Description: Binary data

Attachment: remove_case_when_default.patch
Description: Binary data

Attachment: default_parse.patch
Description: Binary data

Attachment: make_switch_cases_ors.patch
Description: Binary data

------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
CIL-users mailing list
CIL-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cil-users

Reply via email to