[frontend] Remove duplicate variable declaration

Summary:
We get duplicated variable declaration instruction for primitive type variable initialized using list initializer, e.g.
```
int* p{nullptr};
```
This happens because we add variable declaration instruction when we translate both `DeclStmt` and `InitListExpr`. To fix this, we do not add the duplicated variable declaration when we translate `InitListExpr`.

Reviewed By: jvillard

Differential Revision: D22844726

fbshipit-source-id: 422806924
master
Daiva Naudziuniene 4 years ago committed by Facebook GitHub Bot
parent 5bd78660ea
commit c0152f2278

@ -2351,7 +2351,8 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
and initListExpr_builtin_trans trans_state stmt_info stmts var_exp var_typ =
match stmts with
| [stmt] ->
[init_expr_trans trans_state (var_exp, var_typ) stmt_info (Some stmt)]
[ init_expr_trans ~is_initListExpr_builtin:true trans_state (var_exp, var_typ) stmt_info
(Some stmt) ]
| _ ->
CFrontend_errors.unimplemented __POS__ stmt_info.Clang_ast_t.si_source_range
"InitListExpression for var %a type %a with multiple init statements" Exp.pp var_exp
@ -2434,8 +2435,8 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
|> mk_trans_result ret_exp_typ
and init_expr_trans ?(is_var_unused = false) trans_state var_exp_typ ?qual_type var_stmt_info
init_expr_opt =
and init_expr_trans ?(is_var_unused = false) ?(is_initListExpr_builtin = false) trans_state
var_exp_typ ?qual_type var_stmt_info init_expr_opt =
match init_expr_opt with
| None -> (
match Option.bind qual_type ~f:(fun qt -> CAst_utils.get_type qt.Clang_ast_t.qt_type_ptr) with
@ -2477,7 +2478,9 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
in
let pre_init_opt =
match var_exp with
| Exp.Lvar pvar ->
| Exp.Lvar pvar when not is_initListExpr_builtin ->
(* Do not add duplicated variable declaration when we translate InitListExpr
as it will be added in the translation of DeclStmt *)
Some
{ empty_control with
instrs= [Sil.Metadata (VariableLifetimeBegins (pvar, var_typ, sil_loc))] }

@ -25,6 +25,7 @@ struct C { // non-POD struct
void zero_init_primitive() {
int i{};
int* p{};
int* p2{nullptr};
float f{};
}

@ -7,7 +7,7 @@ digraph cfg {
"init_in_binop#init_list#8348250075128359911.7adaa67964536570064366a92056cf46_2" [label="2: Exit init_list::init_in_binop \n " color=yellow style=filled]
"init_in_binop#init_list#8348250075128359911.7adaa67964536570064366a92056cf46_3" [label="3: BinaryOperatorStmt: Assign \n n$0=*&x:int [line 51, column 34]\n VARIABLE_DECLARED(0$?%__sil_tmpSIL_init_list__n$1:int); [line 51, column 42]\n *&0$?%__sil_tmpSIL_init_list__n$1:int=0 [line 51, column 42]\n *&x:int=(-n$0 & ~&0$?%__sil_tmpSIL_init_list__n$1) [line 51, column 29]\n " shape="box"]
"init_in_binop#init_list#8348250075128359911.7adaa67964536570064366a92056cf46_3" [label="3: BinaryOperatorStmt: Assign \n n$0=*&x:int [line 52, column 34]\n *&0$?%__sil_tmpSIL_init_list__n$1:int=0 [line 52, column 42]\n *&x:int=(-n$0 & ~&0$?%__sil_tmpSIL_init_list__n$1) [line 52, column 29]\n " shape="box"]
"init_in_binop#init_list#8348250075128359911.7adaa67964536570064366a92056cf46_3" -> "init_in_binop#init_list#8348250075128359911.7adaa67964536570064366a92056cf46_2" ;
@ -18,23 +18,23 @@ digraph cfg {
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_2" [label="2: Exit init_list::list_init \n " color=yellow style=filled]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_3" [label="3: Destruction(Scope) \n _=*&y:init_list::Y [line 49, column 1]\n n$1=_fun_init_list::Y::~Y(&y:init_list::Y*) injected [line 49, column 1]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_3" [label="3: Destruction(Scope) \n _=*&y:init_list::Y [line 50, column 1]\n n$1=_fun_init_list::Y::~Y(&y:init_list::Y*) injected [line 50, column 1]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_3" -> "list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_2" ;
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_4" [label="4: DeclStmt \n VARIABLE_DECLARED(ty:init_list::Y[3*24]); [line 48, column 3]\n *&ty[0].z:int=1 [line 48, column 14]\n *&ty[0].x.a:int=2 [line 48, column 18]\n *&ty[0].x.p:int*=null [line 48, column 18]\n n$3=_fun_init_list::Y::Y(&ty[1]:init_list::Y*,&y:init_list::Y&) [line 48, column 33]\n n$4=*&yref:init_list::Y& [line 48, column 36]\n n$5=_fun_init_list::Y::Y(&ty[2]:init_list::Y*,n$4:init_list::Y&) [line 48, column 36]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_4" [label="4: DeclStmt \n VARIABLE_DECLARED(ty:init_list::Y[3*24]); [line 49, column 3]\n *&ty[0].z:int=1 [line 49, column 14]\n *&ty[0].x.a:int=2 [line 49, column 18]\n *&ty[0].x.p:int*=null [line 49, column 18]\n n$3=_fun_init_list::Y::Y(&ty[1]:init_list::Y*,&y:init_list::Y&) [line 49, column 33]\n n$4=*&yref:init_list::Y& [line 49, column 36]\n n$5=_fun_init_list::Y::Y(&ty[2]:init_list::Y*,n$4:init_list::Y&) [line 49, column 36]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_4" -> "list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_3" ;
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_5" [label="5: DeclStmt \n VARIABLE_DECLARED(yref:init_list::Y&); [line 47, column 3]\n *&yref:init_list::Y&=&y [line 47, column 3]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_5" [label="5: DeclStmt \n VARIABLE_DECLARED(yref:init_list::Y&); [line 48, column 3]\n *&yref:init_list::Y&=&y [line 48, column 3]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_5" -> "list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_4" ;
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_6" [label="6: DeclStmt \n VARIABLE_DECLARED(y:init_list::Y); [line 46, column 3]\n n$6=_fun_init_list::Y::Y(&y:init_list::Y*) [line 46, column 5]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_6" [label="6: DeclStmt \n VARIABLE_DECLARED(y:init_list::Y); [line 47, column 3]\n n$6=_fun_init_list::Y::Y(&y:init_list::Y*) [line 47, column 5]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_6" -> "list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_5" ;
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_7" [label="7: DeclStmt \n VARIABLE_DECLARED(ti:int[4*4]); [line 45, column 3]\n *&ti[0]:int=1 [line 45, column 15]\n *&ti[1]:int=2 [line 45, column 15]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_7" [label="7: DeclStmt \n VARIABLE_DECLARED(ti:int[4*4]); [line 46, column 3]\n *&ti[0]:int=1 [line 46, column 15]\n *&ti[1]:int=2 [line 46, column 15]\n " shape="box"]
"list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_7" -> "list_init#init_list#18348854466346904105.0126b9f1f80f91b73d5fbdbf2bc60754_6" ;
@ -45,45 +45,49 @@ digraph cfg {
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_2" [label="2: Exit init_list::record_init \n " color=yellow style=filled]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_3" [label="3: Destruction(Scope) \n _=*&c:init_list::C [line 42, column 1]\n n$1=_fun_init_list::C::~C(&c:init_list::C*) injected [line 42, column 1]\n _=*&y2:init_list::Y [line 42, column 1]\n n$3=_fun_init_list::Y::~Y(&y2:init_list::Y*) injected [line 42, column 1]\n _=*&y1:init_list::Y [line 42, column 1]\n n$5=_fun_init_list::Y::~Y(&y1:init_list::Y*) injected [line 42, column 1]\n _=*&x:init_list::X [line 42, column 1]\n n$7=_fun_init_list::X::~X(&x:init_list::X*) injected [line 42, column 1]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_3" [label="3: Destruction(Scope) \n _=*&c:init_list::C [line 43, column 1]\n n$1=_fun_init_list::C::~C(&c:init_list::C*) injected [line 43, column 1]\n _=*&y2:init_list::Y [line 43, column 1]\n n$3=_fun_init_list::Y::~Y(&y2:init_list::Y*) injected [line 43, column 1]\n _=*&y1:init_list::Y [line 43, column 1]\n n$5=_fun_init_list::Y::~Y(&y1:init_list::Y*) injected [line 43, column 1]\n _=*&x:init_list::X [line 43, column 1]\n n$7=_fun_init_list::X::~X(&x:init_list::X*) injected [line 43, column 1]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_3" -> "record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_2" ;
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_4" [label="4: DeclStmt \n VARIABLE_DECLARED(c:init_list::C); [line 41, column 3]\n n$9=_fun_init_list::C::C(&c:init_list::C*,1:int,2:int,&x:init_list::X&) [line 41, column 5]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_4" [label="4: DeclStmt \n VARIABLE_DECLARED(c:init_list::C); [line 42, column 3]\n n$9=_fun_init_list::C::C(&c:init_list::C*,1:int,2:int,&x:init_list::X&) [line 42, column 5]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_4" -> "record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_3" ;
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_5" [label="5: DeclStmt \n VARIABLE_DECLARED(y2:init_list::Y); [line 39, column 3]\n *&y2.z:int=1 [line 39, column 7]\n *&y2.x.a:int=2 [line 39, column 11]\n *&y2.x.p:int*=null [line 39, column 11]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_5" [label="5: DeclStmt \n VARIABLE_DECLARED(y2:init_list::Y); [line 40, column 3]\n *&y2.z:int=1 [line 40, column 7]\n *&y2.x.a:int=2 [line 40, column 11]\n *&y2.x.p:int*=null [line 40, column 11]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_5" -> "record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_4" ;
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_6" [label="6: DeclStmt \n VARIABLE_DECLARED(y1:init_list::Y); [line 38, column 3]\n *&y1.z:int=1 [line 38, column 7]\n n$10=_fun_init_list::X::X(&y1.x:init_list::X*,&x:init_list::X&) [line 38, column 11]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_6" [label="6: DeclStmt \n VARIABLE_DECLARED(y1:init_list::Y); [line 39, column 3]\n *&y1.z:int=1 [line 39, column 7]\n n$10=_fun_init_list::X::X(&y1.x:init_list::X*,&x:init_list::X&) [line 39, column 11]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_6" -> "record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_5" ;
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_7" [label="7: DeclStmt \n VARIABLE_DECLARED(x:init_list::X); [line 37, column 3]\n *&x.a:int=1 [line 37, column 6]\n *&x.p:int*=null [line 37, column 6]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_7" [label="7: DeclStmt \n VARIABLE_DECLARED(x:init_list::X); [line 38, column 3]\n *&x.a:int=1 [line 38, column 6]\n *&x.p:int*=null [line 38, column 6]\n " shape="box"]
"record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_7" -> "record_init#init_list#9390182661430352809.a0bac2a3cf71c0b7c450ce49d030845f_6" ;
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_1" [label="1: Start init_list::zero_init_primitive\nFormals: \nLocals: f:float p:int* i:int \n " color=yellow style=filled]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_1" [label="1: Start init_list::zero_init_primitive\nFormals: \nLocals: f:float p2:int* p:int* i:int \n " color=yellow style=filled]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_1" -> "zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_5" ;
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_1" -> "zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_6" ;
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_2" [label="2: Exit init_list::zero_init_primitive \n " color=yellow style=filled]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_3" [label="3: DeclStmt \n VARIABLE_DECLARED(f:float); [line 28, column 3]\n *&f:float=0. [line 28, column 3]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_3" [label="3: DeclStmt \n VARIABLE_DECLARED(f:float); [line 29, column 3]\n *&f:float=0. [line 29, column 3]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_3" -> "zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_2" ;
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_4" [label="4: DeclStmt \n VARIABLE_DECLARED(p:int*); [line 27, column 3]\n *&p:int*=null [line 27, column 3]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_4" [label="4: DeclStmt \n VARIABLE_DECLARED(p2:int*); [line 28, column 3]\n *&p2:int*=null [line 28, column 10]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_4" -> "zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_3" ;
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_5" [label="5: DeclStmt \n VARIABLE_DECLARED(i:int); [line 26, column 3]\n *&i:int=0 [line 26, column 3]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_5" [label="5: DeclStmt \n VARIABLE_DECLARED(p:int*); [line 27, column 3]\n *&p:int*=null [line 27, column 3]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_5" -> "zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_4" ;
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_6" [label="6: DeclStmt \n VARIABLE_DECLARED(i:int); [line 26, column 3]\n *&i:int=0 [line 26, column 3]\n " shape="box"]
"zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_6" -> "zero_init_primitive#init_list#3465759276925732066.1d03db1e38d38f4b345f33049176e92c_5" ;
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_1" [label="1: Start init_list::zero_init_record\nFormals: \nLocals: c:init_list::C y:init_list::Y \n " color=yellow style=filled]
@ -91,15 +95,15 @@ digraph cfg {
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_2" [label="2: Exit init_list::zero_init_record \n " color=yellow style=filled]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_3" [label="3: Destruction(Scope) \n _=*&c:init_list::C [line 34, column 1]\n n$1=_fun_init_list::C::~C(&c:init_list::C*) injected [line 34, column 1]\n _=*&y:init_list::Y [line 34, column 1]\n n$3=_fun_init_list::Y::~Y(&y:init_list::Y*) injected [line 34, column 1]\n " shape="box"]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_3" [label="3: Destruction(Scope) \n _=*&c:init_list::C [line 35, column 1]\n n$1=_fun_init_list::C::~C(&c:init_list::C*) injected [line 35, column 1]\n _=*&y:init_list::Y [line 35, column 1]\n n$3=_fun_init_list::Y::~Y(&y:init_list::Y*) injected [line 35, column 1]\n " shape="box"]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_3" -> "zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_2" ;
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_4" [label="4: DeclStmt \n VARIABLE_DECLARED(c:init_list::C); [line 33, column 3]\n n$5=_fun_init_list::C::C(&c:init_list::C*) [line 33, column 5]\n " shape="box"]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_4" [label="4: DeclStmt \n VARIABLE_DECLARED(c:init_list::C); [line 34, column 3]\n n$5=_fun_init_list::C::C(&c:init_list::C*) [line 34, column 5]\n " shape="box"]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_4" -> "zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_3" ;
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_5" [label="5: DeclStmt \n VARIABLE_DECLARED(y:init_list::Y); [line 32, column 3]\n *&y.z:int=0 [line 32, column 7]\n *&y.x.a:int=0 [line 32, column 7]\n *&y.x.p:int*=null [line 32, column 7]\n " shape="box"]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_5" [label="5: DeclStmt \n VARIABLE_DECLARED(y:init_list::Y); [line 33, column 3]\n *&y.z:int=0 [line 33, column 7]\n *&y.x.a:int=0 [line 33, column 7]\n *&y.x.p:int*=null [line 33, column 7]\n " shape="box"]
"zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_5" -> "zero_init_record#init_list#7364160241041626579.8baaea62666796dca7b4a7b11bf4f2bb_4" ;

Loading…
Cancel
Save