[infer] Fix clang frontend for switch statment

Summary:
This diff fixes the clang translation for switch statement.  It assumed that `default:` comes always
at last, which introduced some unreachable nodes inadvertently, e.g. when `default:` comes at first.

Reviewed By: dulmarod

Differential Revision: D19793138

fbshipit-source-id: 1e8b52c0d
master
Sungkeun Cho 5 years ago committed by Facebook Github Bot
parent a864823f38
commit 92e7aeeb3e

@ -1955,6 +1955,18 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
(* just return the [root_nodes] to be linked to the previous case's fallthrough *)
root_nodes
in
let switch_cases =
(* move the default case to the last in the list of cases, which is the first in
[switch_cases] since the list is reversed by default *)
let default, cases =
List.partition_tf switch_cases ~f:(function
| {SwitchCase.condition= Default} ->
true
| {SwitchCase.condition= Case _} ->
false )
in
default @ cases
in
let cases_root_nodes =
List.fold switch_cases ~init:trans_state.succ_nodes ~f:link_up_switch_cases
in

@ -262,7 +262,7 @@ digraph cfg {
"test_switch2.0717c55583f10f472ddb2d73d867e556_11" [label="11: Prune (false branch, switch) \n PRUNE(!(n$0 == 3), false); [line 53, column 5]\n " shape="invhouse"]
"test_switch2.0717c55583f10f472ddb2d73d867e556_11" -> "test_switch2.0717c55583f10f472ddb2d73d867e556_3" ;
"test_switch2.0717c55583f10f472ddb2d73d867e556_11" -> "test_switch2.0717c55583f10f472ddb2d73d867e556_7" ;
"test_switch2.0717c55583f10f472ddb2d73d867e556_12" [label="12: Prune (true branch, switch) \n PRUNE((n$0 == 2), true); [line 52, column 5]\n " shape="invhouse"]
@ -288,7 +288,8 @@ digraph cfg {
"test_switch2.0717c55583f10f472ddb2d73d867e556_17" [label="17: Prune (false branch, switch) \n PRUNE(!(n$0 == 0), false); [line 40, column 5]\n " shape="invhouse"]
"test_switch2.0717c55583f10f472ddb2d73d867e556_17" -> "test_switch2.0717c55583f10f472ddb2d73d867e556_7" ;
"test_switch2.0717c55583f10f472ddb2d73d867e556_17" -> "test_switch2.0717c55583f10f472ddb2d73d867e556_14" ;
"test_switch2.0717c55583f10f472ddb2d73d867e556_17" -> "test_switch2.0717c55583f10f472ddb2d73d867e556_15" ;
"test_switch2.0717c55583f10f472ddb2d73d867e556_18" [label="18: DeclStmt \n VARIABLE_DECLARED(value:int); [line 37, column 3]\n *&value:int=0 [line 37, column 3]\n " shape="box"]
@ -407,7 +408,7 @@ digraph cfg {
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_11" [label="11: Prune (false branch, switch) \n PRUNE(!(n$0 == 3), false); [line 94, column 5]\n " shape="invhouse"]
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_11" -> "test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_3" ;
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_11" -> "test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_7" ;
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_12" [label="12: Prune (true branch, switch) \n PRUNE((n$0 == 2), true); [line 93, column 5]\n " shape="invhouse"]
@ -433,7 +434,8 @@ digraph cfg {
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_17" [label="17: Prune (false branch, switch) \n PRUNE(!(n$0 == 0), false); [line 81, column 5]\n " shape="invhouse"]
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_17" -> "test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_7" ;
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_17" -> "test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_14" ;
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_17" -> "test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_15" ;
"test_switch4.70d4e6e8539e8d1ee3505d4562bc236d_18" [label="18: DeclStmt \n VARIABLE_DECLARED(value:int); [line 78, column 3]\n *&value:int=0 [line 78, column 3]\n " shape="box"]

@ -0,0 +1,59 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
void default_last(int i) {
switch (i) {
case 1:
i = 2;
break;
case 2:
i = 3;
break;
default:
i = 0;
break;
}
}
void default_first(int i) {
switch (i) {
default:
i = 0;
break;
case 1:
i = 2;
break;
case 2:
i = 3;
break;
}
}
void default_middle(int i) {
switch (i) {
case 1:
i = 2;
break;
default:
i = 0;
break;
case 2:
i = 3;
break;
}
}
void default_middle_no_break(int i) {
switch (i) {
case 1:
i = 2;
default:
i = 0;
case 2:
i = 3;
}
}

@ -0,0 +1,167 @@
/* @generated */
digraph cfg {
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_1" [label="1: Start default_first\nFormals: i:int\nLocals: \n " color=yellow style=filled]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_1" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_3" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_2" [label="2: Exit default_first \n " color=yellow style=filled]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_3" [label="3: SwitchStmt \n n$0=*&i:int [line 23, column 11]\n " shape="box"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_3" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_9" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_3" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_10" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_4" [label="4: BinaryOperatorStmt: Assign \n *&i:int=3 [line 31, column 7]\n " shape="box"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_4" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_2" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_5" [label="5: BinaryOperatorStmt: Assign \n *&i:int=2 [line 28, column 7]\n " shape="box"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_5" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_2" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_6" [label="6: BinaryOperatorStmt: Assign \n *&i:int=0 [line 25, column 7]\n " shape="box"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_6" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_2" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_7" [label="7: Prune (true branch, switch) \n PRUNE((n$0 == 2), true); [line 30, column 5]\n " shape="invhouse"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_7" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_4" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_8" [label="8: Prune (false branch, switch) \n PRUNE(!(n$0 == 2), false); [line 30, column 5]\n " shape="invhouse"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_8" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_6" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_9" [label="9: Prune (true branch, switch) \n PRUNE((n$0 == 1), true); [line 27, column 5]\n " shape="invhouse"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_9" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_5" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_10" [label="10: Prune (false branch, switch) \n PRUNE(!(n$0 == 1), false); [line 27, column 5]\n " shape="invhouse"]
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_10" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_7" ;
"default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_10" -> "default_first#6846905812772168043.4ef6682b69256b2600b845667cf73cc5_8" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_1" [label="1: Start default_last\nFormals: i:int\nLocals: \n " color=yellow style=filled]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_1" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_3" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_2" [label="2: Exit default_last \n " color=yellow style=filled]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_3" [label="3: SwitchStmt \n n$0=*&i:int [line 9, column 11]\n " shape="box"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_3" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_9" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_3" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_10" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_4" [label="4: BinaryOperatorStmt: Assign \n *&i:int=0 [line 17, column 7]\n " shape="box"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_4" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_2" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_5" [label="5: BinaryOperatorStmt: Assign \n *&i:int=3 [line 14, column 7]\n " shape="box"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_5" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_2" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_6" [label="6: BinaryOperatorStmt: Assign \n *&i:int=2 [line 11, column 7]\n " shape="box"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_6" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_2" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_7" [label="7: Prune (true branch, switch) \n PRUNE((n$0 == 2), true); [line 13, column 5]\n " shape="invhouse"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_7" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_5" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_8" [label="8: Prune (false branch, switch) \n PRUNE(!(n$0 == 2), false); [line 13, column 5]\n " shape="invhouse"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_8" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_4" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_9" [label="9: Prune (true branch, switch) \n PRUNE((n$0 == 1), true); [line 10, column 5]\n " shape="invhouse"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_9" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_6" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_10" [label="10: Prune (false branch, switch) \n PRUNE(!(n$0 == 1), false); [line 10, column 5]\n " shape="invhouse"]
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_10" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_7" ;
"default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_10" -> "default_last#16530854322231458976.ca2a177e8abc1894ef39d12e3b93b5ce_8" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_1" [label="1: Start default_middle\nFormals: i:int\nLocals: \n " color=yellow style=filled]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_1" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_3" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_2" [label="2: Exit default_middle \n " color=yellow style=filled]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_3" [label="3: SwitchStmt \n n$0=*&i:int [line 37, column 11]\n " shape="box"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_3" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_9" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_3" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_10" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_4" [label="4: BinaryOperatorStmt: Assign \n *&i:int=3 [line 45, column 7]\n " shape="box"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_4" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_2" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_5" [label="5: BinaryOperatorStmt: Assign \n *&i:int=0 [line 42, column 7]\n " shape="box"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_5" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_2" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_6" [label="6: BinaryOperatorStmt: Assign \n *&i:int=2 [line 39, column 7]\n " shape="box"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_6" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_2" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_7" [label="7: Prune (true branch, switch) \n PRUNE((n$0 == 2), true); [line 44, column 5]\n " shape="invhouse"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_7" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_4" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_8" [label="8: Prune (false branch, switch) \n PRUNE(!(n$0 == 2), false); [line 44, column 5]\n " shape="invhouse"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_8" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_5" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_9" [label="9: Prune (true branch, switch) \n PRUNE((n$0 == 1), true); [line 38, column 5]\n " shape="invhouse"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_9" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_6" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_10" [label="10: Prune (false branch, switch) \n PRUNE(!(n$0 == 1), false); [line 38, column 5]\n " shape="invhouse"]
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_10" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_7" ;
"default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_10" -> "default_middle#6704148577752077233.063f27b3bbfd847c375c1fb990fa5aff_8" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_1" [label="1: Start default_middle_no_break\nFormals: i:int\nLocals: \n " color=yellow style=filled]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_1" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_3" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_2" [label="2: Exit default_middle_no_break \n " color=yellow style=filled]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_3" [label="3: SwitchStmt \n n$0=*&i:int [line 51, column 11]\n " shape="box"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_3" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_9" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_3" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_10" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_4" [label="4: BinaryOperatorStmt: Assign \n *&i:int=3 [line 57, column 7]\n " shape="box"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_4" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_2" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_5" [label="5: BinaryOperatorStmt: Assign \n *&i:int=0 [line 55, column 7]\n " shape="box"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_5" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_4" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_6" [label="6: BinaryOperatorStmt: Assign \n *&i:int=2 [line 53, column 7]\n " shape="box"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_6" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_5" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_7" [label="7: Prune (true branch, switch) \n PRUNE((n$0 == 2), true); [line 56, column 5]\n " shape="invhouse"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_7" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_4" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_8" [label="8: Prune (false branch, switch) \n PRUNE(!(n$0 == 2), false); [line 56, column 5]\n " shape="invhouse"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_8" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_5" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_9" [label="9: Prune (true branch, switch) \n PRUNE((n$0 == 1), true); [line 52, column 5]\n " shape="invhouse"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_9" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_6" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_10" [label="10: Prune (false branch, switch) \n PRUNE(!(n$0 == 1), false); [line 52, column 5]\n " shape="invhouse"]
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_10" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_7" ;
"default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_10" -> "default_middle_no_break#4834754287745914325.bb66b5ddc891f61f192c15c288bc776c_8" ;
}
Loading…
Cancel
Save