[C++ translation] Use correct type when generating dereference

Summary:
This modifies `CTrans_utils.dereference_value_from_result` function. Reasons:
1. Type in Sil.Load tuple is supposed to be the type of Ident.t field (result), not Exp.t (source)
2. Inside `dereference_value_from_result` we used wrong type when `~strip_pointer:true` - it used original type instead of the resulting one
3. Fixing (2), uncovered similar issue in `CTrans_utils.cast_operation` - it should have used resulting type, not original one

Changed tests are expected:
1. `deprecated_hack.cpp` test was how I discovered this issue (it was very wrong)
2. the other test also had issues, now it's correct

Sadly, there was no logic I followed when writing this change - I stuffed changes in a way that fits, but it may be breaking something somewhere else.

Reviewed By: jberdine

Differential Revision: D4481895

fbshipit-source-id: b139c59
master
Andrzej Kotulski 8 years ago committed by Facebook Github Bot
parent 269ea5ffe5
commit f697739b21

@ -668,7 +668,7 @@ struct
match typ with
| Tptr (_, Pk_reference) ->
(* dereference pvar due to the behavior of reference types in clang's AST *)
dereference_value_from_result sil_loc res_trans ~strip_pointer:true
dereference_value_from_result sil_loc res_trans ~strip_pointer:false
| _ -> res_trans
and decl_ref_trans trans_state pre_trans_result stmt_info decl_ref ~is_constructor_init =

@ -399,9 +399,9 @@ let dereference_var_sil (exp, typ) sil_loc =
assigned to it *)
let dereference_value_from_result sil_loc trans_result ~strip_pointer =
let (obj_sil, class_typ) = extract_exp_from_list trans_result.exps "" in
let cast_inst, cast_exp = dereference_var_sil (obj_sil, class_typ) sil_loc in
let typ_no_ptr = match class_typ with | Typ.Tptr (typ, _) -> typ | _ -> assert false in
let cast_typ = if strip_pointer then typ_no_ptr else class_typ in
let cast_inst, cast_exp = dereference_var_sil (obj_sil, cast_typ) sil_loc in
{ trans_result with
instrs = trans_result.instrs @ cast_inst;
exps = [(cast_exp, cast_typ)]
@ -433,7 +433,7 @@ let cast_operation trans_state cast_kind exps cast_typ sil_loc is_objc_bridged =
| `LValueToRValue ->
(* Takes an LValue and allow it to use it as RValue. *)
(* So we assign the LValue to a temp and we pass it to the parent.*)
let instrs, deref_exp = dereference_var_sil (exp, typ) sil_loc in
let instrs, deref_exp = dereference_var_sil (exp, cast_typ) sil_loc in
instrs, (deref_exp, cast_typ)
| _ ->
Logging.err_debug

@ -7,7 +7,7 @@ digraph iCFG {
"derefFirstArg_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z24derefFirstArg_null_derefv}.927581a38e7f35f59353c6a172d4704c_2" [label="2: Exit derefFirstArg_null_deref \n " color=yellow style=filled]
"derefFirstArg_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z24derefFirstArg_null_derefv}.927581a38e7f35f59353c6a172d4704c_3" [label="3: Return Stmt \n n$0=*null:int* [line 39]\n *&return:int=n$0 [line 39]\n " shape="box"]
"derefFirstArg_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z24derefFirstArg_null_derefv}.927581a38e7f35f59353c6a172d4704c_3" [label="3: Return Stmt \n n$0=*null:int [line 39]\n *&return:int=n$0 [line 39]\n " shape="box"]
"derefFirstArg_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z24derefFirstArg_null_derefv}.927581a38e7f35f59353c6a172d4704c_3" -> "derefFirstArg_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z24derefFirstArg_null_derefv}.927581a38e7f35f59353c6a172d4704c_2" ;
@ -22,7 +22,7 @@ digraph iCFG {
"derefFirstArg_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22derefFirstArg_ok_derefv}.81162ff63fe403ed6f07c90fd7747246_2" [label="2: Exit derefFirstArg_ok_deref \n " color=yellow style=filled]
"derefFirstArg_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22derefFirstArg_ok_derefv}.81162ff63fe403ed6f07c90fd7747246_3" [label="3: Return Stmt \n n$0=*&a:int* [line 44]\n *&return:int=n$0 [line 44]\n " shape="box"]
"derefFirstArg_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22derefFirstArg_ok_derefv}.81162ff63fe403ed6f07c90fd7747246_3" [label="3: Return Stmt \n n$0=*&a:int [line 44]\n *&return:int=n$0 [line 44]\n " shape="box"]
"derefFirstArg_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22derefFirstArg_ok_derefv}.81162ff63fe403ed6f07c90fd7747246_3" -> "derefFirstArg_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22derefFirstArg_ok_derefv}.81162ff63fe403ed6f07c90fd7747246_2" ;
@ -37,7 +37,7 @@ digraph iCFG {
"derefFirstArg2_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z25derefFirstArg2_null_derefv}.87e84a45939c306e33dfa703cc7fd30b_2" [label="2: Exit derefFirstArg2_null_deref \n " color=yellow style=filled]
"derefFirstArg2_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z25derefFirstArg2_null_derefv}.87e84a45939c306e33dfa703cc7fd30b_3" [label="3: Return Stmt \n n$0=*null:int* [line 49]\n *&return:int=n$0 [line 49]\n " shape="box"]
"derefFirstArg2_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z25derefFirstArg2_null_derefv}.87e84a45939c306e33dfa703cc7fd30b_3" [label="3: Return Stmt \n n$0=*null:int [line 49]\n *&return:int=n$0 [line 49]\n " shape="box"]
"derefFirstArg2_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z25derefFirstArg2_null_derefv}.87e84a45939c306e33dfa703cc7fd30b_3" -> "derefFirstArg2_null_deref{d41d8cd98f00b204e9800998ecf8427e_Z25derefFirstArg2_null_derefv}.87e84a45939c306e33dfa703cc7fd30b_2" ;
@ -52,7 +52,7 @@ digraph iCFG {
"derefFirstArg2_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z23derefFirstArg2_ok_derefv}.c5c7a2c8b8d8c952220574562b32d658_2" [label="2: Exit derefFirstArg2_ok_deref \n " color=yellow style=filled]
"derefFirstArg2_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z23derefFirstArg2_ok_derefv}.c5c7a2c8b8d8c952220574562b32d658_3" [label="3: Return Stmt \n n$0=*&a:int* [line 54]\n *&return:int=n$0 [line 54]\n " shape="box"]
"derefFirstArg2_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z23derefFirstArg2_ok_derefv}.c5c7a2c8b8d8c952220574562b32d658_3" [label="3: Return Stmt \n n$0=*&a:int [line 54]\n *&return:int=n$0 [line 54]\n " shape="box"]
"derefFirstArg2_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z23derefFirstArg2_ok_derefv}.c5c7a2c8b8d8c952220574562b32d658_3" -> "derefFirstArg2_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z23derefFirstArg2_ok_derefv}.c5c7a2c8b8d8c952220574562b32d658_2" ;
@ -97,7 +97,7 @@ digraph iCFG {
"getPtr_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref1v}.ca0bcbfa30cc9f3486057a2fbf4c0a43_2" [label="2: Exit getPtr_null_deref1 \n " color=yellow style=filled]
"getPtr_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref1v}.ca0bcbfa30cc9f3486057a2fbf4c0a43_3" [label="3: Return Stmt \n _=*&t:int* [line 92]\n n$1=*&t:int*& [line 92]\n n$2=*n$1:int [line 92]\n *&return:int=n$2 [line 92]\n " shape="box"]
"getPtr_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref1v}.ca0bcbfa30cc9f3486057a2fbf4c0a43_3" [label="3: Return Stmt \n _=*&t:int* [line 92]\n n$1=*&t:int* [line 92]\n n$2=*n$1:int [line 92]\n *&return:int=n$2 [line 92]\n " shape="box"]
"getPtr_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref1v}.ca0bcbfa30cc9f3486057a2fbf4c0a43_3" -> "getPtr_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref1v}.ca0bcbfa30cc9f3486057a2fbf4c0a43_2" ;
@ -116,7 +116,7 @@ digraph iCFG {
"getPtr_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref2v}.c0658f9bf55154f40aa7887ee5e9c67d_2" [label="2: Exit getPtr_null_deref2 \n " color=yellow style=filled]
"getPtr_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref2v}.c0658f9bf55154f40aa7887ee5e9c67d_3" [label="3: Return Stmt \n _=*&t:int* [line 98]\n n$1=*&t:int*& [line 98]\n n$2=*n$1:int [line 98]\n *&return:int=n$2 [line 98]\n " shape="box"]
"getPtr_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref2v}.c0658f9bf55154f40aa7887ee5e9c67d_3" [label="3: Return Stmt \n _=*&t:int* [line 98]\n n$1=*&t:int* [line 98]\n n$2=*n$1:int [line 98]\n *&return:int=n$2 [line 98]\n " shape="box"]
"getPtr_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref2v}.c0658f9bf55154f40aa7887ee5e9c67d_3" -> "getPtr_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getPtr_null_deref2v}.c0658f9bf55154f40aa7887ee5e9c67d_2" ;
@ -135,7 +135,7 @@ digraph iCFG {
"getPtr_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getPtr_ok_derefv}.e6a87ae178aca92d95d36c354a58684b_2" [label="2: Exit getPtr_ok_deref \n " color=yellow style=filled]
"getPtr_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getPtr_ok_derefv}.e6a87ae178aca92d95d36c354a58684b_3" [label="3: Return Stmt \n _=*&t:int* [line 105]\n n$1=*&t:int*& [line 105]\n n$2=*n$1:int [line 105]\n *&return:int=n$2 [line 105]\n " shape="box"]
"getPtr_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getPtr_ok_derefv}.e6a87ae178aca92d95d36c354a58684b_3" [label="3: Return Stmt \n _=*&t:int* [line 105]\n n$1=*&t:int* [line 105]\n n$2=*n$1:int [line 105]\n *&return:int=n$2 [line 105]\n " shape="box"]
"getPtr_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getPtr_ok_derefv}.e6a87ae178aca92d95d36c354a58684b_3" -> "getPtr_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getPtr_ok_derefv}.e6a87ae178aca92d95d36c354a58684b_2" ;
@ -158,7 +158,7 @@ digraph iCFG {
"operator_star_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref1v}.768771cba782263aea8d94a1dbaadafa_2" [label="2: Exit operator_star_null_deref1 \n " color=yellow style=filled]
"operator_star_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref1v}.768771cba782263aea8d94a1dbaadafa_3" [label="3: Return Stmt \n n$0=*&t:int*& [line 111]\n n$1=*n$0:int* [line 111]\n *&return:int=n$1 [line 111]\n " shape="box"]
"operator_star_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref1v}.768771cba782263aea8d94a1dbaadafa_3" [label="3: Return Stmt \n n$0=*&t:int* [line 111]\n n$1=*n$0:int [line 111]\n *&return:int=n$1 [line 111]\n " shape="box"]
"operator_star_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref1v}.768771cba782263aea8d94a1dbaadafa_3" -> "operator_star_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref1v}.768771cba782263aea8d94a1dbaadafa_2" ;
@ -177,7 +177,7 @@ digraph iCFG {
"operator_star_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref2v}.5dff5fc29e89a752d52014e07a143b0d_2" [label="2: Exit operator_star_null_deref2 \n " color=yellow style=filled]
"operator_star_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref2v}.5dff5fc29e89a752d52014e07a143b0d_3" [label="3: Return Stmt \n _=*&t:int* [line 117]\n n$1=*&t:int*& [line 117]\n n$2=*n$1:int* [line 117]\n *&return:int=n$2 [line 117]\n " shape="box"]
"operator_star_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref2v}.5dff5fc29e89a752d52014e07a143b0d_3" [label="3: Return Stmt \n _=*&t:int* [line 117]\n n$1=*&t:int* [line 117]\n n$2=*n$1:int [line 117]\n *&return:int=n$2 [line 117]\n " shape="box"]
"operator_star_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref2v}.5dff5fc29e89a752d52014e07a143b0d_3" -> "operator_star_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z25operator_star_null_deref2v}.5dff5fc29e89a752d52014e07a143b0d_2" ;
@ -196,7 +196,7 @@ digraph iCFG {
"operator_star_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22operator_star_ok_derefv}.fddf0680fb971d2f1a70bb3c9074d811_2" [label="2: Exit operator_star_ok_deref \n " color=yellow style=filled]
"operator_star_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22operator_star_ok_derefv}.fddf0680fb971d2f1a70bb3c9074d811_3" [label="3: Return Stmt \n _=*&t:int* [line 124]\n n$1=*&t:int*& [line 124]\n n$2=*n$1:int* [line 124]\n *&return:int=n$2 [line 124]\n " shape="box"]
"operator_star_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22operator_star_ok_derefv}.fddf0680fb971d2f1a70bb3c9074d811_3" [label="3: Return Stmt \n _=*&t:int* [line 124]\n n$1=*&t:int* [line 124]\n n$2=*n$1:int [line 124]\n *&return:int=n$2 [line 124]\n " shape="box"]
"operator_star_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22operator_star_ok_derefv}.fddf0680fb971d2f1a70bb3c9074d811_3" -> "operator_star_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z22operator_star_ok_derefv}.fddf0680fb971d2f1a70bb3c9074d811_2" ;
@ -215,7 +215,7 @@ digraph iCFG {
"getRef_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref1v}.122119e932803230e14664a05f8e9bc1_2" [label="2: Exit getRef_null_deref1 \n " color=yellow style=filled]
"getRef_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref1v}.122119e932803230e14664a05f8e9bc1_3" [label="3: Return Stmt \n _=*&t:int* [line 130]\n n$1=*&t:int*& [line 130]\n n$2=*n$1:int* [line 130]\n *&return:int=n$2 [line 130]\n " shape="box"]
"getRef_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref1v}.122119e932803230e14664a05f8e9bc1_3" [label="3: Return Stmt \n _=*&t:int* [line 130]\n n$1=*&t:int* [line 130]\n n$2=*n$1:int [line 130]\n *&return:int=n$2 [line 130]\n " shape="box"]
"getRef_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref1v}.122119e932803230e14664a05f8e9bc1_3" -> "getRef_null_deref1{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref1v}.122119e932803230e14664a05f8e9bc1_2" ;
@ -234,7 +234,7 @@ digraph iCFG {
"getRef_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref2v}.9ffcde6f39df65a7c54071cb9a9ddd2d_2" [label="2: Exit getRef_null_deref2 \n " color=yellow style=filled]
"getRef_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref2v}.9ffcde6f39df65a7c54071cb9a9ddd2d_3" [label="3: Return Stmt \n _=*&t:int* [line 136]\n n$1=*&t:int*& [line 136]\n n$2=*n$1:int* [line 136]\n *&return:int=n$2 [line 136]\n " shape="box"]
"getRef_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref2v}.9ffcde6f39df65a7c54071cb9a9ddd2d_3" [label="3: Return Stmt \n _=*&t:int* [line 136]\n n$1=*&t:int* [line 136]\n n$2=*n$1:int [line 136]\n *&return:int=n$2 [line 136]\n " shape="box"]
"getRef_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref2v}.9ffcde6f39df65a7c54071cb9a9ddd2d_3" -> "getRef_null_deref2{d41d8cd98f00b204e9800998ecf8427e_Z18getRef_null_deref2v}.9ffcde6f39df65a7c54071cb9a9ddd2d_2" ;
@ -253,7 +253,7 @@ digraph iCFG {
"getRef_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getRef_ok_derefv}.a4f3a4a756890f6bb1065e1d22e6485f_2" [label="2: Exit getRef_ok_deref \n " color=yellow style=filled]
"getRef_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getRef_ok_derefv}.a4f3a4a756890f6bb1065e1d22e6485f_3" [label="3: Return Stmt \n _=*&t:int* [line 143]\n n$1=*&t:int*& [line 143]\n n$2=*n$1:int* [line 143]\n *&return:int=n$2 [line 143]\n " shape="box"]
"getRef_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getRef_ok_derefv}.a4f3a4a756890f6bb1065e1d22e6485f_3" [label="3: Return Stmt \n _=*&t:int* [line 143]\n n$1=*&t:int* [line 143]\n n$2=*n$1:int [line 143]\n *&return:int=n$2 [line 143]\n " shape="box"]
"getRef_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getRef_ok_derefv}.a4f3a4a756890f6bb1065e1d22e6485f_3" -> "getRef_ok_deref{d41d8cd98f00b204e9800998ecf8427e_Z15getRef_ok_derefv}.a4f3a4a756890f6bb1065e1d22e6485f_2" ;

@ -34,7 +34,7 @@ digraph iCFG {
"reference_field::ref_I_div0{d41d8cd98f00b204e9800998ecf8427e_ZN15reference_field10ref_I_div0Ev}.0bd2008b8587002a989e1bcf33d28ccc_2" [label="2: Exit reference_field::ref_I_div0 \n " color=yellow style=filled]
"reference_field::ref_I_div0{d41d8cd98f00b204e9800998ecf8427e_ZN15reference_field10ref_I_div0Ev}.0bd2008b8587002a989e1bcf33d28ccc_3" [label="3: Return Stmt \n n$0=*&r.i:int& [line 61]\n n$1=*n$0:int& [line 61]\n *&return:int=(1 / n$1) [line 61]\n " shape="box"]
"reference_field::ref_I_div0{d41d8cd98f00b204e9800998ecf8427e_ZN15reference_field10ref_I_div0Ev}.0bd2008b8587002a989e1bcf33d28ccc_3" [label="3: Return Stmt \n n$0=*&r.i:int& [line 61]\n n$1=*n$0:int [line 61]\n *&return:int=(1 / n$1) [line 61]\n " shape="box"]
"reference_field::ref_I_div0{d41d8cd98f00b204e9800998ecf8427e_ZN15reference_field10ref_I_div0Ev}.0bd2008b8587002a989e1bcf33d28ccc_3" -> "reference_field::ref_I_div0{d41d8cd98f00b204e9800998ecf8427e_ZN15reference_field10ref_I_div0Ev}.0bd2008b8587002a989e1bcf33d28ccc_2" ;
@ -379,7 +379,7 @@ digraph iCFG {
"reference_field::Ref_getI(_ZN15reference_field3Ref4getIEv).6c579f271ff6f4867c3e4cb775f86414_2" [label="2: Exit reference_field::Ref_getI \n " color=yellow style=filled]
"reference_field::Ref_getI(_ZN15reference_field3Ref4getIEv).6c579f271ff6f4867c3e4cb775f86414_3" [label="3: Return Stmt \n n$0=*&this:class reference_field::Ref* [line 28]\n n$1=*n$0.i:int& [line 28]\n n$2=*n$1:int& [line 28]\n *&return:int=n$2 [line 28]\n " shape="box"]
"reference_field::Ref_getI(_ZN15reference_field3Ref4getIEv).6c579f271ff6f4867c3e4cb775f86414_3" [label="3: Return Stmt \n n$0=*&this:class reference_field::Ref* [line 28]\n n$1=*n$0.i:int& [line 28]\n n$2=*n$1:int [line 28]\n *&return:int=n$2 [line 28]\n " shape="box"]
"reference_field::Ref_getI(_ZN15reference_field3Ref4getIEv).6c579f271ff6f4867c3e4cb775f86414_3" -> "reference_field::Ref_getI(_ZN15reference_field3Ref4getIEv).6c579f271ff6f4867c3e4cb775f86414_2" ;

Loading…
Cancel
Save