Translate CXXOperatorCallExpr

Summary: public
Translate C++ overloaded operator calls. AST of their children looks slightly differently
which means that we have to be more permissive in params_trans.exps.

Difference:

  CXXMemberCallExpr:
    MemberExpr:
      'ThisExpr' (it's part of method decl ref evaluation)
      MethodDeclRef
    Param1
    Param2
    ...

  CXXOperatorCallExpr
    MethodDeclRef/FunctionDeclRef
    'ThisExpr' (it's part of parameters list evaluation)
    Param1
    Param2
    ...

Reviewed By: dulmarod

Differential Revision: D2679503

fb-gh-sync-id: 1437f73
master
Andrzej Kotulski 9 years ago committed by facebook-github-bot-7
parent c45f7793ea
commit 16cb8e3f30

@ -384,13 +384,14 @@ struct
match CMethod_trans.method_signature_of_pointer decl_ptr with
| Some ms -> CMethod_signature.ms_is_instance ms
| _ -> assert false in (* will happen for generated methods, shouldn't happen right now *)
let extra_exps = if is_instance_method then
(assert (IList.length pre_trans_result.exps = 1);
let (obj_sil, class_typ) = extract_exp_from_list pre_trans_result.exps
"WARNING: in Method call we expect to know the object\n" in
[(obj_sil, class_typ)])
let extra_exps = if is_instance_method then (
(* pre_trans_result.exps may contain expr for 'this' parameter:*)
(* if it comes from CXXMemberCallExpr it will be there *)
(* if it comes from CXXOperatorCallExpr it won't be there and will be added later *)
assert (IList.length pre_trans_result.exps <= 1);
pre_trans_result.exps)
else
(* don't add (obj_sil, class_typ) for static methods *)
(* don't add 'this' expression for static methods *)
[] in
(* consider using context.CContext.is_callee_expression to deal with pointers to methods? *)
(* unlike field access, for method calls there is no need to expand class type *)
@ -788,7 +789,10 @@ struct
and cxxMemberCallExpr_trans trans_state si stmt_list expr_info =
let pln = trans_state.parent_line_number in
let context = trans_state.context in
(* First stmt is the method+this expr and the rest are params *)
(* Structure is the following: *)
(* CXXMemberCallExpr: first stmt is method+this expr and the rest are normal params *)
(* CXXOperatorCallExpr: First stmt is method/function deref without this expr and the *)
(* rest are params, possibly including 'this' *)
let fun_exp_stmt, params_stmt = (match stmt_list with
| fe :: params -> fe, params
| _ -> assert false) in
@ -1975,6 +1979,9 @@ struct
| CXXMemberCallExpr(stmt_info, stmt_list, ei) ->
cxxMemberCallExpr_trans trans_state stmt_info stmt_list ei
| CXXOperatorCallExpr(stmt_info, stmt_list, ei) ->
callExpr_trans trans_state stmt_info stmt_list ei
| ObjCMessageExpr(stmt_info, stmt_list, expr_info, obj_c_message_expr_info) ->
if is_block_enumerate_function obj_c_message_expr_info then
block_enumeration_trans trans_state stmt_info stmt_list expr_info

@ -0,0 +1,53 @@
/*
* 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.
*/
struct X {
// operator which is a method
int operator[](int x) { return x; }
};
struct Y : public X {
};
// operator that is a function
int operator*(const X& x1, int v) {
return v;
}
int div0_method_op(X &x) {
// call method operator
int v = x[0];
return 1 / v;
}
int div0_method_op_ptr(X *x) {
return 1 / (*x)[0];
}
int div0_function_op(X &x) {
// call function operator
int v = x * 0;
return 1 / v;
}
int div0_method(X &x) {
// call method operator as a method
int v = x.operator[](0);
return 1 / v;
}
int div0_inheritted_op(Y &y) {
// call operator of superclass
return 1 / y[0];
}
int div1_method_op(X &x) {
return 1 / x[1];
}

@ -0,0 +1,102 @@
digraph iCFG {
27 [label="27: Return Stmt \n n$0=*&x:class X & [line 52]\n n$1=_fun_X_operator[](n$0:class X &,1:int ) [line 52]\n *&return:int =(1 / n$1) [line 52]\n REMOVE_TEMPS(n$0,n$1); [line 52]\n NULLIFY(&x,false); [line 52]\n APPLY_ABSTRACTION; [line 52]\n " shape="box"]
27 -> 26 ;
26 [label="26: Exit div1_method_op \n " color=yellow style=filled]
25 [label="25: Start div1_method_op\nFormals: x:class X &\nLocals: \n DECLARE_LOCALS(&return); [line 51]\n " color=yellow style=filled]
25 -> 27 ;
24 [label="24: Return Stmt \n n$0=*&y:class Y & [line 48]\n n$1=_fun_X_operator[](n$0:class X &,0:int ) [line 48]\n *&return:int =(1 / n$1) [line 48]\n REMOVE_TEMPS(n$0,n$1); [line 48]\n NULLIFY(&y,false); [line 48]\n APPLY_ABSTRACTION; [line 48]\n " shape="box"]
24 -> 23 ;
23 [label="23: Exit div0_inheritted_op \n " color=yellow style=filled]
22 [label="22: Start div0_inheritted_op\nFormals: y:class Y &\nLocals: \n DECLARE_LOCALS(&return); [line 46]\n " color=yellow style=filled]
22 -> 24 ;
21 [label="21: DeclStmt \n n$1=*&x:class X & [line 42]\n n$2=_fun_X_operator[](n$1:class X &,0:int ) [line 42]\n *&v:int =n$2 [line 42]\n REMOVE_TEMPS(n$1,n$2); [line 42]\n NULLIFY(&x,false); [line 42]\n " shape="box"]
21 -> 20 ;
20 [label="20: Return Stmt \n n$0=*&v:int [line 43]\n *&return:int =(1 / n$0) [line 43]\n REMOVE_TEMPS(n$0); [line 43]\n NULLIFY(&v,false); [line 43]\n APPLY_ABSTRACTION; [line 43]\n " shape="box"]
20 -> 19 ;
19 [label="19: Exit div0_method \n " color=yellow style=filled]
18 [label="18: Start div0_method\nFormals: x:class X &\nLocals: v:int \n DECLARE_LOCALS(&return,&v); [line 40]\n NULLIFY(&v,false); [line 40]\n " color=yellow style=filled]
18 -> 21 ;
17 [label="17: DeclStmt \n n$1=*&x:class X & [line 36]\n n$2=_fun_operator*(n$1:class X &,0:int ) [line 36]\n *&v:int =n$2 [line 36]\n REMOVE_TEMPS(n$1,n$2); [line 36]\n NULLIFY(&x,false); [line 36]\n " shape="box"]
17 -> 16 ;
16 [label="16: Return Stmt \n n$0=*&v:int [line 37]\n *&return:int =(1 / n$0) [line 37]\n REMOVE_TEMPS(n$0); [line 37]\n NULLIFY(&v,false); [line 37]\n APPLY_ABSTRACTION; [line 37]\n " shape="box"]
16 -> 15 ;
15 [label="15: Exit div0_function_op \n " color=yellow style=filled]
14 [label="14: Start div0_function_op\nFormals: x:class X &\nLocals: v:int \n DECLARE_LOCALS(&return,&v); [line 34]\n NULLIFY(&v,false); [line 34]\n " color=yellow style=filled]
14 -> 17 ;
13 [label="13: Return Stmt \n n$0=*&x:class X * [line 31]\n n$1=_fun_X_operator[](n$0:class X &,0:int ) [line 31]\n *&return:int =(1 / n$1) [line 31]\n REMOVE_TEMPS(n$0,n$1); [line 31]\n NULLIFY(&x,false); [line 31]\n APPLY_ABSTRACTION; [line 31]\n " shape="box"]
13 -> 12 ;
12 [label="12: Exit div0_method_op_ptr \n " color=yellow style=filled]
11 [label="11: Start div0_method_op_ptr\nFormals: x:class X *\nLocals: \n DECLARE_LOCALS(&return); [line 30]\n " color=yellow style=filled]
11 -> 13 ;
10 [label="10: DeclStmt \n n$1=*&x:class X & [line 26]\n n$2=_fun_X_operator[](n$1:class X &,0:int ) [line 26]\n *&v:int =n$2 [line 26]\n REMOVE_TEMPS(n$1,n$2); [line 26]\n NULLIFY(&x,false); [line 26]\n " shape="box"]
10 -> 9 ;
9 [label="9: Return Stmt \n n$0=*&v:int [line 27]\n *&return:int =(1 / n$0) [line 27]\n REMOVE_TEMPS(n$0); [line 27]\n NULLIFY(&v,false); [line 27]\n APPLY_ABSTRACTION; [line 27]\n " shape="box"]
9 -> 8 ;
8 [label="8: Exit div0_method_op \n " color=yellow style=filled]
7 [label="7: Start div0_method_op\nFormals: x:class X &\nLocals: v:int \n DECLARE_LOCALS(&return,&v); [line 24]\n NULLIFY(&v,false); [line 24]\n " color=yellow style=filled]
7 -> 10 ;
6 [label="6: Return Stmt \n n$0=*&v:int [line 21]\n *&return:int =n$0 [line 21]\n REMOVE_TEMPS(n$0); [line 21]\n NULLIFY(&v,false); [line 21]\n APPLY_ABSTRACTION; [line 21]\n " shape="box"]
6 -> 5 ;
5 [label="5: Exit operator* \n " color=yellow style=filled]
4 [label="4: Start operator*\nFormals: x1:class X & v:int \nLocals: \n DECLARE_LOCALS(&return); [line 20]\n NULLIFY(&x1,false); [line 20]\n " color=yellow style=filled]
4 -> 6 ;
3 [label="3: Return Stmt \n n$0=*&x:int [line 12]\n *&return:int =n$0 [line 12]\n REMOVE_TEMPS(n$0); [line 12]\n NULLIFY(&x,false); [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="box"]
3 -> 2 ;
2 [label="2: Exit X_operator[] \n " color=yellow style=filled]
1 [label="1: Start X_operator[]\nFormals: this:class X * x:int \nLocals: \n DECLARE_LOCALS(&return); [line 12]\n NULLIFY(&this,false); [line 12]\n " color=yellow style=filled]
1 -> 3 ;
}

@ -0,0 +1,67 @@
/*
* 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.
*/
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 OperatorOverloadTest {
public static final String FILE =
"infer/tests/codetoanalyze/cpp/frontend/types/operator_overload.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 = {
"div0_method_op",
"div0_method_op_ptr",
"div0_function_op",
"div0_method",
"div0_inheritted_op",
};
assertThat(
"Results should contain the expected divide by zero",
inferResults,
containsExactly(
DIVIDE_BY_ZERO,
FILE,
procedures
)
);
}
}

@ -20,14 +20,25 @@ import utils.ClangFrontendUtils;
public class FunctionsTest {
String basePath = "infer/tests/codetoanalyze/cpp/frontend/types/";
@Rule
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
void frontendTest(String fileRelative) throws InterruptedException, IOException, InferException {
ClangFrontendUtils.createAndCompareCppDotFiles(folder, basePath + fileRelative);
}
@Test
public void whenCaptureRunCommaThenDotFilesAreTheSame()
throws InterruptedException, IOException, InferException {
String src =
"infer/tests/codetoanalyze/cpp/frontend/types/functions.cpp";
ClangFrontendUtils.createAndCompareCppDotFiles(folder, src);
frontendTest("functions.cpp");
}
@Test
public void testOperatorDotFilesMatch()
throws InterruptedException, IOException, InferException {
frontendTest("operator_overload.cpp");
}
}

Loading…
Cancel
Save