From 84549f691d7c9c79229ddf992cdcbcbdcdfa4459 Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Wed, 9 Dec 2015 03:53:19 -0800 Subject: [PATCH] Support decl_stmt in switch stmt Summary: public Add support for decl statements inside condition of switch statement Reviewed By: ddino Differential Revision: D2734213 fb-gh-sync-id: d60021d --- infer/src/clang/cTrans.ml | 5 +- .../var_decl_inside_switch.cpp | 20 +++++++ .../var_decl_inside_switch.cpp.dot | 60 +++++++++++++++++++ .../frontend/cpp/NestedCPPOperatorsTest.java | 5 ++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp create mode 100644 infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp.dot diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index e1837ced3..a384fb1dc 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -1115,7 +1115,7 @@ struct let sil_loc = CLocation.get_sil_location stmt_info pln context in let open Clang_ast_t in match switch_stmt_list with - | [_; cond; CompoundStmt(stmt_info, stmt_list)] -> + | [decl_stmt; cond; CompoundStmt(stmt_info, stmt_list)] -> let trans_state_pri = PriorityNode.try_claim_priority_node trans_state stmt_info in let trans_state' ={ trans_state_pri with succ_nodes = []} in let res_trans_cond_tmp = instruction trans_state' cond in @@ -1133,6 +1133,7 @@ struct root_nodes = root_nodes; leaf_nodes = [switch_special_cond_node] } in + let res_trans_decl = declStmt_in_condition_trans trans_state decl_stmt res_trans_cond in let trans_state_no_pri = if PriorityNode.own_priority_node trans_state_pri.priority stmt_info then { trans_state_pri with priority = Free } else trans_state_pri in @@ -1226,7 +1227,7 @@ struct let top_entry_point, top_prune_nodes = translate_and_connect_cases list_of_cases succ_nodes succ_nodes in let _ = connected_instruction (IList.rev pre_case_stmts) top_entry_point in Cfg.Node.set_succs_exn switch_special_cond_node top_prune_nodes []; - let top_nodes = res_trans_cond.root_nodes in + let top_nodes = res_trans_decl.root_nodes in IList.iter (fun n' -> Cfg.Node.append_instrs_temps n' [] res_trans_cond.ids) succ_nodes; (* succ_nodes will remove the temps *) { root_nodes = top_nodes; leaf_nodes = succ_nodes; ids = []; instrs = []; exps =[]} | _ -> assert false diff --git a/infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp b/infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp new file mode 100644 index 000000000..45a23dd9c --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp @@ -0,0 +1,20 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +int get(int a) { + switch (int x = a) { + case 0: + case 1: + return 0; + case 2: + return 1; + default: + return x; + } +} diff --git a/infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp.dot new file mode 100644 index 000000000..df3ffaad8 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/nestedoperators/var_decl_inside_switch.cpp.dot @@ -0,0 +1,60 @@ +digraph iCFG { +14 [label="14: Prune (false branch) \n PRUNE(((n$0 == 0) == 0), false); [line 12]\n " shape="invhouse"] + + + 14 -> 11 ; + 14 -> 12 ; +13 [label="13: Prune (true branch) \n PRUNE(((n$0 == 0) != 0), true); [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="invhouse"] + + + 13 -> 10 ; +12 [label="12: Prune (false branch) \n PRUNE(((n$0 == 1) == 0), false); [line 13]\n " shape="invhouse"] + + + 12 -> 8 ; + 12 -> 9 ; +11 [label="11: Prune (true branch) \n PRUNE(((n$0 == 1) != 0), true); [line 13]\n APPLY_ABSTRACTION; [line 13]\n " shape="invhouse"] + + + 11 -> 10 ; +10 [label="10: Return Stmt \n NULLIFY(&x,false); [line 14]\n *&return:int =0 [line 14]\n APPLY_ABSTRACTION; [line 14]\n " shape="box"] + + + 10 -> 2 ; +9 [label="9: Prune (false branch) \n PRUNE(((n$0 == 2) == 0), false); [line 15]\n " shape="invhouse"] + + + 9 -> 5 ; +8 [label="8: Prune (true branch) \n PRUNE(((n$0 == 2) != 0), true); [line 15]\n " shape="invhouse"] + + + 8 -> 7 ; +7 [label="7: Return Stmt \n NULLIFY(&x,false); [line 16]\n *&return:int =1 [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"] + + + 7 -> 2 ; +6 [label="6: Return Stmt \n n$2=*&x:int [line 18]\n *&return:int =n$2 [line 18]\n REMOVE_TEMPS(n$2); [line 18]\n NULLIFY(&x,false); [line 18]\n APPLY_ABSTRACTION; [line 18]\n " shape="box"] + + + 6 -> 2 ; +5 [label="5: DefaultStmt_placeholder \n " shape="box"] + + + 5 -> 6 ; +4 [label="4: DeclStmt \n n$1=*&a:int [line 11]\n *&x:int =n$1 [line 11]\n REMOVE_TEMPS(n$1); [line 11]\n NULLIFY(&a,false); [line 11]\n " shape="box"] + + + 4 -> 3 ; +3 [label="3: Switch_stmt \n n$0=*&x:int [line 11]\n " shape="box"] + + + 3 -> 13 ; + 3 -> 14 ; +2 [label="2: Exit get \n REMOVE_TEMPS(n$0); [line 20]\n " color=yellow style=filled] + + +1 [label="1: Start get\nFormals: a:int \nLocals: x:int \n DECLARE_LOCALS(&return,&x); [line 10]\n NULLIFY(&x,false); [line 10]\n " color=yellow style=filled] + + + 1 -> 4 ; +} diff --git a/infer/tests/frontend/cpp/NestedCPPOperatorsTest.java b/infer/tests/frontend/cpp/NestedCPPOperatorsTest.java index 3e1dda998..99cee0a16 100644 --- a/infer/tests/frontend/cpp/NestedCPPOperatorsTest.java +++ b/infer/tests/frontend/cpp/NestedCPPOperatorsTest.java @@ -48,4 +48,9 @@ public class NestedCPPOperatorsTest { frontendTest("var_decl_inside_for.cpp"); } + @Test + public void testVarDeclInsideSwitchDotFilesMatch() + throws InterruptedException, IOException, InferException { + frontendTest("var_decl_inside_switch.cpp"); + } }