diff --git a/infer/src/IR/Procdesc.ml b/infer/src/IR/Procdesc.ml index 07f6735a8..930de0258 100644 --- a/infer/src/IR/Procdesc.ml +++ b/infer/src/IR/Procdesc.ml @@ -84,6 +84,7 @@ module Node = struct | IfStmtBranch | InitializeDynamicArrayLength | InitListExp + | LoopBody | MessageCall of string | MethodBody | MonitorEnter @@ -345,6 +346,8 @@ module Node = struct F.pp_print_string fmt "Initialize dynamic array length" | InitListExp -> F.pp_print_string fmt "InitListExp" + | LoopBody -> + F.pp_print_string fmt "LoopBody" | MessageCall selector -> F.fprintf fmt "Message Call: %s" selector | MethodBody -> diff --git a/infer/src/IR/Procdesc.mli b/infer/src/IR/Procdesc.mli index 6380ec137..039782e65 100644 --- a/infer/src/IR/Procdesc.mli +++ b/infer/src/IR/Procdesc.mli @@ -65,6 +65,7 @@ module Node : sig | IfStmtBranch | InitializeDynamicArrayLength | InitListExp + | LoopBody | MessageCall of string | MethodBody | MonitorEnter diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 4a4817609..f7d114d79 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -2194,7 +2194,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s let trans_state_body = {trans_state with succ_nodes= body_succ_nodes; continuation= body_continuation} in - instruction trans_state_body (Loops.get_body loop_kind) + exec_with_node_creation LoopBody ~f:instruction trans_state_body (Loops.get_body loop_kind) in let join_succ_nodes = match loop_kind with diff --git a/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c b/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c index b1f433eb6..e939cee5c 100644 --- a/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c +++ b/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c @@ -5,10 +5,16 @@ * LICENSE file in the root directory of this source tree. */ -int main() { +int simple_for_loop() { int j = 0; for (int i = 0; i < 10; i++) { j += j; } return 0; } + +void for_loop_no_brackets() { + int j = 0; + for (int i = 0; i < 1; i++) + j; +} diff --git a/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c.dot b/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c.dot index 005156512..6f1f59b3b 100644 --- a/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c.dot +++ b/infer/tests/codetoanalyze/c/frontend/loops/for_simple.c.dot @@ -1,47 +1,87 @@ /* @generated */ digraph cfg { -"main.fad58de7366495db4650cfefac2fcd61_1" [label="1: Start main\nFormals: \nLocals: i:int j:int \n " color=yellow style=filled] +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_1" [label="1: Start for_loop_no_brackets\nFormals: \nLocals: i:int j:int \n " color=yellow style=filled] - "main.fad58de7366495db4650cfefac2fcd61_1" -> "main.fad58de7366495db4650cfefac2fcd61_11" ; -"main.fad58de7366495db4650cfefac2fcd61_2" [label="2: Exit main \n " color=yellow style=filled] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_1" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_10" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_2" [label="2: Exit for_loop_no_brackets \n " color=yellow style=filled] -"main.fad58de7366495db4650cfefac2fcd61_3" [label="3: Return Stmt \n *&return:int=0 [line 13, column 3]\n " shape="box"] +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_3" [label="3: + \n " ] - "main.fad58de7366495db4650cfefac2fcd61_3" -> "main.fad58de7366495db4650cfefac2fcd61_2" ; -"main.fad58de7366495db4650cfefac2fcd61_4" [label="4: + \n " ] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_3" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_6" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_4" [label="4: DeclStmt \n VARIABLE_DECLARED(i:int); [line 18, column 8]\n *&i:int=0 [line 18, column 8]\n " shape="box"] - "main.fad58de7366495db4650cfefac2fcd61_4" -> "main.fad58de7366495db4650cfefac2fcd61_7" ; -"main.fad58de7366495db4650cfefac2fcd61_5" [label="5: DeclStmt \n VARIABLE_DECLARED(i:int); [line 10, column 8]\n *&i:int=0 [line 10, column 8]\n " shape="box"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_4" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_3" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_5" [label="5: UnaryOperator \n n$0=*&i:int [line 18, column 26]\n *&i:int=(n$0 + 1) [line 18, column 26]\n " shape="box"] - "main.fad58de7366495db4650cfefac2fcd61_5" -> "main.fad58de7366495db4650cfefac2fcd61_4" ; -"main.fad58de7366495db4650cfefac2fcd61_6" [label="6: UnaryOperator \n n$0=*&i:int [line 10, column 27]\n *&i:int=(n$0 + 1) [line 10, column 27]\n " shape="box"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_5" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_3" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_6" [label="6: BinaryOperatorStmt: LT \n n$1=*&i:int [line 18, column 19]\n " shape="box"] - "main.fad58de7366495db4650cfefac2fcd61_6" -> "main.fad58de7366495db4650cfefac2fcd61_4" ; -"main.fad58de7366495db4650cfefac2fcd61_7" [label="7: BinaryOperatorStmt: LT \n n$1=*&i:int [line 10, column 19]\n " shape="box"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_6" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_7" ; + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_6" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_8" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_7" [label="7: Prune (true branch, for loop) \n PRUNE((n$1 < 1), true); [line 18, column 19]\n " shape="invhouse"] - "main.fad58de7366495db4650cfefac2fcd61_7" -> "main.fad58de7366495db4650cfefac2fcd61_8" ; - "main.fad58de7366495db4650cfefac2fcd61_7" -> "main.fad58de7366495db4650cfefac2fcd61_9" ; -"main.fad58de7366495db4650cfefac2fcd61_8" [label="8: Prune (true branch, for loop) \n PRUNE((n$1 < 10), true); [line 10, column 19]\n " shape="invhouse"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_7" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_9" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_8" [label="8: Prune (false branch, for loop) \n PRUNE(!(n$1 < 1), false); [line 18, column 19]\n " shape="invhouse"] - "main.fad58de7366495db4650cfefac2fcd61_8" -> "main.fad58de7366495db4650cfefac2fcd61_10" ; -"main.fad58de7366495db4650cfefac2fcd61_9" [label="9: Prune (false branch, for loop) \n PRUNE(!(n$1 < 10), false); [line 10, column 19]\n " shape="invhouse"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_8" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_2" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_9" [label="9: LoopBody \n n$2=*&j:int [line 19, column 5]\n " shape="box"] - "main.fad58de7366495db4650cfefac2fcd61_9" -> "main.fad58de7366495db4650cfefac2fcd61_3" ; -"main.fad58de7366495db4650cfefac2fcd61_10" [label="10: BinaryOperatorStmt: AddAssign \n n$2=*&j:int [line 11, column 10]\n n$3=*&j:int [line 11, column 5]\n *&j:int=(n$3 + n$2) [line 11, column 5]\n " shape="box"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_9" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_5" ; +"for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_10" [label="10: DeclStmt \n VARIABLE_DECLARED(j:int); [line 17, column 3]\n *&j:int=0 [line 17, column 3]\n " shape="box"] - "main.fad58de7366495db4650cfefac2fcd61_10" -> "main.fad58de7366495db4650cfefac2fcd61_6" ; -"main.fad58de7366495db4650cfefac2fcd61_11" [label="11: DeclStmt \n VARIABLE_DECLARED(j:int); [line 9, column 3]\n *&j:int=0 [line 9, column 3]\n " shape="box"] + "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_10" -> "for_loop_no_brackets.4c9da26592fb7fb2077b8ad5de66fe11_4" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_1" [label="1: Start simple_for_loop\nFormals: \nLocals: i:int j:int \n " color=yellow style=filled] - "main.fad58de7366495db4650cfefac2fcd61_11" -> "main.fad58de7366495db4650cfefac2fcd61_5" ; + "simple_for_loop.620d527928c080a35fd7b56855111df4_1" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_11" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_2" [label="2: Exit simple_for_loop \n " color=yellow style=filled] + + +"simple_for_loop.620d527928c080a35fd7b56855111df4_3" [label="3: Return Stmt \n *&return:int=0 [line 13, column 3]\n " shape="box"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_3" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_2" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_4" [label="4: + \n " ] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_4" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_7" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_5" [label="5: DeclStmt \n VARIABLE_DECLARED(i:int); [line 10, column 8]\n *&i:int=0 [line 10, column 8]\n " shape="box"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_5" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_4" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_6" [label="6: UnaryOperator \n n$0=*&i:int [line 10, column 27]\n *&i:int=(n$0 + 1) [line 10, column 27]\n " shape="box"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_6" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_4" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_7" [label="7: BinaryOperatorStmt: LT \n n$1=*&i:int [line 10, column 19]\n " shape="box"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_7" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_8" ; + "simple_for_loop.620d527928c080a35fd7b56855111df4_7" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_9" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_8" [label="8: Prune (true branch, for loop) \n PRUNE((n$1 < 10), true); [line 10, column 19]\n " shape="invhouse"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_8" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_10" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_9" [label="9: Prune (false branch, for loop) \n PRUNE(!(n$1 < 10), false); [line 10, column 19]\n " shape="invhouse"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_9" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_3" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_10" [label="10: BinaryOperatorStmt: AddAssign \n n$2=*&j:int [line 11, column 10]\n n$3=*&j:int [line 11, column 5]\n *&j:int=(n$3 + n$2) [line 11, column 5]\n " shape="box"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_10" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_6" ; +"simple_for_loop.620d527928c080a35fd7b56855111df4_11" [label="11: DeclStmt \n VARIABLE_DECLARED(j:int); [line 9, column 3]\n *&j:int=0 [line 9, column 3]\n " shape="box"] + + + "simple_for_loop.620d527928c080a35fd7b56855111df4_11" -> "simple_for_loop.620d527928c080a35fd7b56855111df4_5" ; }