diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 974831a76..ed89ae6f4 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -1440,6 +1440,32 @@ struct let dowhile_kind = Loops.DoWhile (cond, body) in loop_instruction trans_state dowhile_kind stmt_info + + (* Iteration over colection + + for (v : C) { body; } + + is translated as follows: + + TypeC __range = C; + for (__begin = __range.begin(), __end = __range.end(); + __begin != __end; + ++__begin) + { + v = *__begin; + loop_body; + } + *) + and cxxForRangeStmt_trans trans_state stmt_info stmt_list = + let open Clang_ast_t in + match stmt_list with + | [iterator_decl; initial_cond; exit_cond; increment; assign_current_index; loop_body] -> + let loop_body' = CompoundStmt (stmt_info, [assign_current_index; loop_body]) in + let null_stmt = NullStmt (stmt_info, []) in + let for_loop = ForStmt (stmt_info, [initial_cond; null_stmt; exit_cond; increment; loop_body']) in + instruction trans_state (CompoundStmt (stmt_info, [iterator_decl; for_loop])) + | _ -> assert false + (* Fast iteration for colection for (type_it i in collection) { body } is translate as @@ -2064,7 +2090,6 @@ struct stmtExpr_trans trans_state stmt_info stmt_list expr_info | ForStmt(stmt_info, [init; decl_stmt; cond; incr; body]) -> - (* Note: we ignore null_stmt at the moment.*) forStmt_trans trans_state init decl_stmt cond incr body stmt_info | WhileStmt(stmt_info, [decl_stmt; cond; body]) -> @@ -2072,6 +2097,9 @@ struct | DoStmt(stmt_info, [body; cond]) -> doStmt_trans trans_state stmt_info cond body + + | CXXForRangeStmt (stmt_info, stmt_list) -> + cxxForRangeStmt_trans trans_state stmt_info stmt_list | ObjCForCollectionStmt(stmt_info, [item; items; body]) -> objCForCollectionStmt_trans trans_state item items body stmt_info @@ -2237,7 +2265,7 @@ struct | CXXDefaultArgExpr (stmt_info, stmt_list, expr_info, default_arg_info) -> cxxDefaultArgExpr_trans trans_state default_arg_info - + | s -> (Printing.log_stats "\n!!!!WARNING: found statement %s. \nACTION REQUIRED: Translation need to be defined. Statement ignored.... \n" (Ast_utils.string_of_stmt s); diff --git a/infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp b/infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp new file mode 100644 index 000000000..5439f0fa8 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016 - 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. + */ + + +// iterator +struct iterator { + int val; + iterator operator++() { val += 1; return *this; } + int operator*() { return val; } // this should return type of values stored in vec +}; +bool operator!=(iterator i1, iterator i2) { return i1.val != i2.val; } + +struct vec { + vec(int size) { begin_.val = 0; end_.val = size; } + iterator begin() { return begin_; } + iterator end() { return end_; } + + iterator begin_; + iterator end_; +}; + +void test() { + vec vector(10); + for (int value : vector) { + int temp = value * value + 10; + } +} diff --git a/infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp.dot new file mode 100644 index 000000000..ec2954b9d --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp.dot @@ -0,0 +1,165 @@ +digraph iCFG { +43 [label="43: DeclStmt \n _fun_vec_vec(&vector:class vec *,10:int ) [line 29]\n " shape="box"] + + + 43 -> 42 ; +42 [label="42: DeclStmt \n *&__range:class vec &=&vector [line 30]\n " shape="box"] + + + 42 -> 35 ; +41 [label="41: DeclStmt \n n$13=_fun_iterator_operator*(&__begin:class iterator &) [line 30]\n *&value:int =n$13 [line 30]\n REMOVE_TEMPS(n$13); [line 30]\n " shape="box"] + + + 41 -> 40 ; +40 [label="40: DeclStmt \n n$11=*&value:int [line 31]\n n$12=*&value:int [line 31]\n *&temp:int =((n$11 * n$12) + 10) [line 31]\n REMOVE_TEMPS(n$11,n$12); [line 31]\n NULLIFY(&temp,false); [line 31]\n NULLIFY(&value,false); [line 31]\n " shape="box"] + + + 40 -> 36 ; +39 [label="39: Prune (false branch) \n PRUNE((n$10 == 0), false); [line 30]\n REMOVE_TEMPS(n$10); [line 30]\n NULLIFY(&SIL_materialize_temp__n$0,false); [line 30]\n NULLIFY(&SIL_materialize_temp__n$3,false); [line 30]\n NULLIFY(&__begin,false); [line 30]\n NULLIFY(&__end,false); [line 30]\n NULLIFY(&__temp_construct_n$8,false); [line 30]\n NULLIFY(&__temp_construct_n$9,false); [line 30]\n NULLIFY(&__temp_return_n$7,false); [line 30]\n NULLIFY(&vector,false); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="invhouse"] + + + 39 -> 32 ; +38 [label="38: Prune (true branch) \n PRUNE((n$10 != 0), true); [line 30]\n REMOVE_TEMPS(n$10); [line 30]\n " shape="invhouse"] + + + 38 -> 41 ; +37 [label="37: Call _fun_operator!= \n _fun_iterator_iterator(&__temp_construct_n$8:class iterator *,&__begin:class iterator &) [line 30]\n _fun_iterator_iterator(&__temp_construct_n$9:class iterator *,&__end:class iterator &) [line 30]\n n$10=_fun_operator!=(&__temp_construct_n$8:class iterator ,&__temp_construct_n$9:class iterator ) [line 30]\n " shape="box"] + + + 37 -> 38 ; + 37 -> 39 ; +36 [label="36: Call _fun_iterator_operator++ \n _fun_iterator_operator++(&__begin:class iterator &,&__temp_return_n$7:class iterator *) [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="box"] + + + 36 -> 33 ; +35 [label="35: DeclStmt \n n$4=*&__range:class vec & [line 30]\n _fun_vec_begin(n$4:class vec &,&SIL_materialize_temp__n$3:class iterator *) [line 30]\n _fun_iterator_iterator(&__begin:class iterator *,&SIL_materialize_temp__n$3:class iterator &) [line 30]\n REMOVE_TEMPS(n$4); [line 30]\n " shape="box"] + + + 35 -> 34 ; +34 [label="34: DeclStmt \n n$1=*&__range:class vec & [line 30]\n _fun_vec_end(n$1:class vec &,&SIL_materialize_temp__n$0:class iterator *) [line 30]\n _fun_iterator_iterator(&__end:class iterator *,&SIL_materialize_temp__n$0:class iterator &) [line 30]\n REMOVE_TEMPS(n$1); [line 30]\n NULLIFY(&__range,false); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="box"] + + + 34 -> 33 ; +33 [label="33: + \n " ] + + + 33 -> 37 ; +32 [label="32: Exit test \n " color=yellow style=filled] + + +31 [label="31: Start test\nFormals: \nLocals: __end:class iterator SIL_materialize_temp__n$0:class iterator __begin:class iterator SIL_materialize_temp__n$3:class iterator __temp_return_n$7:class iterator __temp_construct_n$8:class iterator __temp_construct_n$9:class iterator temp:int value:int __range:class vec & vector:class vec \n DECLARE_LOCALS(&return,&__end,&SIL_materialize_temp__n$0,&__begin,&SIL_materialize_temp__n$3,&__temp_return_n$7,&__temp_construct_n$8,&__temp_construct_n$9,&temp,&value,&__range,&vector); [line 28]\n NULLIFY(&__range,false); [line 28]\n NULLIFY(&temp,false); [line 28]\n NULLIFY(&value,false); [line 28]\n " color=yellow style=filled] + + + 31 -> 43 ; +30 [label="30: Return Stmt \n n$0=*&__return_param:class iterator * [line 22]\n n$1=*&this:class vec * [line 22]\n _fun_iterator_iterator(n$0:class iterator *,n$1.end_:class iterator &) [line 22]\n REMOVE_TEMPS(n$0,n$1); [line 22]\n NULLIFY(&__return_param,false); [line 22]\n NULLIFY(&this,false); [line 22]\n APPLY_ABSTRACTION; [line 22]\n " shape="box"] + + + 30 -> 29 ; +29 [label="29: Exit vec_end \n " color=yellow style=filled] + + +28 [label="28: Start vec_end\nFormals: this:class vec * __return_param:class iterator *\nLocals: \n DECLARE_LOCALS(&return); [line 22]\n " color=yellow style=filled] + + + 28 -> 30 ; +27 [label="27: Return Stmt \n n$0=*&__return_param:class iterator * [line 21]\n n$1=*&this:class vec * [line 21]\n _fun_iterator_iterator(n$0:class iterator *,n$1.begin_:class iterator &) [line 21]\n REMOVE_TEMPS(n$0,n$1); [line 21]\n NULLIFY(&__return_param,false); [line 21]\n NULLIFY(&this,false); [line 21]\n APPLY_ABSTRACTION; [line 21]\n " shape="box"] + + + 27 -> 26 ; +26 [label="26: Exit vec_begin \n " color=yellow style=filled] + + +25 [label="25: Start vec_begin\nFormals: this:class vec * __return_param:class iterator *\nLocals: \n DECLARE_LOCALS(&return); [line 21]\n " color=yellow style=filled] + + + 25 -> 27 ; +24 [label="24: Constructor Init \n n$4=*&this:class vec * [line 20]\n _fun_iterator_iterator(n$4.begin_:class iterator *) [line 20]\n REMOVE_TEMPS(n$4); [line 20]\n " shape="box"] + + + 24 -> 23 ; +23 [label="23: Constructor Init \n n$3=*&this:class vec * [line 20]\n _fun_iterator_iterator(n$3.end_:class iterator *) [line 20]\n REMOVE_TEMPS(n$3); [line 20]\n " shape="box"] + + + 23 -> 22 ; +22 [label="22: BinaryOperatorStmt: Assign \n n$2=*&this:class vec * [line 20]\n *n$2.begin_.val:int =0 [line 20]\n REMOVE_TEMPS(n$2); [line 20]\n " shape="box"] + + + 22 -> 21 ; +21 [label="21: BinaryOperatorStmt: Assign \n n$0=*&this:class vec * [line 20]\n n$1=*&size:int [line 20]\n *n$0.end_.val:int =n$1 [line 20]\n REMOVE_TEMPS(n$0,n$1); [line 20]\n NULLIFY(&size,false); [line 20]\n NULLIFY(&this,false); [line 20]\n APPLY_ABSTRACTION; [line 20]\n " shape="box"] + + + 21 -> 20 ; +20 [label="20: Exit vec_vec \n " color=yellow style=filled] + + +19 [label="19: Start vec_vec\nFormals: this:class vec * size:int \nLocals: \n DECLARE_LOCALS(&return); [line 20]\n " color=yellow style=filled] + + + 19 -> 24 ; +18 [label="18: Return Stmt \n n$0=*&i1.val:int [line 17]\n n$1=*&i2.val:int [line 17]\n *&return:_Bool =(n$0 != n$1) [line 17]\n REMOVE_TEMPS(n$0,n$1); [line 17]\n NULLIFY(&i1,false); [line 17]\n NULLIFY(&i2,false); [line 17]\n APPLY_ABSTRACTION; [line 17]\n " shape="box"] + + + 18 -> 17 ; +17 [label="17: Exit operator!= \n " color=yellow style=filled] + + +16 [label="16: Start operator!=\nFormals: i1:class iterator i2:class iterator \nLocals: \n DECLARE_LOCALS(&return); [line 17]\n " color=yellow style=filled] + + + 16 -> 18 ; +15 [label="15: Constructor Init \n n$0=*&this:class iterator * [line 12]\n n$1=*&__param_0:class iterator & [line 12]\n n$2=*n$1.val:int [line 12]\n *n$0.val:int =n$2 [line 12]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 12]\n NULLIFY(&__param_0,false); [line 12]\n NULLIFY(&this,false); [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="box"] + + + 15 -> 14 ; +14 [label="14: Exit iterator_iterator \n " color=yellow style=filled] + + +13 [label="13: Start iterator_iterator\nFormals: this:class iterator * __param_0:class iterator &\nLocals: \n DECLARE_LOCALS(&return); [line 12]\n " color=yellow style=filled] + + + 13 -> 15 ; +12 [label="12: Constructor Init \n n$0=*&this:class iterator * [line 12]\n n$1=*&__param_0:class iterator & [line 12]\n n$2=*n$1.val:int [line 12]\n *n$0.val:int =n$2 [line 12]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 12]\n NULLIFY(&__param_0,false); [line 12]\n NULLIFY(&this,false); [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="box"] + + + 12 -> 11 ; +11 [label="11: Exit iterator_iterator \n " color=yellow style=filled] + + +10 [label="10: Start iterator_iterator\nFormals: this:class iterator * __param_0:class iterator &\nLocals: \n DECLARE_LOCALS(&return); [line 12]\n " color=yellow style=filled] + + + 10 -> 12 ; +9 [label="9: Exit iterator_iterator \n " color=yellow style=filled] + + +8 [label="8: Start iterator_iterator\nFormals: this:class iterator *\nLocals: \n DECLARE_LOCALS(&return); [line 12]\n NULLIFY(&this,false); [line 12]\n " color=yellow style=filled] + + + 8 -> 9 ; +7 [label="7: Return Stmt \n n$0=*&this:class iterator * [line 15]\n n$1=*n$0.val:int [line 15]\n *&return:int =n$1 [line 15]\n REMOVE_TEMPS(n$0,n$1); [line 15]\n NULLIFY(&this,false); [line 15]\n APPLY_ABSTRACTION; [line 15]\n " shape="box"] + + + 7 -> 6 ; +6 [label="6: Exit iterator_operator* \n " color=yellow style=filled] + + +5 [label="5: Start iterator_operator*\nFormals: this:class iterator *\nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled] + + + 5 -> 7 ; +4 [label="4: BinaryOperatorStmt: AddAssign \n n$2=*&this:class iterator * [line 14]\n n$3=*n$2.val:int [line 14]\n *n$2.val:int =(n$3 + 1) [line 14]\n REMOVE_TEMPS(n$2,n$3); [line 14]\n " shape="box"] + + + 4 -> 3 ; +3 [label="3: Return Stmt \n n$0=*&__return_param:class iterator * [line 14]\n n$1=*&this:class iterator * [line 14]\n _fun_iterator_iterator(n$0:class iterator *,n$1:class iterator &) [line 14]\n REMOVE_TEMPS(n$0,n$1); [line 14]\n NULLIFY(&__return_param,false); [line 14]\n NULLIFY(&this,false); [line 14]\n APPLY_ABSTRACTION; [line 14]\n " shape="box"] + + + 3 -> 2 ; +2 [label="2: Exit iterator_operator++ \n " color=yellow style=filled] + + +1 [label="1: Start iterator_operator++\nFormals: this:class iterator * __return_param:class iterator *\nLocals: \n DECLARE_LOCALS(&return); [line 14]\n " color=yellow style=filled] + + + 1 -> 4 ; +} diff --git a/infer/tests/frontend/cpp/ForRangeLoopTest.java b/infer/tests/frontend/cpp/ForRangeLoopTest.java new file mode 100644 index 000000000..0f6c46f86 --- /dev/null +++ b/infer/tests/frontend/cpp/ForRangeLoopTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2013 - 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. + */ + +package frontend.cpp; + +import org.junit.Rule; +import org.junit.Test; +import utils.ClangFrontendUtils; +import utils.DebuggableTemporaryFolder; +import utils.InferException; + +import java.io.IOException; + +public class ForRangeLoopTest { + + @Rule + public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder(); + + @Test + public void whenCaptureRunOnForEach1ThenDotFilesAreTheSame() + throws InterruptedException, IOException, InferException { + String src = + "infer/tests/codetoanalyze/cpp/frontend/loops/foreach1.cpp"; + ClangFrontendUtils.createAndCompareCppDotFiles(folder, src); + } +}