Skip to content

Commit

Permalink
[clang] Fix expression statement with conditional inside
Browse files Browse the repository at this point in the history
Summary: CFG nodes were not connected and some instructions ended up in wrong place. Fix those issues

Reviewed By: dulmarod

Differential Revision: D5406720

fbshipit-source-id: 2a70e1a
  • Loading branch information
Andrzej Kotulski authored and facebook-github-bot committed Jul 13, 2017
1 parent 36b6ab1 commit e366b0d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 15 deletions.
5 changes: 3 additions & 2 deletions infer/src/clang/cTrans.ml
Expand Up @@ -1659,7 +1659,8 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
let stmt =
extract_stmt_from_singleton stmt_list "ERROR: StmtExpr should have only one statement.@\n"
in
let res_trans_stmt = instruction trans_state stmt in
let trans_state' = {trans_state with priority= Free} in
let res_trans_stmt = instruction trans_state' stmt in
let exps' = List.rev res_trans_stmt.exps in
match exps' with
| last_exp :: _
Expand Down Expand Up @@ -3009,7 +3010,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
let res_trans_tail = exec_trans_instrs_no_rev trans_state' trans_stmt_fun_list' in
{ empty_res_trans with
root_nodes= res_trans_tail.root_nodes
; leaf_nodes= []
; leaf_nodes= res_trans_s.leaf_nodes
; instrs= res_trans_tail.instrs @ res_trans_s.instrs
; exps= res_trans_tail.exps @ res_trans_s.exps
; initd_exps= res_trans_tail.initd_exps @ res_trans_s.initd_exps }
Expand Down
Expand Up @@ -24,3 +24,10 @@ int test(int* p) {
x + y;
});
}

int with_conditional(int* p) {
return ({
int x = 1;
p ? *p + x : x;
});
}
74 changes: 67 additions & 7 deletions infer/tests/codetoanalyze/c/frontend/nestedoperators/gnuexpr.c.dot
Expand Up @@ -3,31 +3,91 @@ digraph iCFG {
"main.fad58de7366495db4650cfefac2fcd61_1" [label="1: Start main\nFormals: \nLocals: X:int y:int \n DECLARE_LOCALS(&return,&X,&y); [line 10]\n " color=yellow style=filled]


"main.fad58de7366495db4650cfefac2fcd61_1" -> "main.fad58de7366495db4650cfefac2fcd61_5" ;
"main.fad58de7366495db4650cfefac2fcd61_1" -> "main.fad58de7366495db4650cfefac2fcd61_7" ;
"main.fad58de7366495db4650cfefac2fcd61_2" [label="2: Exit main \n " color=yellow style=filled]


"main.fad58de7366495db4650cfefac2fcd61_3" [label="3: Return Stmt \n *&return:int=0 [line 17]\n " shape="box"]


"main.fad58de7366495db4650cfefac2fcd61_3" -> "main.fad58de7366495db4650cfefac2fcd61_2" ;
"main.fad58de7366495db4650cfefac2fcd61_4" [label="4: BinaryOperatorStmt: Assign \n *&X:int=4 [line 14]\n n$0=*&X:int [line 15]\n *&y:int=n$0 [line 13]\n " shape="box"]
"main.fad58de7366495db4650cfefac2fcd61_4" [label="4: Fallback node \n n$0=*&X:int [line 15]\n " shape="box"]


"main.fad58de7366495db4650cfefac2fcd61_4" -> "main.fad58de7366495db4650cfefac2fcd61_3" ;
"main.fad58de7366495db4650cfefac2fcd61_5" [label="5: DeclStmt \n *&y:int=3 [line 11]\n " shape="box"]
"main.fad58de7366495db4650cfefac2fcd61_4" -> "main.fad58de7366495db4650cfefac2fcd61_6" ;
"main.fad58de7366495db4650cfefac2fcd61_5" [label="5: DeclStmt \n *&X:int=4 [line 14]\n " shape="box"]


"main.fad58de7366495db4650cfefac2fcd61_5" -> "main.fad58de7366495db4650cfefac2fcd61_4" ;
"main.fad58de7366495db4650cfefac2fcd61_6" [label="6: BinaryOperatorStmt: Assign \n *&y:int=n$0 [line 13]\n " shape="box"]


"main.fad58de7366495db4650cfefac2fcd61_6" -> "main.fad58de7366495db4650cfefac2fcd61_3" ;
"main.fad58de7366495db4650cfefac2fcd61_7" [label="7: DeclStmt \n *&y:int=3 [line 11]\n " shape="box"]


"main.fad58de7366495db4650cfefac2fcd61_7" -> "main.fad58de7366495db4650cfefac2fcd61_5" ;
"test.098f6bcd4621d373cade4e832627b4f6_1" [label="1: Start test\nFormals: p:int*\nLocals: y:int x:int \n DECLARE_LOCALS(&return,&y,&x); [line 20]\n " color=yellow style=filled]


"test.098f6bcd4621d373cade4e832627b4f6_1" -> "test.098f6bcd4621d373cade4e832627b4f6_3" ;
"test.098f6bcd4621d373cade4e832627b4f6_1" -> "test.098f6bcd4621d373cade4e832627b4f6_5" ;
"test.098f6bcd4621d373cade4e832627b4f6_2" [label="2: Exit test \n " color=yellow style=filled]


"test.098f6bcd4621d373cade4e832627b4f6_3" [label="3: Return Stmt \n n$2=*&p:int* [line 22]\n n$3=*n$2:int [line 22]\n *&x:int=n$3 [line 22]\n *&y:int=1 [line 23]\n n$0=*&x:int [line 24]\n n$1=*&y:int [line 24]\n *&return:int=(n$0 + n$1) [line 21]\n " shape="box"]
"test.098f6bcd4621d373cade4e832627b4f6_3" [label="3: BinaryOperatorStmt: Add \n n$0=*&x:int [line 24]\n n$1=*&y:int [line 24]\n " shape="box"]


"test.098f6bcd4621d373cade4e832627b4f6_3" -> "test.098f6bcd4621d373cade4e832627b4f6_6" ;
"test.098f6bcd4621d373cade4e832627b4f6_4" [label="4: DeclStmt \n *&y:int=1 [line 23]\n " shape="box"]


"test.098f6bcd4621d373cade4e832627b4f6_4" -> "test.098f6bcd4621d373cade4e832627b4f6_3" ;
"test.098f6bcd4621d373cade4e832627b4f6_5" [label="5: DeclStmt \n n$2=*&p:int* [line 22]\n n$3=*n$2:int [line 22]\n *&x:int=n$3 [line 22]\n " shape="box"]


"test.098f6bcd4621d373cade4e832627b4f6_5" -> "test.098f6bcd4621d373cade4e832627b4f6_4" ;
"test.098f6bcd4621d373cade4e832627b4f6_6" [label="6: Return Stmt \n *&return:int=(n$0 + n$1) [line 21]\n " shape="box"]


"test.098f6bcd4621d373cade4e832627b4f6_6" -> "test.098f6bcd4621d373cade4e832627b4f6_2" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_1" [label="1: Start with_conditional\nFormals: p:int*\nLocals: 0$?%__sil_tmpSIL_temp_conditional___n$0:int x:int \n DECLARE_LOCALS(&return,&0$?%__sil_tmpSIL_temp_conditional___n$0,&x); [line 28]\n " color=yellow style=filled]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_1" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_9" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_2" [label="2: Exit with_conditional \n " color=yellow style=filled]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_3" [label="3: + \n " ]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_3" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_8" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_4" [label="4: Prune (true branch) \n n$1=*&p:int* [line 31]\n PRUNE((n$1 != 0), true); [line 31]\n " shape="invhouse"]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_4" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_6" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_5" [label="5: Prune (false branch) \n n$1=*&p:int* [line 31]\n PRUNE((n$1 == 0), false); [line 31]\n " shape="invhouse"]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_5" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_7" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_6" [label="6: ConditinalStmt Branch \n n$2=*&p:int* [line 31]\n n$3=*n$2:int [line 31]\n n$4=*&x:int [line 31]\n *&0$?%__sil_tmpSIL_temp_conditional___n$0:int=(n$3 + n$4) [line 31]\n " shape="box"]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_6" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_3" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_7" [label="7: ConditinalStmt Branch \n n$5=*&x:int [line 31]\n *&0$?%__sil_tmpSIL_temp_conditional___n$0:int=n$5 [line 31]\n " shape="box"]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_7" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_3" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_8" [label="8: Fallback node \n n$6=*&0$?%__sil_tmpSIL_temp_conditional___n$0:int [line 31]\n " shape="box"]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_8" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_10" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_9" [label="9: DeclStmt \n *&x:int=1 [line 30]\n " shape="box"]


"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_9" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_4" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_9" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_5" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_10" [label="10: Return Stmt \n *&return:int=n$6 [line 29]\n " shape="box"]


"test.098f6bcd4621d373cade4e832627b4f6_3" -> "test.098f6bcd4621d373cade4e832627b4f6_2" ;
"with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_10" -> "with_conditional.c7f3381cc5bd6cfe19bc60c013ae8f1c_2" ;
}
18 changes: 15 additions & 3 deletions infer/tests/codetoanalyze/objc/shared/block/dispatch.m.dot
Expand Up @@ -89,14 +89,26 @@ digraph iCFG {
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_1" [label="1: Start DispatchA_dispatch_a_block_variable_from_macro\nFormals: \nLocals: initialization_block__:_fn_(*) \n DECLARE_LOCALS(&return,&initialization_block__); [line 55]\n " color=yellow style=filled]


"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_1" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_3" ;
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_1" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_5" ;
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_2" [label="2: Exit DispatchA_dispatch_a_block_variable_from_macro \n " color=yellow style=filled]


"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_3" [label="3: Return Stmt \n DECLARE_LOCALS(&__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4); [line 58]\n n$25=_fun___objc_alloc_no_fail(sizeof(__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4):unsigned long) [line 58]\n *&__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4:__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4=n$25 [line 58]\n n$26=*&#GB<codetoanalyze/objc/shared/block/dispatch.m>$DispatchA_dispatch_a_block_variable_from_macro_static_storage__:objc_object* [line 58]\n *n$25.DispatchA_dispatch_a_block_variable_from_macro_static_storage__:objc_object*=n$26 [line 58]\n *&initialization_block__:_fn_(*)=(_fun___objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4) [line 58]\n n$21=*&initialization_block__:_fn_(*) [line 62]\n n$22=n$21() [line 62]\n n$20=*&#GB<codetoanalyze/objc/shared/block/dispatch.m>$DispatchA_dispatch_a_block_variable_from_macro_static_storage__:objc_object* [line 63]\n *&return:objc_object*=n$20 [line 56]\n " shape="box"]
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_3" [label="3: Fallback node \n n$20=*&#GB<codetoanalyze/objc/shared/block/dispatch.m>$DispatchA_dispatch_a_block_variable_from_macro_static_storage__:objc_object* [line 63]\n " shape="box"]


"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_3" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_2" ;
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_3" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_6" ;
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_4" [label="4: Call n$21 \n n$21=*&initialization_block__:_fn_(*) [line 62]\n n$22=n$21() [line 62]\n " shape="box"]


"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_4" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_3" ;
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_5" [label="5: DeclStmt \n DECLARE_LOCALS(&__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4); [line 58]\n n$25=_fun___objc_alloc_no_fail(sizeof(__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4):unsigned long) [line 58]\n *&__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4:__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4=n$25 [line 58]\n n$26=*&#GB<codetoanalyze/objc/shared/block/dispatch.m>$DispatchA_dispatch_a_block_variable_from_macro_static_storage__:objc_object* [line 58]\n *n$25.DispatchA_dispatch_a_block_variable_from_macro_static_storage__:objc_object*=n$26 [line 58]\n *&initialization_block__:_fn_(*)=(_fun___objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4) [line 58]\n " shape="box"]


"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_5" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_4" ;
"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_6" [label="6: Return Stmt \n *&return:objc_object*=n$20 [line 56]\n " shape="box"]


"dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_6" -> "dispatch_a_block_variable_from_macro#DispatchA#class.92567a38d5ab3cf637f72030b1097441_2" ;
"__objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4.82bfd971252ed3dd1cbfd850406db887_1" [label="1: Start __objc_anonymous_block_DispatchA_dispatch_a_block_variable_from_macro______4\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 58]\n " color=yellow style=filled]


Expand Down
Expand Up @@ -3,14 +3,22 @@ digraph iCFG {
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_1" [label="1: Start DispatchInMacroTest\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 22]\n " color=yellow style=filled]


"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_1" -> "DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_3" ;
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_1" -> "DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_4" ;
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_2" [label="2: Exit DispatchInMacroTest \n " color=yellow style=filled]


"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_3" [label="3: Return Stmt \n DECLARE_LOCALS(&__objc_anonymous_block_DispatchInMacroTest______1); [line 23]\n n$3=_fun___objc_alloc_no_fail(sizeof(__objc_anonymous_block_DispatchInMacroTest______1):unsigned long) [line 23]\n *&__objc_anonymous_block_DispatchInMacroTest______1:__objc_anonymous_block_DispatchInMacroTest______1=n$3 [line 23]\n n$4=*&#GB<codetoanalyze/objc/shared/block/dispatch_in_macro.m>$DispatchInMacroTest_static_storage:NSObject* [line 23]\n *n$3.DispatchInMacroTest_static_storage:NSObject*=n$4 [line 23]\n n$5=(_fun___objc_anonymous_block_DispatchInMacroTest______1)() [line 23]\n n$0=*&#GB<codetoanalyze/objc/shared/block/dispatch_in_macro.m>$DispatchInMacroTest_static_storage:NSObject* [line 23]\n *&return:objc_object*=n$0 [line 23]\n " shape="box"]
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_3" [label="3: Fallback node \n n$0=*&#GB<codetoanalyze/objc/shared/block/dispatch_in_macro.m>$DispatchInMacroTest_static_storage:NSObject* [line 23]\n " shape="box"]


"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_3" -> "DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_2" ;
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_3" -> "DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_5" ;
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_4" [label="4: Call (_fun___objc_anonymous_block_DispatchInMacroTest______1) \n DECLARE_LOCALS(&__objc_anonymous_block_DispatchInMacroTest______1); [line 23]\n n$3=_fun___objc_alloc_no_fail(sizeof(__objc_anonymous_block_DispatchInMacroTest______1):unsigned long) [line 23]\n *&__objc_anonymous_block_DispatchInMacroTest______1:__objc_anonymous_block_DispatchInMacroTest______1=n$3 [line 23]\n n$4=*&#GB<codetoanalyze/objc/shared/block/dispatch_in_macro.m>$DispatchInMacroTest_static_storage:NSObject* [line 23]\n *n$3.DispatchInMacroTest_static_storage:NSObject*=n$4 [line 23]\n n$5=(_fun___objc_anonymous_block_DispatchInMacroTest______1)() [line 23]\n " shape="box"]


"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_4" -> "DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_3" ;
"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_5" [label="5: Return Stmt \n *&return:objc_object*=n$0 [line 23]\n " shape="box"]


"DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_5" -> "DispatchInMacroTest.f5d56763274a479d06265a2f9562bef1_2" ;
"__objc_anonymous_block_DispatchInMacroTest______1.db6c315d2cd0e3514d444428887908e2_1" [label="1: Start __objc_anonymous_block_DispatchInMacroTest______1\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 23]\n " color=yellow style=filled]


Expand Down

0 comments on commit e366b0d

Please sign in to comment.