Summary: Pass object by reference every time struct object is passed by value in C++. Do it only for C++/objC++ where we have guarantee that the object which is passed will be temporary one (created by copy constructor). Reviewed By: jberdine Differential Revision: D3346271 fbshipit-source-id: d3e5daamaster
parent
8639042bc0
commit
3699b9c0dc
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
int f;
|
||||||
|
X(int f) : f(f) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Y {
|
||||||
|
Y(const X& x) : x(x) {}
|
||||||
|
X x;
|
||||||
|
};
|
||||||
|
|
||||||
|
int get_f(X val) { return val.f; }
|
||||||
|
|
||||||
|
// val is passed by value, so it's not really a setter
|
||||||
|
void set_f(X val, int f) { val.f = f; }
|
||||||
|
|
||||||
|
int var_div0() {
|
||||||
|
X x(0);
|
||||||
|
return 1 / get_f(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
int var_div1() {
|
||||||
|
X x(1);
|
||||||
|
return 1 / get_f(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
int temp_div0() { return 1 / get_f(X(0)); }
|
||||||
|
|
||||||
|
int temp_div1() { return 1 / get_f(X(1)); }
|
||||||
|
|
||||||
|
int field_div0() {
|
||||||
|
X x(0);
|
||||||
|
Y y(x);
|
||||||
|
return 1 / get_f(y.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
int param_get_copied_div0() {
|
||||||
|
X x(0);
|
||||||
|
set_f(x, 1); // this won't change x
|
||||||
|
return 1 / x.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int param_get_copied_div1() {
|
||||||
|
X x(1);
|
||||||
|
set_f(x, 0); // this won't change x
|
||||||
|
return 1 / x.f;
|
||||||
|
}
|
@ -0,0 +1,177 @@
|
|||||||
|
digraph iCFG {
|
||||||
|
47 [label="47: DeclStmt \n _fun_X_X(&x:class X *,1:int ) [line 52]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
47 -> 46 ;
|
||||||
|
46 [label="46: Call _fun_set_f \n _fun_X_X(&__temp_construct_n$1:class X *,&x:class X &) [line 53]\n _fun_set_f(&__temp_construct_n$1:class X ,0:int ) [line 53]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
46 -> 45 ;
|
||||||
|
45 [label="45: Return Stmt \n n$0=*&x.f:int [line 54]\n *&return:int =(1 / n$0) [line 54]\n REMOVE_TEMPS(n$0); [line 54]\n APPLY_ABSTRACTION; [line 54]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
45 -> 44 ;
|
||||||
|
44 [label="44: Exit param_get_copied_div1 \n NULLIFY(&x); [line 55]\n NULLIFY(&__temp_construct_n$1); [line 55]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
43 [label="43: Start param_get_copied_div1\nFormals: \nLocals: __temp_construct_n$1:class X x:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$1,&x); [line 51]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
43 -> 47 ;
|
||||||
|
42 [label="42: DeclStmt \n _fun_X_X(&x:class X *,0:int ) [line 46]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
42 -> 41 ;
|
||||||
|
41 [label="41: Call _fun_set_f \n _fun_X_X(&__temp_construct_n$1:class X *,&x:class X &) [line 47]\n _fun_set_f(&__temp_construct_n$1:class X ,1:int ) [line 47]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
41 -> 40 ;
|
||||||
|
40 [label="40: Return Stmt \n n$0=*&x.f:int [line 48]\n *&return:int =(1 / n$0) [line 48]\n REMOVE_TEMPS(n$0); [line 48]\n APPLY_ABSTRACTION; [line 48]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
40 -> 39 ;
|
||||||
|
39 [label="39: Exit param_get_copied_div0 \n NULLIFY(&x); [line 49]\n NULLIFY(&__temp_construct_n$1); [line 49]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
38 [label="38: Start param_get_copied_div0\nFormals: \nLocals: __temp_construct_n$1:class X x:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$1,&x); [line 45]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
38 -> 42 ;
|
||||||
|
37 [label="37: DeclStmt \n _fun_X_X(&x:class X *,0:int ) [line 40]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
37 -> 36 ;
|
||||||
|
36 [label="36: DeclStmt \n _fun_Y_Y(&y:class Y *,&x:class X &) [line 41]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
36 -> 35 ;
|
||||||
|
35 [label="35: Return Stmt \n _fun_X_X(&__temp_construct_n$0:class X *,&y.x:class X &) [line 42]\n n$1=_fun_get_f(&__temp_construct_n$0:class X ) [line 42]\n *&return:int =(1 / n$1) [line 42]\n REMOVE_TEMPS(n$1); [line 42]\n APPLY_ABSTRACTION; [line 42]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
35 -> 34 ;
|
||||||
|
34 [label="34: Exit field_div0 \n NULLIFY(&y); [line 43]\n NULLIFY(&x); [line 43]\n NULLIFY(&__temp_construct_n$0); [line 43]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
33 [label="33: Start field_div0\nFormals: \nLocals: __temp_construct_n$0:class X y:class Y x:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$0,&y,&x); [line 39]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
33 -> 37 ;
|
||||||
|
32 [label="32: Return Stmt \n _fun_X_X(&SIL_materialize_temp__n$1:class X *,1:int ) [line 37]\n _fun_X_X(&__temp_construct_n$0:class X *,&SIL_materialize_temp__n$1:class X &) [line 37]\n n$2=_fun_get_f(&__temp_construct_n$0:class X ) [line 37]\n *&return:int =(1 / n$2) [line 37]\n REMOVE_TEMPS(n$2); [line 37]\n APPLY_ABSTRACTION; [line 37]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
32 -> 31 ;
|
||||||
|
31 [label="31: Exit temp_div1 \n NULLIFY(&__temp_construct_n$0); [line 37]\n NULLIFY(&SIL_materialize_temp__n$1); [line 37]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
30 [label="30: Start temp_div1\nFormals: \nLocals: __temp_construct_n$0:class X SIL_materialize_temp__n$1:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$0,&SIL_materialize_temp__n$1); [line 37]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
30 -> 32 ;
|
||||||
|
29 [label="29: Return Stmt \n _fun_X_X(&SIL_materialize_temp__n$1:class X *,0:int ) [line 35]\n _fun_X_X(&__temp_construct_n$0:class X *,&SIL_materialize_temp__n$1:class X &) [line 35]\n n$2=_fun_get_f(&__temp_construct_n$0:class X ) [line 35]\n *&return:int =(1 / n$2) [line 35]\n REMOVE_TEMPS(n$2); [line 35]\n APPLY_ABSTRACTION; [line 35]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
29 -> 28 ;
|
||||||
|
28 [label="28: Exit temp_div0 \n NULLIFY(&__temp_construct_n$0); [line 35]\n NULLIFY(&SIL_materialize_temp__n$1); [line 35]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
27 [label="27: Start temp_div0\nFormals: \nLocals: __temp_construct_n$0:class X SIL_materialize_temp__n$1:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$0,&SIL_materialize_temp__n$1); [line 35]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
27 -> 29 ;
|
||||||
|
26 [label="26: DeclStmt \n _fun_X_X(&x:class X *,1:int ) [line 31]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
26 -> 25 ;
|
||||||
|
25 [label="25: Return Stmt \n _fun_X_X(&__temp_construct_n$0:class X *,&x:class X &) [line 32]\n n$1=_fun_get_f(&__temp_construct_n$0:class X ) [line 32]\n *&return:int =(1 / n$1) [line 32]\n REMOVE_TEMPS(n$1); [line 32]\n APPLY_ABSTRACTION; [line 32]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
25 -> 24 ;
|
||||||
|
24 [label="24: Exit var_div1 \n NULLIFY(&x); [line 33]\n NULLIFY(&__temp_construct_n$0); [line 33]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
23 [label="23: Start var_div1\nFormals: \nLocals: __temp_construct_n$0:class X x:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$0,&x); [line 30]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
23 -> 26 ;
|
||||||
|
22 [label="22: DeclStmt \n _fun_X_X(&x:class X *,0:int ) [line 26]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
22 -> 21 ;
|
||||||
|
21 [label="21: Return Stmt \n _fun_X_X(&__temp_construct_n$0:class X *,&x:class X &) [line 27]\n n$1=_fun_get_f(&__temp_construct_n$0:class X ) [line 27]\n *&return:int =(1 / n$1) [line 27]\n REMOVE_TEMPS(n$1); [line 27]\n APPLY_ABSTRACTION; [line 27]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
21 -> 20 ;
|
||||||
|
20 [label="20: Exit var_div0 \n NULLIFY(&x); [line 28]\n NULLIFY(&__temp_construct_n$0); [line 28]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
19 [label="19: Start var_div0\nFormals: \nLocals: __temp_construct_n$0:class X x:class X \n DECLARE_LOCALS(&return,&__temp_construct_n$0,&x); [line 25]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
19 -> 22 ;
|
||||||
|
18 [label="18: BinaryOperatorStmt: Assign \n n$0=*&val:class X & [line 23]\n n$1=*&f:int [line 23]\n *n$0.f:int =n$1 [line 23]\n REMOVE_TEMPS(n$0,n$1); [line 23]\n NULLIFY(&f); [line 23]\n NULLIFY(&val); [line 23]\n APPLY_ABSTRACTION; [line 23]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
18 -> 17 ;
|
||||||
|
17 [label="17: Exit set_f \n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
16 [label="16: Start set_f\nFormals: val:class X & f:int \nLocals: \n DECLARE_LOCALS(&return); [line 23]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
16 -> 18 ;
|
||||||
|
15 [label="15: Return Stmt \n n$0=*&val:class X & [line 20]\n n$1=*n$0.f:int [line 20]\n *&return:int =n$1 [line 20]\n REMOVE_TEMPS(n$0,n$1); [line 20]\n NULLIFY(&val); [line 20]\n APPLY_ABSTRACTION; [line 20]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
15 -> 14 ;
|
||||||
|
14 [label="14: Exit get_f \n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
13 [label="13: Start get_f\nFormals: val:class X &\nLocals: \n DECLARE_LOCALS(&return); [line 20]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
13 -> 15 ;
|
||||||
|
12 [label="12: Constructor Init \n n$0=*&this:class Y * [line 16]\n n$1=*&x:class X & [line 16]\n _fun_X_X(n$0.x:class X *,n$1:class X &) [line 16]\n REMOVE_TEMPS(n$0,n$1); [line 16]\n NULLIFY(&this); [line 16]\n NULLIFY(&x); [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
12 -> 11 ;
|
||||||
|
11 [label="11: Exit Y_Y \n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
10 [label="10: Start Y_Y\nFormals: this:class Y * x:class X &\nLocals: \n DECLARE_LOCALS(&return); [line 16]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
10 -> 12 ;
|
||||||
|
9 [label="9: Constructor Init \n n$0=*&this:class X * [line 10]\n n$1=*&__param_0:class X & [line 10]\n n$2=*n$1.f:int [line 10]\n *n$0.f:int =n$2 [line 10]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 10]\n NULLIFY(&__param_0); [line 10]\n NULLIFY(&this); [line 10]\n APPLY_ABSTRACTION; [line 10]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
9 -> 8 ;
|
||||||
|
8 [label="8: Exit X_X \n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
7 [label="7: Start X_X\nFormals: this:class X * __param_0:class X &\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
7 -> 9 ;
|
||||||
|
6 [label="6: Constructor Init \n n$0=*&this:class X * [line 10]\n n$1=*&__param_0:class X & [line 10]\n n$2=*n$1.f:int [line 10]\n *n$0.f:int =n$2 [line 10]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 10]\n NULLIFY(&__param_0); [line 10]\n NULLIFY(&this); [line 10]\n APPLY_ABSTRACTION; [line 10]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
6 -> 5 ;
|
||||||
|
5 [label="5: Exit X_X \n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
4 [label="4: Start X_X\nFormals: this:class X * __param_0:class X &\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
4 -> 6 ;
|
||||||
|
3 [label="3: Constructor Init \n n$0=*&this:class X * [line 12]\n n$1=*&f:int [line 12]\n *n$0.f:int =n$1 [line 12]\n REMOVE_TEMPS(n$0,n$1); [line 12]\n NULLIFY(&f); [line 12]\n NULLIFY(&this); [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="box"]
|
||||||
|
|
||||||
|
|
||||||
|
3 -> 2 ;
|
||||||
|
2 [label="2: Exit X_X \n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
1 [label="1: Start X_X\nFormals: this:class X * f:int \nLocals: \n DECLARE_LOCALS(&return); [line 12]\n " color=yellow style=filled]
|
||||||
|
|
||||||
|
|
||||||
|
1 -> 3 ;
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package endtoend.cpp;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import utils.DebuggableTemporaryFolder;
|
||||||
|
import utils.InferException;
|
||||||
|
import utils.InferResults;
|
||||||
|
import utils.InferRunner;
|
||||||
|
|
||||||
|
public class StructPassByValueTest {
|
||||||
|
|
||||||
|
public static final String FILE =
|
||||||
|
"infer/tests/codetoanalyze/cpp/frontend/types/struct_pass_by_value.cpp";
|
||||||
|
|
||||||
|
private static ImmutableList<String> inferCmd;
|
||||||
|
|
||||||
|
public static final String DIVIDE_BY_ZERO = "DIVIDE_BY_ZERO";
|
||||||
|
|
||||||
|
@ClassRule
|
||||||
|
public static DebuggableTemporaryFolder folder =
|
||||||
|
new DebuggableTemporaryFolder();
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void runInfer() throws InterruptedException, IOException {
|
||||||
|
inferCmd = InferRunner.createCPPInferCommand(folder, FILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInferRunsOnDiv0MethodsErrorIsFound()
|
||||||
|
throws InterruptedException, IOException, InferException {
|
||||||
|
InferResults inferResults = InferRunner.runInferCPP(inferCmd);
|
||||||
|
String[] procedures = {
|
||||||
|
"var_div0",
|
||||||
|
"temp_div0",
|
||||||
|
"field_div0",
|
||||||
|
"param_get_copied_div0",
|
||||||
|
};
|
||||||
|
assertThat(
|
||||||
|
"Results should contain the expected divide by zero",
|
||||||
|
inferResults,
|
||||||
|
containsExactly(
|
||||||
|
DIVIDE_BY_ZERO,
|
||||||
|
FILE,
|
||||||
|
procedures
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue